Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import stripe
- from dotenv import load_dotenv
- import os
- load_dotenv()
- stripe.api_key = os.getenv('STRIPE_SECRET_KEY')
- # 1. Create a Customer
- def create_customer():
- customer = stripe.Customer.create(
- email="testuser@example.com",
- name="10.01.25",
- description="A test customer for Stripe",
- )
- print("Customer created:", customer)
- return customer.id
- # 2. Create a Payment Intent # Valid card
- def create_payment_intent(customer_id):
- payment_intent = stripe.PaymentIntent.create(
- amount=100, # Amount in the smallest currency unit (e.g., 1000 = $10.00)
- currency="bgn",
- customer=customer_id,
- payment_method_types=["card"], # Supported payment methods
- )
- print("Payment Intent created:", payment_intent)
- return payment_intent.client_secret
- # def create_payment_intent(customer_id): # Invalid card
- # try:
- # payment_intent = stripe.PaymentIntent.create(
- # amount=1000, # Amount in stotinki (1000 = 10.00 лв)
- # currency="bgn",
- # customer=customer_id,
- # payment_method_data={
- # "type": "card",
- # "card": {
- # "number": "4000000000009995", # Card declined
- # "exp_month": 12,
- # "exp_year": 34,
- # "cvc": "123",
- # },
- # },
- # confirm=True, # Simulate immediate confirmation
- # )
- # print("Payment Intent created:", payment_intent)
- #
- # except stripe.error.CardError as e:
- # print("Payment failed!")
- # print("Error code:", e.error.code)
- # print("Error message:", e.user_message)
- # """
- # raise err
- # stripe._error.InvalidRequestError: Request req_mysHynuQ01gRUi:
- # This PaymentIntent is configured to accept payment methods enabled in your Dashboard.
- # because some of these payment methods might redirect your customer off of your page,
- # you must provide a `return_url`. If you don't want to accept redirect-based payment
- # methods, set `automatic_payment_methods[enabled]` to `true`
- # and `automatic_payment_methods[allow_redirects]` to `never`
- # when creating Setup Intents and Payment Intents.
- # """
- # 3. Simulate Successful Payment
- def confirm_payment(payment_intent_client_secret):
- print("\nSimulate Payment:")
- print(f"Use test card 4242 4242 4242 4242 with any valid expiration and CVV.")
- print(f"Client secret for this transaction: {payment_intent_client_secret}")
- if __name__ == "__main__":
- print("Testing Stripe API...\n")
- # Step 1: Create a Customer
- customer_id = create_customer()
- # Step 2: Create a Payment Intent for the Customer
- client_secret = create_payment_intent(customer_id)
- # Step 3: Simulate the payment with test card info
- confirm_payment(client_secret)
- _____________________________________________________________________________________________________
- return:
- Testing Stripe API...
- Customer created: {
- "address": null,
- "balance": 0,
- "created": 1736532835,
- "currency": null,
- "default_source": null,
- "delinquent": false,
- "description": "A test customer for Stripe",
- "discount": null,
- "email": "testuser@example.com",
- "id": "cus_RYuXWZgWR8FKq1",
- "invoice_prefix": "2B206D93",
- "invoice_settings": {
- "custom_fields": null,
- "default_payment_method": null,
- "footer": null,
- "rendering_options": null
- },
- "livemode": false,
- "metadata": {},
- "name": "10.01.25",
- "object": "customer",
- "phone": null,
- "preferred_locales": [],
- "shipping": null,
- "tax_exempt": "none",
- "test_clock": null
- }
- Payment Intent created: {
- "amount": 100,
- "amount_capturable": 0,
- "amount_details": {
- "tip": {}
- },
- "amount_received": 0,
- "application": null,
- "application_fee_amount": null,
- "automatic_payment_methods": null,
- "canceled_at": null,
- "cancellation_reason": null,
- "capture_method": "automatic_async",
- "client_secret": "pi_3QfmiVJa90wenWb80jSbeBKp_secret_VUjKtak2E8m1ia5xASJjtSCR9",
- "confirmation_method": "automatic",
- "created": 1736532835,
- "currency": "bgn",
- "customer": "cus_RYuXWZgWR8FKq1",
- "description": null,
- "id": "pi_3QfmiVJa90wenWb80jSbeBKp",
- "invoice": null,
- "last_payment_error": null,
- "latest_charge": null,
- "livemode": false,
- "metadata": {},
- "next_action": null,
- "object": "payment_intent",
- "on_behalf_of": null,
- "payment_method": null,
- "payment_method_configuration_details": null,
- "payment_method_options": {
- "card": {
- "installments": null,
- "mandate_options": null,
- "network": null,
- "request_three_d_secure": "automatic"
- }
- },
- "payment_method_types": [
- "card"
- ],
- "processing": null,
- "receipt_email": null,
- "review": null,
- "setup_future_usage": null,
- "shipping": null,
- "source": null,
- "statement_descriptor": null,
- "statement_descriptor_suffix": null,
- "status": "requires_payment_method",
- "transfer_data": null,
- "transfer_group": null
- }
- Simulate Payment:
- Use test card 4242 4242 4242 4242 with any valid expiration and CVV.
- Client secret for this transaction: pi_3QfmiVJa90wenWb80jSbeBKp_secret_VUjKtak2E8m1ia5xASJjtSCR9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement