Advertisement
GeorgiLukanov87

stripe test

Jan 10th, 2025 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. import stripe
  2.  
  3. from dotenv import load_dotenv
  4. import os
  5.  
  6. load_dotenv()
  7.  
  8. stripe.api_key = os.getenv('STRIPE_SECRET_KEY')
  9.  
  10.  
  11. # 1. Create a Customer
  12. def create_customer():
  13.     customer = stripe.Customer.create(
  14.         email="testuser@example.com",
  15.         name="10.01.25",
  16.         description="A test customer for Stripe",
  17.     )
  18.     print("Customer created:", customer)
  19.     return customer.id
  20.  
  21.  
  22. # 2. Create a Payment Intent # Valid card
  23. def create_payment_intent(customer_id):
  24.     payment_intent = stripe.PaymentIntent.create(
  25.         amount=100,  # Amount in the smallest currency unit (e.g., 1000 = $10.00)
  26.         currency="bgn",
  27.         customer=customer_id,
  28.         payment_method_types=["card"],  # Supported payment methods
  29.     )
  30.     print("Payment Intent created:", payment_intent)
  31.     return payment_intent.client_secret
  32.  
  33. # def create_payment_intent(customer_id):  # Invalid card
  34. #     try:
  35. #         payment_intent = stripe.PaymentIntent.create(
  36. #             amount=1000,  # Amount in stotinki (1000 = 10.00 лв)
  37. #             currency="bgn",
  38. #             customer=customer_id,
  39. #             payment_method_data={
  40. #                 "type": "card",
  41. #                 "card": {
  42. #                     "number": "4000000000009995",  # Card declined
  43. #                     "exp_month": 12,
  44. #                     "exp_year": 34,
  45. #                     "cvc": "123",
  46. #                 },
  47. #             },
  48. #             confirm=True,  # Simulate immediate confirmation
  49. #         )
  50. #         print("Payment Intent created:", payment_intent)
  51. #
  52. #     except stripe.error.CardError as e:
  53. #         print("Payment failed!")
  54. #         print("Error code:", e.error.code)
  55. #         print("Error message:", e.user_message)
  56. #         """
  57. #         raise err
  58. #         stripe._error.InvalidRequestError: Request req_mysHynuQ01gRUi:
  59. #         This PaymentIntent is configured to accept payment methods enabled in your Dashboard.
  60. #         because some of these payment methods might redirect your customer off of your page,
  61. #         you must provide a `return_url`. If you don't want to accept redirect-based payment
  62. #         methods, set `automatic_payment_methods[enabled]` to `true`
  63. #         and `automatic_payment_methods[allow_redirects]` to `never`
  64. #         when creating Setup Intents and Payment Intents.
  65. #         """
  66.  
  67.  
  68. # 3. Simulate Successful Payment
  69. def confirm_payment(payment_intent_client_secret):
  70.     print("\nSimulate Payment:")
  71.     print(f"Use test card 4242 4242 4242 4242 with any valid expiration and CVV.")
  72.     print(f"Client secret for this transaction: {payment_intent_client_secret}")
  73.  
  74.  
  75. if __name__ == "__main__":
  76.     print("Testing Stripe API...\n")
  77.  
  78.     # Step 1: Create a Customer
  79.     customer_id = create_customer()
  80.  
  81.     # Step 2: Create a Payment Intent for the Customer
  82.     client_secret = create_payment_intent(customer_id)
  83.  
  84.     # Step 3: Simulate the payment with test card info
  85.     confirm_payment(client_secret)
  86.  
  87. _____________________________________________________________________________________________________
  88. return:
  89. Testing Stripe API...
  90.  
  91. Customer created: {
  92.   "address": null,
  93.   "balance": 0,
  94.   "created": 1736532835,
  95.   "currency": null,
  96.   "default_source": null,
  97.   "delinquent": false,
  98.   "description": "A test customer for Stripe",
  99.   "discount": null,
  100.   "email": "testuser@example.com",
  101.   "id": "cus_RYuXWZgWR8FKq1",
  102.   "invoice_prefix": "2B206D93",
  103.   "invoice_settings": {
  104.     "custom_fields": null,
  105.     "default_payment_method": null,
  106.     "footer": null,
  107.     "rendering_options": null
  108.   },
  109.   "livemode": false,
  110.   "metadata": {},
  111.   "name": "10.01.25",
  112.   "object": "customer",
  113.   "phone": null,
  114.   "preferred_locales": [],
  115.   "shipping": null,
  116.   "tax_exempt": "none",
  117.   "test_clock": null
  118. }
  119. Payment Intent created: {
  120.   "amount": 100,
  121.   "amount_capturable": 0,
  122.   "amount_details": {
  123.     "tip": {}
  124.   },
  125.   "amount_received": 0,
  126.   "application": null,
  127.   "application_fee_amount": null,
  128.   "automatic_payment_methods": null,
  129.   "canceled_at": null,
  130.   "cancellation_reason": null,
  131.   "capture_method": "automatic_async",
  132.   "client_secret": "pi_3QfmiVJa90wenWb80jSbeBKp_secret_VUjKtak2E8m1ia5xASJjtSCR9",
  133.   "confirmation_method": "automatic",
  134.   "created": 1736532835,
  135.   "currency": "bgn",
  136.   "customer": "cus_RYuXWZgWR8FKq1",
  137.   "description": null,
  138.   "id": "pi_3QfmiVJa90wenWb80jSbeBKp",
  139.   "invoice": null,
  140.   "last_payment_error": null,
  141.   "latest_charge": null,
  142.   "livemode": false,
  143.   "metadata": {},
  144.   "next_action": null,
  145.   "object": "payment_intent",
  146.   "on_behalf_of": null,
  147.   "payment_method": null,
  148.   "payment_method_configuration_details": null,
  149.   "payment_method_options": {
  150.     "card": {
  151.       "installments": null,
  152.       "mandate_options": null,
  153.       "network": null,
  154.       "request_three_d_secure": "automatic"
  155.     }
  156.   },
  157.   "payment_method_types": [
  158.     "card"
  159.   ],
  160.   "processing": null,
  161.   "receipt_email": null,
  162.   "review": null,
  163.   "setup_future_usage": null,
  164.   "shipping": null,
  165.   "source": null,
  166.   "statement_descriptor": null,
  167.   "statement_descriptor_suffix": null,
  168.   "status": "requires_payment_method",
  169.   "transfer_data": null,
  170.   "transfer_group": null
  171. }
  172.  
  173. Simulate Payment:
  174. Use test card 4242 4242 4242 4242 with any valid expiration and CVV.
  175. Client secret for this transaction: pi_3QfmiVJa90wenWb80jSbeBKp_secret_VUjKtak2E8m1ia5xASJjtSCR9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement