· 9 years ago · Aug 30, 2016, 03:40 PM
1<?php
2
3require_once('/donnees/www/Rest/vendor/autoload.php');
4
5class Stripe {
6
7
8 public function __construct() {
9 $this->stripe = array(
10 "secret_key" => "sk_test_qm2asYSbgB4qXLFMo0CMpNzV",
11 "publishable_key" => "pk_test_rH3ZBAm6FPntfXPHm4xy9oMU"
12 );
13
14 \Stripe\Stripe::setApiKey($this->stripe['secret_key']);
15 }
16
17 /**
18 * Returns a JSON string object to the browser when hitting the root of the domain
19 *
20 * @url GET /
21 */
22 public function index() {
23 return get_class() . " : It's work Api !";
24 }
25
26 /**
27 * Returns a JSON string object
28 *
29 * @return object
30 * @url GET /chargeCustomer
31 */
32 public function chargeCustomer() {
33 $token = $_GET['stripeToken'];
34 self::log(__METHOD__ . ' Stripe Token : ' . $token);
35 try {
36
37 $customer = \Stripe\Customer::create(array(
38 'email' => $_GET['email'],
39 'source' => $token
40 ));
41
42 } catch (\Stripe\Error\RateLimit $e) {
43 // Too many requests made to the API too quickly
44 $body = $e->getJsonBody();
45 $err = $body['error'];
46
47 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
48 self::log(__METHOD__ . ' Type : ' . $err['type']);
49 self::log(__METHOD__ . ' Code : ' . $err['code']);
50 self::log(__METHOD__ . ' Param : ' . $err['param']);
51 self::log(__METHOD__ . ' Message : ' . $err['message']);
52 return false;
53 } catch (\Stripe\Error\InvalidRequest $e) {
54 // Invalid parameters were supplied to Stripe's API
55 $body = $e->getJsonBody();
56 $err = $body['error'];
57
58 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
59 self::log(__METHOD__ . ' Type : ' . $err['type']);
60 self::log(__METHOD__ . ' Code : ' . $err['code']);
61 self::log(__METHOD__ . ' Param : ' . $err['param']);
62 self::log(__METHOD__ . ' Message : ' . $err['message']);
63 return false;
64 } catch (\Stripe\Error\Authentication $e) {
65 // Authentication with Stripe's API failed
66 // (maybe you changed API keys recently)
67 $body = $e->getJsonBody();
68 $err = $body['error'];
69
70 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
71 self::log(__METHOD__ . ' Type : ' . $err['type']);
72 self::log(__METHOD__ . ' Code : ' . $err['code']);
73 self::log(__METHOD__ . ' Param : ' . $err['param']);
74 self::log(__METHOD__ . ' Message : ' . $err['message']);
75 return false;
76 } catch (\Stripe\Error\ApiConnection $e) {
77 // Network communication with Stripe failed
78 $body = $e->getJsonBody();
79 $err = $body['error'];
80
81 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
82 self::log(__METHOD__ . ' Type : ' . $err['type']);
83 self::log(__METHOD__ . ' Code : ' . $err['code']);
84 self::log(__METHOD__ . ' Param : ' . $err['param']);
85 self::log(__METHOD__ . ' Message : ' . $err['message']);
86 return false;
87 } catch (\Stripe\Error\Base $e) {
88 // Display a very generic error to the user, and maybe send
89 // yourself an email
90 $body = $e->getJsonBody();
91 $err = $body['error'];
92
93 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
94 self::log(__METHOD__ . ' Type : ' . $err['type']);
95 self::log(__METHOD__ . ' Code : ' . $err['code']);
96 self::log(__METHOD__ . ' Param : ' . $err['param']);
97 self::log(__METHOD__ . ' Message : ' . $err['message']);
98 return false;
99 } catch (Exception $e) {
100 // Something else happened, completely unrelated to Stripe
101 $body = $e->getJsonBody();
102 $err = $body['error'];
103
104 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
105 self::log(__METHOD__ . ' Type : ' . $err['type']);
106 self::log(__METHOD__ . ' Code : ' . $err['code']);
107 self::log(__METHOD__ . ' Param : ' . $err['param']);
108 self::log(__METHOD__ . ' Message : ' . $err['message']);
109 return false;
110 }
111
112
113 self::log(__METHOD__ . ' Customer : ' . $_GET['email']);
114
115 try {
116 $charge = \Stripe\Charge::create(array(
117 'customer' => $customer->id,
118 'amount' => $_GET['amount'],
119 'currency' => $_GET['currency'],
120 'description' => $_GET['description'],
121 ));
122 self::log(__METHOD__ . ' Charge Amount: ' . $_GET['amount']);
123 self::log(__METHOD__ . ' Charge Currency: ' . $_GET['currency']);
124 self::log(__METHOD__ . ' Charge Description: ' . $_GET['description']);
125 return true;
126
127 } catch(\Stripe\Error\Card $e) {
128 // Since it's a decline, \Stripe\Error\Card will be caught
129 $body = $e->getJsonBody();
130 $err = $body['error'];
131
132 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
133 self::log(__METHOD__ . ' Type : ' . $err['type']);
134 self::log(__METHOD__ . ' Code : ' . $err['code']);
135 self::log(__METHOD__ . ' Param : ' . $err['param']);
136 self::log(__METHOD__ . ' Message : ' . $err['message']);
137 return false;
138 } catch (\Stripe\Error\RateLimit $e) {
139 // Too many requests made to the API too quickly
140 $body = $e->getJsonBody();
141 $err = $body['error'];
142
143 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
144 self::log(__METHOD__ . ' Type : ' . $err['type']);
145 self::log(__METHOD__ . ' Code : ' . $err['code']);
146 self::log(__METHOD__ . ' Param : ' . $err['param']);
147 self::log(__METHOD__ . ' Message : ' . $err['message']);
148 return false;
149 } catch (\Stripe\Error\InvalidRequest $e) {
150 // Invalid parameters were supplied to Stripe's API
151 $body = $e->getJsonBody();
152 $err = $body['error'];
153
154 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
155 self::log(__METHOD__ . ' Type : ' . $err['type']);
156 self::log(__METHOD__ . ' Code : ' . $err['code']);
157 self::log(__METHOD__ . ' Param : ' . $err['param']);
158 self::log(__METHOD__ . ' Message : ' . $err['message']);
159 return false;
160 } catch (\Stripe\Error\Authentication $e) {
161 // Authentication with Stripe's API failed
162 // (maybe you changed API keys recently)
163 $body = $e->getJsonBody();
164 $err = $body['error'];
165
166 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
167 self::log(__METHOD__ . ' Type : ' . $err['type']);
168 self::log(__METHOD__ . ' Code : ' . $err['code']);
169 self::log(__METHOD__ . ' Param : ' . $err['param']);
170 self::log(__METHOD__ . ' Message : ' . $err['message']);
171 return false;
172 } catch (\Stripe\Error\ApiConnection $e) {
173 // Network communication with Stripe failed
174 $body = $e->getJsonBody();
175 $err = $body['error'];
176
177 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
178 self::log(__METHOD__ . ' Type : ' . $err['type']);
179 self::log(__METHOD__ . ' Code : ' . $err['code']);
180 self::log(__METHOD__ . ' Param : ' . $err['param']);
181 self::log(__METHOD__ . ' Message : ' . $err['message']);
182 return false;
183 } catch (\Stripe\Error\Base $e) {
184 // Display a very generic error to the user, and maybe send
185 // yourself an email
186 $body = $e->getJsonBody();
187 $err = $body['error'];
188
189 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
190 self::log(__METHOD__ . ' Type : ' . $err['type']);
191 self::log(__METHOD__ . ' Code : ' . $err['code']);
192 self::log(__METHOD__ . ' Param : ' . $err['param']);
193 self::log(__METHOD__ . ' Message : ' . $err['message']);
194 return false;
195 } catch (Exception $e) {
196 // Something else happened, completely unrelated to Stripe
197 $body = $e->getJsonBody();
198 $err = $body['error'];
199
200 self::log(__METHOD__ . ' Status : ' . $e->getHttpStatus());
201 self::log(__METHOD__ . ' Type : ' . $err['type']);
202 self::log(__METHOD__ . ' Code : ' . $err['code']);
203 self::log(__METHOD__ . ' Param : ' . $err['param']);
204 self::log(__METHOD__ . ' Message : ' . $err['message']);
205 return false;
206 }
207
208
209
210
211
212 }
213
214 static function log($str) {
215 RH::log($str,__CLASS__);
216 }
217
218}
219
220?>