-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into issue-294
- Loading branch information
Showing
10 changed files
with
282 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import stripe | ||
|
||
from .. import utils | ||
from .. import models | ||
|
||
|
||
def sync_coupons(): | ||
""" | ||
Syncronizes all coupons from the Stripe API | ||
""" | ||
try: | ||
coupons = stripe.Coupon.auto_paging_iter() | ||
except AttributeError: | ||
coupons = iter(stripe.Coupon.all().data) | ||
|
||
for coupon in coupons: | ||
defaults = dict( | ||
amount_off=( | ||
utils.convert_amount_for_db(coupon["amount_off"], coupon["currency"]) | ||
if coupon["amount_off"] | ||
else None | ||
), | ||
currency=coupon["currency"] or "", | ||
duration=coupon["duration"], | ||
duration_in_months=coupon["duration_in_months"], | ||
max_redemptions=coupon["max_redemptions"], | ||
metadata=coupon["metadata"], | ||
percent_off=coupon["percent_off"], | ||
redeem_by=utils.convert_tstamp(coupon["redeem_by"]) if coupon["redeem_by"] else None, | ||
times_redeemed=coupon["times_redeemed"], | ||
valid=coupon["valid"], | ||
) | ||
obj, created = models.Coupon.objects.get_or_create( | ||
stripe_id=coupon["id"], | ||
defaults=defaults | ||
) | ||
utils.update_with_defaults(obj, defaults, created) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from ...actions import coupons | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
help = "Make sure your Stripe account has the coupons" | ||
|
||
def handle(self, *args, **options): | ||
coupons.sync_coupons() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.10 on 2016-12-20 03:16 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import django.utils.timezone | ||
import jsonfield.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('pinax_stripe', '0005_auto_20161006_1445'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Coupon', | ||
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_off', models.DecimalField(decimal_places=2, max_digits=9, null=True)), | ||
('currency', models.CharField(default='usd', max_length=10)), | ||
('duration', models.CharField(default='once', max_length=10)), | ||
('duration_in_months', models.PositiveIntegerField(null=True)), | ||
('livemode', models.BooleanField(default=False)), | ||
('max_redemptions', models.PositiveIntegerField(null=True)), | ||
('metadata', jsonfield.fields.JSONField(null=True)), | ||
('percent_off', models.PositiveIntegerField(null=True)), | ||
('redeem_by', models.DateTimeField(null=True)), | ||
('times_redeemed', models.PositiveIntegerField(null=True)), | ||
('valid', models.BooleanField(default=False)), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters