Skip to content

In this repository, I am gonna show you how to play with stripe payment gateway, create customer , one-time payment and subscriprion

Notifications You must be signed in to change notification settings

rajat4665/Stripe_payment_gateway_with_python_programming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Stripe-payment-gateway-with-python-programming

In this repository, I am gonna show you how to play with stripe payment gateway, create customer , one-time payment and subscriprion

How to run this code:

  • download this code from my GitHub
  • open it into Jupyter notebook
  • Now run its cells one by one

How to install jupyter notebook in Ubuntu:

open your terminal and paste these commands one by one.
sudo apt install python3-pip python3-dev
pip install jupyter

How to install jupyter notebook in windows:

Follow this Link for windows

Introduction:

Do you guys ever wonder how payment process work in E-commerce websites? The whole process work using API provided by a payment gateway provider and these provider charges some percent in the return, for example, stripe charge about 3 percent from a successful transition. One just needs to follow their procedure on how to integrate with your website. It is very easy because most of the payment gateway provides have really good documentation. There are lots of payment gateways are available in markets such as Paypal, Paytm, Wepay and Stripe.  I think you guys already heard about some of them.  In this post, I am gonna show you how one can set up stripe payment on their website. and how to set up a one-time payment, create customer and set subscription using stripe API.

Install these requirements:

pip install stripe

Setting up a Stripe Account :

Sign up for Stripe at https://dashboard.stripe.com/register.

Read its official document for python from this link
Now go to api keys option under Developer section or simply follow this link

Basic understanding of api:

There are two api keys one is live and other is test. For live one should attach their credit details. Test keys are available for everyone because it is just for testing and I am also demonstrate with test keys.

Now copy your Secret key  it is like "sk_test_********************* "

Screenshot from 2019-06-30 23-32-14

 

Lets start playing with this api:

First of all import stripe module and place your test api key in a variable
import 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 rajat4665@gmail.com",
    name = 'Rajat',
    email = "rajat4665@gmail.com",
)
# 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

Screenshot from 2019-07-01 00-16-37

 

How to fetch data of existing customers:

customer_data = stripe.Customer.retrieve('Customer_id')

 

One-time payment:

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 jenny.rajat4665@example.com"
)

""" 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",\
"""

Screenshot from 2019-07-01 00-23-44.png

As you can see , one time payment has been done.

 

How to create Subscriprion in stripe :

First of all we need plans which user can subscribe , so go to link of stripe and create a plan.

Screenshot from 2019-07-01 00-29-04

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

Screenshot from 2019-07-01 00-53-04.png

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.

Screenshot from 2019-07-01 01-04-17

And also check more details in subscription section.

Screenshot from 2019-07-01 01-04-35

How to cancel this Subscription:

 stripe.Subscription.delete('sub_id')
Moreover, you can also delete customer from stripe:
stripe.Customer.delete('cus_id')
Thank you for reading this article, stay tuned for more updates!!!!!!!

References:

https://stripe.com/docs/api?lang=python

About

In this repository, I am gonna show you how to play with stripe payment gateway, create customer , one-time payment and subscriprion

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published