In this repository, I am gonna show you how to play with stripe payment gateway, create customer , one-time payment and subscriprion
- download this code from my GitHub
- open it into Jupyter notebook
- Now run its cells one by one
sudo apt install python3-pip python3-dev
pip install jupyter
pip install stripe
Sign up for Stripe at https://dashboard.stripe.com/register.
Now copy your Secret key it is like "sk_test_********************* "
First of all import stripe module and place your test api key in a variableimport stripe stripe.api_key = "sk_testxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"Create Customer
Now create customer because we need customer to process any payment in stripe
stripe.Customer.create( description="Customer for [email protected]", name = 'Rajat', email = "[email protected]", ) # it return a json respone where you can store its customer id for further process # JSON:{}
Go to this link and see customer is register successfully
customer_data = stripe.Customer.retrieve('Customer_id')
One-time payment is known as charge in the world of stripe payments. So we need to create charge.
stripe.Charge.create( amount=2000, # amount is $20 it show 20*100 currency="usd", # currency source="tok_mastercard", # this is default description="Charge for [email protected]" )""" it return charge id , txt id in return store it in your database
JSON: { "amount": 2000, "amount_refunded": 0, "application": null, "application_fee": null, "application_fee_amount": null, "balance_transaction": "txn_1Er87vIypo2lmEr61eixqsP0",\ """
As you can see , one time payment has been done.
First of all we need plans which user can subscribe , so go to link of stripe and create a plan.
Now click on new for create a product plan by click on new button and fill basic details like name , currency, price and Billing interval
As you can see i have created one plan.
fetch existing plan , bellow code will display all existing plans from you api , Here you need to capture plan_id it will use in subscription proceess
plan = stripe.Plan.list() plan_id = plan['data'][0]['id']
Now we have created plan , created customer and we have plan id . Now time to create subscription
# create Subscription stripe.Subscription.create( customer="cus_F2W1hNSAYf7pUm",# this is customer id who want to buy this subscription items=[ { "plan": "plan_F2VGZl0q6F51Cf", # this plan id of particular plan }, ] ) # it will also return json response save it and mark subscription id # subscription_id will use to cancel, update and track payment.
You can see its payment in transaction section.
And also check more details in subscription section.
stripe.Subscription.delete('sub_id')Moreover, you can also delete customer from stripe:
stripe.Customer.delete('cus_id')