Skip to content

Commit

Permalink
ideal support; sry no tests yet!
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul424 committed Apr 19, 2017
1 parent ad267f5 commit a03b657
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
1 change: 0 additions & 1 deletion pinax/stripe/actions/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_imme
email=user.email,
source=card,
plan=plan,
quantity=quantity,
trial_end=trial_end
)
try:
Expand Down
52 changes: 52 additions & 0 deletions pinax/stripe/actions/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,44 @@ def sync_bitcoin(customer, source):
return utils.update_with_defaults(receiver, defaults, created)


def sync_ideal(customer, source):
"""
Syncronizes the data for an ideal source locally for a given customer
This is required since payment through ideal involves additional steps to be taken by the customer (select bank, enter codes, confirm, ...), updates will be made
available through webhooks or the return url and we then need to relate the payment to an instance of a source locally in order to process it further.
Args:
customer: the customer to create or update the source for
source: data reprenting the source from the Stripe API
"""
defaults = dict(
customer=customer,
amount = utils.convert_amount_for_db(source["amount"], source["currency"]), # currency is in but in fact it's always eur
flow = source["flow"] or "",
livemode = source["livemode"],
owner_address = source["owner"]["address"] or "",
owner_email = source["owner"]["email"] or "",
owner_name = source["owner"]["name"] or "",
owner_phone = source["owner"]["phone"] or "",
owner_verified_address = source["owner"]["verified_address"] or "",
owner_verified_email = source["owner"]["verified_email"] or "",
owner_verified_name = source["owner"]["verified_name"] or "",
owner_verified_phone = source["owner"]["verified_phone"] or "",
redirect_return_url = source["redirect"]["return_url"] or "",
redirect_status = source["redirect"]["status"] or "",
redirect_url = source["redirect"]["url"] or "",
status = source["status"] or "",
usage =source["usage"] or "",
ideal_bank = source["ideal"]["bank"] or "",
)
o, created = models.Ideal.objects.get_or_create(
stripe_id=source["id"],
defaults=defaults
)
return utils.update_with_defaults(o, defaults, created)


def sync_payment_source_from_stripe_data(customer, source):
"""
Syncronizes the data for a payment source locally for a given customer
Expand All @@ -116,6 +154,8 @@ def sync_payment_source_from_stripe_data(customer, source):
"""
if source["id"].startswith("card_"):
return sync_card(customer, source)
elif source["type"] == "ideal":
return sync_ideal(customer, source)
else:
return sync_bitcoin(customer, source)

Expand All @@ -140,3 +180,15 @@ def update_card(customer, source, name=None, exp_month=None, exp_year=None):
stripe_source.exp_year = exp_year
s = stripe_source.save()
return sync_payment_source_from_stripe_data(customer, s)


def create_ideal(customer, token):
"""
Attaches an ideal source to a customer
Args:
customer: the customer to create the source for
token: the token created from Stripe.js
"""
source = customer.stripe_customer.sources.create(source=token)
return sync_payment_source_from_stripe_data(customer, source)
10 changes: 9 additions & 1 deletion pinax/stripe/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Subscription,
Card,
BitcoinReceiver,
Ideal,
Customer,
Event,
EventProcessingException,
Expand Down Expand Up @@ -195,6 +196,12 @@ class BitcoinReceiverInline(admin.TabularInline):
max_num = 0


class IdealInline(admin.TabularInline):
model = Ideal
extra = 0
max_num = 0


def subscription_status(obj):
return ", ".join([subscription.status for subscription in obj.subscription_set.all()])
subscription_status.short_description = "Subscription Status"
Expand Down Expand Up @@ -224,7 +231,8 @@ def subscription_status(obj):
inlines=[
SubscriptionInline,
CardInline,
BitcoinReceiverInline
BitcoinReceiverInline,
IdealInline,
]
)

Expand Down
46 changes: 46 additions & 0 deletions pinax/stripe/migrations/0008_ideal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-04-19 14:32
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('pinax_stripe', '0007_auto_20170108_1202'),
]

operations = [
migrations.CreateModel(
name='Ideal',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('stripe_id', models.CharField(max_length=255, unique=True)),
('created_at', models.DateTimeField(default=django.utils.timezone.now)),
('amount', models.DecimalField(decimal_places=2, max_digits=9, null=True)),
('flow', models.CharField(max_length=32)),
('livemode', models.BooleanField(default=False)),
('owner_address', models.TextField(blank=True)),
('owner_email', models.EmailField(blank=True, max_length=254)),
('owner_name', models.TextField(blank=True)),
('owner_phone', models.TextField(blank=True)),
('owner_verified_address', models.TextField(blank=True)),
('owner_verified_email', models.EmailField(blank=True, max_length=254)),
('owner_verified_name', models.TextField(blank=True)),
('owner_verified_phone', models.TextField(blank=True)),
('redirect_return_url', models.URLField(max_length=1024)),
('redirect_status', models.TextField(blank=True)),
('redirect_url', models.URLField(max_length=1024)),
('status', models.TextField(blank=True)),
('usage', models.TextField(blank=True)),
('ideal_bank', models.TextField(blank=True)),
('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='pinax_stripe.Customer')),
],
options={
'abstract': False,
},
),
]
22 changes: 22 additions & 0 deletions pinax/stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ class BitcoinReceiver(StripeObject):
used_for_payment = models.BooleanField(default=False)


class Ideal(StripeObject):

customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
amount = models.DecimalField(decimal_places=2, max_digits=9, null=True)
flow = models.CharField(max_length=32)
livemode = models.BooleanField(default=False)
owner_address = models.TextField(blank=True)
owner_email = models.EmailField(blank=True)
owner_name = models.TextField(blank=True)
owner_phone = models.TextField(blank=True)
owner_verified_address = models.TextField(blank=True)
owner_verified_email = models.EmailField(blank=True)
owner_verified_name = models.TextField(blank=True)
owner_verified_phone = models.TextField(blank=True)
redirect_return_url = models.URLField(max_length=1024)
redirect_status = models.TextField(blank=True)
redirect_url = models.URLField(max_length=1024)
status = models.TextField(blank=True)
usage = models.TextField(blank=True)
ideal_bank = models.TextField(blank=True)


class Subscription(StripeObject):

customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
Expand Down

0 comments on commit a03b657

Please sign in to comment.