Created
September 13, 2019 21:26
-
-
Save arshbot/2059a99f05cccc59e8be6ecac656b2ba to your computer and use it in GitHub Desktop.
Stripe integration demo
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
[[source]] | |
name = "pypi" | |
url = "https://pypi.org/simple" | |
verify_ssl = true | |
[dev-packages] | |
[packages] | |
stripe = "*" | |
requests = "*" | |
[requires] | |
python_version = "3.7" |
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
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