· 9 years ago · Dec 01, 2016, 05:46 AM
1<?php
2// require with composer install
3require_once('../vendor/autoload.php');
4
5$stripe = array(
6 "secret_key" => "sk_test_ your secret_key",
7 "publishable_key" => "pk_test_ your publishable_key"
8);
9
10StripeStripe::setApiKey($stripe['secret_key']);
11?>
12
13<?php require_once('../config.php');?>
14<!-- Stripe Panel, This script only work remotely -->
15<script src="https://checkout.stripe.com/checkout.js"></script>
16<!-- JQuery auto update CDN -->
17<script src="http://code.jquery.com/jquery-latest.min.js"></script>
18<!-- /////////////////////////////// ONE SHOT PAYMENT ///////////////////////////////// -->
19<h2>One Donation</h2>
20Please add any value below
21<!-- Form and Charge Validation -->
22<form id="payment-form" action="charge1" method="POST">
23 <input type="number" id="amount" name="amount" value="3000" placeholder="3,000 Minimum" min="3000" step="500.00" />
24 <input type="hidden" id="stripeToken" name="stripeToken"/>
25 <input type="hidden" id="stripeEmail" name="stripeEmail"/>
26</form>
27<input type="button" id="btn" value="Donate" class="strbtn">
28
29<script type="text/javascript">
30// JS Script Handle amount value and other Stripe options.
31var handler = StripeCheckout.configure({
32 key: '< ?php echo $stripe['publishable_key']; ?>', // !! Possible to hide from HTML source view ??
33 image: '../logo.jpg',
34 token: function(token) {
35 $("#stripeToken").val(token.id);
36 $("#stripeEmail").val(token.email);
37 $("#payment-form").submit();
38 }
39 });
40
41 $('#btn').on('click', function(e) {
42 var amount = $("#amount").val() *1; // !! normaly it's *1000 but with YEN need to apply this ??
43// Open Checkout with further options
44 handler.open({
45// OPTIONAL, UNCHECK THE // TO ACTIVATE IT:
46 //bitcoin: 'true',
47 //alipay: 'true',
48 billingAddress: 'true',
49 zipcode: 'true',
50 allowRememberMe: 'true',
51 //stripeEmail: 'true', // !! Stripe in test mode doesn't send email confirmation, there is any way to check if it will works ??
52 name: 'company name',
53 description: 'company description',
54 locale: 'auto', // !! on reults it show i'm from USA but i'm in Japan, is it based on navigator ? There is a way to be more accurate ??
55 panelLabel: 'DONATE',
56 currency: 'jpy',
57 amount: amount
58 });
59 e.preventDefault();
60 });
61
62 // Close Checkout on page navigation
63 $(window).on('popstate', function() {
64 handler.close();
65 });
66
67//to prevent zero amount in the form, add 3000 when focus out of the field
68$(document).ready(function(){
69 $("body").delegate('#amount', 'focusout', function(){ // !! There is a better way to make the minimum amount 3000Yen ??
70 if($(this).val() < 3000){
71 $(this).val('3000');
72 }
73 });
74});
75</script>
76
77
78
79<!-- /////////////////////////////// ANNUAL PAYMENT SUBSCIPTION ///////////////////////////////// -->
80<h2>Annual Recurring Donations</h2>
81Please add any value below
82<!-- Form and Charge Validation -->
83<form id="payment-form2" action="charge2" method="POST">
84 <input type="number" id="amount2" name="amount" value="3000" placeholder="3,000 Minimum" min="3000" step="500.00" />
85 <input type="hidden" id="stripeToken2" name="stripeToken"/>
86 <input type="hidden" id="stripeEmail2" name="stripeEmail"/>
87 <input type="hidden" id="idplan" name="idplan"/>
88</form>
89<input type="button" id="btn2" value="Subscription" class="strbtn">
90
91<script type="text/javascript">
92// JS Script Handle amount value and other Stripe options.
93var handler = StripeCheckout.configure({
94 key: '<?php echo $stripe['publishable_key']; ?>',
95 image: '../str-gps-logo.jpg',
96 token: function(token) {
97 $("#stripeToken2").val(token.id);
98 $("#stripeEmail2").val(token.email);
99 $("#payment-form2").submit();
100 }
101 });
102
103
104 $('#btn2').on('click', function(e) {
105 var amount = $("#amount2").val() *1;
106 var plan = $("#idplan").val(Date); // !! Generate Date ID for the Plan to be a unique id value, possible to add milisecond too ??
107
108// Open Checkout with further options
109 handler.open({
110 billingAddress: 'true',
111 zipcode: 'true', // !! is it like this or zip-code: ??
112 name: 'Year Plan',
113 description: 'Variable Amount Year Plan',
114 locale: 'auto',
115 panelLabel: 'Subscribe',
116 currency: 'jpy',
117 amount: amount
118 });
119 e.preventDefault();
120 });
121
122 // Close Checkout on page navigation
123 $(window).on('popstate', function() {
124 handler.close();
125 });
126
127//to prevent zero amount in the form, add 3000 when focus out of the field
128$(document).ready(function(){
129 $("body").delegate('#amount2', 'focusout', function(){
130 if($(this).val() < 3000){
131 $(this).val('3000');
132 }
133 });
134});
135
136</script>
137
138<?php
139 require_once('./config.php');
140
141// Check if the user have javascript and if the token is validated.
142// !! code below needed or more simple with $token = $_POST['stripeToken']; ??
143
144if ($_SERVER['REQUEST_METHOD'] == 'POST') {
145 $errors = array();
146 if (isset($_POST['stripeToken'])) {
147 $token = $_POST['stripeToken'];
148 } else {
149 $errors['token'] = 'The order cannot be processed. You have not been charged.
150 Please confirm that you have JavaScript enabled and try again.';
151 }
152}
153
154// Create the Customer:
155 $customer = StripeCustomer::create(array(
156 'email' => $_POST['stripeEmail'],
157 'source' => $token
158 ));
159
160// Create the Charge:
161 $charge = StripeCharge::create(array(
162 'customer' => $customer->id,
163 'amount' => $_POST['amount'],
164 'currency' => 'jpy'
165 ));
166
167 echo '<h1>Thanks for your donation ! </h1>'; // !! There is a way to show error to user and redirect to index ?
168
169?>
170
171<?php
172 require_once('./config.php');
173
174// Check if the user have javascript and if the token is validated.
175//$token = $_POST['stripeToken'];
176if ($_SERVER['REQUEST_METHOD'] == 'POST') {
177 $errors = array();
178 if (isset($_POST['stripeToken'])) {
179 $token = $_POST['stripeToken'];
180 } else {
181 $errors['token'] = 'The order cannot be processed. You have not been charged.
182 Please confirm that you have JavaScript enabled and try again.';
183 }
184}
185
186// TEST 1 inspired from http://stackoverflow.com/questions/36075869/how-to-create-variable-subscriptions-in-stripe
187
188// !! Find the way to generate unique id to create the plan or any other solution for a variable amount recurent donation plan subscription
189
190$plan = StripePlan::create(array(
191 'name' => $_POST['idplan'],
192 'id' => $_POST['idplan'],
193 'interval' => 'day', // !! interval daily for testing purpose but the final will be in years
194 'interval_count' => '1',
195 'currency' => 'jpy',
196 'amount' => $_POST['amount']
197 ));
198
199$customer = StripeCustomer::create(array(
200 'source' => $token,
201 'email' => $_POST['stripeEmail'],
202 'plan' => $_POST['idplan'],
203 'description' => 'plan description'
204 ));
205
206echo '<h1>Thanks for your annual donation ! </h1>';
207?>