Skip to content

Instantly share code, notes, and snippets.

@arshbot
Created September 13, 2019 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arshbot/2059a99f05cccc59e8be6ecac656b2ba to your computer and use it in GitHub Desktop.
Save arshbot/2059a99f05cccc59e8be6ecac656b2ba to your computer and use it in GitHub Desktop.
Stripe integration demo
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
stripe = "*"
requests = "*"
[requires]
python_version = "3.7"
from time import sleep
import stripe
stripe.api_key = ""
CREDIT_CARD = "4242424242424242"
CVC = "123"
MONTH = "12"
YEAR = "2020"
def register_card_with_stripe(cc_num, month, year, cvc):
return stripe.PaymentMethod.create(
type='card',
card={
'number': cc_num,
'exp_month': month,
'exp_year': year,
'cvc': cvc,
},
)
def charge_payment_source(payment_token, hold_funds=False):
intent = stripe.PaymentIntent.create(
amount=2000,
currency="usd",
payment_method_types=["card"],
payment_method=payment_token,
confirm=False if hold_funds else True,
capture_method='manual' if hold_funds else 'automatic',
)
return intent if not hold_funds else stripe.PaymentIntent.capture(
intent=intent.id,
amount_to_capture=2000,
)
# Step 1
payment_token = register_card_with_stripe(CREDIT_CARD, MONTH, YEAR, CVC)
# Step 2
res = charge_payment_source(payment_token)
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment