|
| 1 | +# -*- coding: iso8859-15 -*- |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +appdir = os.path.abspath(os.path.dirname(__file__)) |
| 6 | +projdir = os.path.abspath(os.path.join(appdir, '..')) |
| 7 | +if projdir not in sys.path: |
| 8 | + sys.path.append(appdir) |
| 9 | + sys.path.append(projdir) |
| 10 | + |
| 11 | +import stripe |
| 12 | +from config.settings import STRIPE_KEY, STRIPE_PRODUCT_ID |
| 13 | + |
| 14 | + |
| 15 | +stripe.api_key = STRIPE_KEY |
| 16 | + |
| 17 | +ACCEPT_TYPE = ['card'] |
| 18 | +ACCEPT_CURRENCIES = ['usd'] |
| 19 | + |
| 20 | + |
| 21 | +def generate_card_token(card_number, exp_month, exp_year, cvc): |
| 22 | + ''' Generate card token ''' |
| 23 | + data = stripe.Token.create( |
| 24 | + card={ |
| 25 | + "number": str(card_number), |
| 26 | + "exp_month": int(exp_month), |
| 27 | + "exp_year": int(exp_year), |
| 28 | + "cvc": str(cvc), |
| 29 | + }) |
| 30 | + card_token = data['id'] |
| 31 | + |
| 32 | + return card_token |
| 33 | + |
| 34 | + |
| 35 | +def create_payment_charge(token_id, amount): |
| 36 | + ''' Create payment charge ''' |
| 37 | + payment = stripe.Charge.create( |
| 38 | + amount=int(amount)*100, # convert amount to cents |
| 39 | + currency='usd', |
| 40 | + description='Example charge', |
| 41 | + source=token_id, |
| 42 | + ) |
| 43 | + |
| 44 | + payment_check = payment['paid'] # return True for payment |
| 45 | + |
| 46 | + return payment_check |
| 47 | + |
| 48 | + |
| 49 | +def create_subscription(payment_method_id, customer_id, price_id): |
| 50 | + ''' Create a subcription ''' |
| 51 | + stripe.PaymentMethod.attach(payment_method_id, customer=customer_id) |
| 52 | + # Set the default payment method on the customer |
| 53 | + stripe.Customer.modify(customer_id, |
| 54 | + invoice_settings={ |
| 55 | + 'default_payment_method': payment_method_id, |
| 56 | + }, |
| 57 | + ) |
| 58 | + # Create the subscription |
| 59 | + # Subscribe the user to the subscription created |
| 60 | + subscription = stripe.Subscription.create( |
| 61 | + customer=customer_id, |
| 62 | + items=[{"price": price_id, }], |
| 63 | + expand=["latest_invoice.payment_intent"] |
| 64 | + ) |
| 65 | + return subscription |
| 66 | + |
| 67 | + |
| 68 | +def get_subscription(subscription_id): |
| 69 | + ''' Get subcription by id''' |
| 70 | + subscription = stripe.Subscription.retrieve(subscription_id) |
| 71 | + return subscription |
| 72 | + |
| 73 | + |
| 74 | +def cancel_subscription(subscription_id): |
| 75 | + ''' Cancel a subcription ''' |
| 76 | + deletedSubscription = stripe.Subscription.delete(subscription_id) |
| 77 | + return deletedSubscription |
| 78 | + |
| 79 | + |
| 80 | +def create_customer(email): |
| 81 | + ''' Create new customer ''' |
| 82 | + customer = stripe.Customer.create(email=email) |
| 83 | + return customer |
| 84 | + |
| 85 | + |
| 86 | +def update_email_customer(customer_id, email): |
| 87 | + ''' Update customer email ''' |
| 88 | + customer = stripe.Customer.modify(customer_id, email=email) |
| 89 | + return customer |
| 90 | + |
| 91 | + |
| 92 | +def create_payment_card(card_number, exp_month, exp_year, cvc): |
| 93 | + ''' Create payment by card ''' |
| 94 | + payment_method = stripe.PaymentMethod.create( |
| 95 | + type="card", |
| 96 | + card={ |
| 97 | + "number": str(card_number), |
| 98 | + "exp_month": int(exp_month), |
| 99 | + "exp_year": int(exp_year), |
| 100 | + "cvc": str(cvc), |
| 101 | + }) |
| 102 | + return payment_method |
| 103 | + |
| 104 | + |
| 105 | +def create_price(unit_amount, currency): |
| 106 | + ''' Create new price ''' |
| 107 | + price = stripe.Price.create( |
| 108 | + unit_amount=int(unit_amount)*100, |
| 109 | + currency=str(currency), |
| 110 | + recurring={"interval": "year"}, |
| 111 | + product=STRIPE_PRODUCT_ID |
| 112 | + ) |
| 113 | + return price |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + print(stripe.Product.create(name="Upstage").id) |
0 commit comments