Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. <?php
  2. /* Charging a Customer
  3.  
  4. For a customer, you can use the same HTML, CSS, and JS. You just have to change the PHP script. You have to create a Customer object then charge this customer object.
  5. */
  6.  
  7. \Stripe\Stripe::setApiKey("____YOUR_STRIPE_SECRET_KEY____");
  8.  
  9. // Get the token from the JS script
  10. $token = $_POST['stripeToken'];
  11.  
  12. // Create a Customer
  13. $customer = \Stripe\Customer::create(array(
  14. "email" => "paying.user@example.com",
  15. "source" => $token,
  16. ));
  17.  
  18. // Save the customer id in your own database!
  19.  
  20. // Charge the Customer instead of the card
  21. $charge = \Stripe\Charge::create(array(
  22. "amount" => 2000,
  23. "currency" => "usd",
  24. "customer" => $customer->id
  25. ));
  26.  
  27. // You can charge the customer later by using the customer id.
  28.  
  29.  
  30. /* Making a Subscription Charge
  31.  
  32. For the subscription, all you have to do is change the PHP script.
  33. */
  34.  
  35. \Stripe\Stripe::setApiKey("____YOUR_STRIPE_SECRET_KEY____");
  36.  
  37. // Get the token from the JS script
  38. $token = $_POST['stripeToken'];
  39.  
  40. // Create a Customer
  41. $customer = \Stripe\Customer::create(array(
  42. "email" => "paying.user@example.com",
  43. "source" => $token,
  44. ));
  45.  
  46. // or you can fetch customer id from the database too.
  47.  
  48. // Creates a subscription plan. This can also be done through the Stripe dashboard.
  49. // You only need to create the plan once.
  50. $subscription = \Stripe\Plan::create(array(
  51. "amount" => 2000,
  52. "interval" => "month",
  53. "name" => "Gold large",
  54. "currency" => "cad",
  55. "id" => "gold"
  56. ));
  57.  
  58. // Subscribe the customer to the plan
  59. $subscription = \Stripe\Subscription::create(array(
  60. "customer" => $customer->id,
  61. "plan" => "gold"
  62. ));
  63.  
  64.  
  65. print_r($subscription);
  66.  
  67. /*
  68. The response is:
  69.  
  70. {
  71. "id": "sub_Aax13APykInoyt",
  72. "object": "subscription",
  73. "application_fee_percent": null,
  74. "cancel_at_period_end": false,
  75. "canceled_at": null,
  76. "created": 1493908778,
  77. "current_period_end": 1496587178,
  78. "current_period_start": 1493908778,
  79. "customer": "cus_Aax1Fuxu2NMEsA",
  80. "discount": null,
  81. "ended_at": null,
  82. "items": {
  83. "object": "list",
  84. "data": [
  85. {
  86. "id": "si_1AFl7G285d61s2cIpGzKedMm",
  87. "object": "subscription_item",
  88. "created": 1493908779,
  89. "plan": {
  90. "id": "gold",
  91. "object": "plan",
  92. "amount": 2000,
  93. "created": 1394782612,
  94. "currency": "cad",
  95. "interval": "month",
  96. "interval_count": 1,
  97. "livemode": false,
  98. "metadata": {
  99.  
  100. },
  101. "name": "Gold Special",
  102. "statement_descriptor": null,
  103. "trial_period_days": null
  104. },
  105. "quantity": 1
  106. }
  107. ],
  108. "has_more": false,
  109. "total_count": 1,
  110. "url": "/v1/subscription_items?subscription=sub_Aax13APykInoyt"
  111. },
  112. "livemode": false,
  113. "metadata": {
  114.  
  115. },
  116. "plan": {
  117. "id": "gold",
  118. "object": "plan",
  119. "amount": 2000,
  120. "created": 1394782612,
  121. "currency": "cad",
  122. "interval": "month",
  123. "interval_count": 1,
  124. "livemode": false,
  125. "metadata": {
  126. },
  127. "name": "Gold Special",
  128. "statement_descriptor": null,
  129. "trial_period_days": null
  130. },
  131. "quantity": 1,
  132. "start": 1493908778,
  133. "status": "active",
  134. "tax_percent": null,
  135. "trial_end": null,
  136. "trial_start": null
  137. }
  138.  
  139. */
  140.  
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement