|
|
|
1
|
+<?php
|
|
|
|
2
|
+/**
|
|
|
|
3
|
+ * @remark :
|
|
|
|
4
|
+ * @name :PayStripeApi.php
|
|
|
|
5
|
+ * @author :lyh
|
|
|
|
6
|
+ * @method :post
|
|
|
|
7
|
+ * @time :2024/12/24 10:35
|
|
|
|
8
|
+ */
|
|
|
|
9
|
+
|
|
|
|
10
|
+namespace App\Helper;
|
|
|
|
11
|
+
|
|
|
|
12
|
+class PayStripeApi
|
|
|
|
13
|
+{
|
|
|
|
14
|
+ private $secretKey;
|
|
|
|
15
|
+ //币种对应支付方式
|
|
|
|
16
|
+ public $currency_types = [
|
|
|
|
17
|
+ 'usd' => ['card', 'alipay', 'wechat_pay', 'cashapp', 'link', 'afterpay_clearpay'],
|
|
|
|
18
|
+ 'eur' => ['card', 'ideal', 'giropay', 'sofort', 'bancontact', 'klarna', 'link'],
|
|
|
|
19
|
+ 'gbp' => ['card', 'apple_pay', 'google_pay', 'klarna', 'link', 'afterpay_clearpay'],
|
|
|
|
20
|
+ 'aud' => ['card', 'afterpay_clearpay', 'apple_pay', 'google_pay'],
|
|
|
|
21
|
+ 'cad' => ['card', 'apple_pay', 'google_pay', 'link'],
|
|
|
|
22
|
+ 'sgd' => ['card', 'grabpay', 'fpx', 'wechat_pay', 'apple_pay', 'google_pay'],
|
|
|
|
23
|
+ 'jpy' => ['card', 'apple_pay', 'google_pay'],
|
|
|
|
24
|
+ 'cny' => ['alipay', 'wechat_pay'],
|
|
|
|
25
|
+ 'brl' => ['card', 'boleto', 'pix'],
|
|
|
|
26
|
+ 'mxn' => ['card', 'oxxo'],
|
|
|
|
27
|
+ 'inr' => ['card', 'upi', 'netbanking'],
|
|
|
|
28
|
+ 'php' => ['card', 'paymaya', 'gcash'],
|
|
|
|
29
|
+ 'myr' => ['card', 'fpx'],
|
|
|
|
30
|
+ 'thb' => ['card', 'promptpay'],
|
|
|
|
31
|
+ 'idr' => ['card', 'bank_transfer'],
|
|
|
|
32
|
+ 'zar' => ['card'],
|
|
|
|
33
|
+ 'ngn' => ['card'],
|
|
|
|
34
|
+ 'aed' => ['card', 'apple_pay', 'google_pay']
|
|
|
|
35
|
+ ];
|
|
|
|
36
|
+
|
|
|
|
37
|
+ // 构造函数设置密钥
|
|
|
|
38
|
+ public function __construct($secretKey)
|
|
|
|
39
|
+ {
|
|
|
|
40
|
+ $this->secretKey = 'sk_test_51MyseXIWCYVeLww1tbPZzRe1Qk4lS5d2VLiDjpju7G0ToiX1RJcFinQXNlftq9eCjZE0n2gjaz1mfy1g0mxTusdf00m636Gv62';
|
|
|
|
41
|
+ }
|
|
|
|
42
|
+
|
|
|
|
43
|
+ /**
|
|
|
|
44
|
+ * @remark :通用的 cURL 请求方法
|
|
|
|
45
|
+ * @name :sendRequest
|
|
|
|
46
|
+ * @author :lyh
|
|
|
|
47
|
+ * @method :post
|
|
|
|
48
|
+ * @time :2024/12/24 10:38
|
|
|
|
49
|
+ */
|
|
|
|
50
|
+ private function sendRequest($url, $method = 'POST', $data = [])
|
|
|
|
51
|
+ {
|
|
|
|
52
|
+ $ch = curl_init();
|
|
|
|
53
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
54
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
55
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
|
|
56
|
+ "Authorization: Bearer {$this->secretKey}",
|
|
|
|
57
|
+ "Content-Type: application/x-www-form-urlencoded"
|
|
|
|
58
|
+ ]);
|
|
|
|
59
|
+ if ($method === 'POST') {
|
|
|
|
60
|
+ curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
61
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
|
|
62
|
+ } elseif ($method === 'GET') {
|
|
|
|
63
|
+ curl_setopt($ch, CURLOPT_HTTPGET, true);
|
|
|
|
64
|
+ } elseif ($method === 'DELETE') {
|
|
|
|
65
|
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
|
|
|
66
|
+ }
|
|
|
|
67
|
+ $response = curl_exec($ch);
|
|
|
|
68
|
+ if (curl_errno($ch)) {
|
|
|
|
69
|
+ throw new Exception('cURL Error: ' . curl_error($ch));
|
|
|
|
70
|
+ }
|
|
|
|
71
|
+ curl_close($ch);
|
|
|
|
72
|
+ return json_decode($response, true);
|
|
|
|
73
|
+ }
|
|
|
|
74
|
+
|
|
|
|
75
|
+ /**
|
|
|
|
76
|
+ * @remark :创建支付意图
|
|
|
|
77
|
+ * @name :createPaymentIntent
|
|
|
|
78
|
+ * @author :lyh
|
|
|
|
79
|
+ * @method :post
|
|
|
|
80
|
+ * @time :2024/12/24 10:38
|
|
|
|
81
|
+ */
|
|
|
|
82
|
+ public function createPaymentIntent($amount, $currency = 'usd')
|
|
|
|
83
|
+ {
|
|
|
|
84
|
+ $url = "https://api.stripe.com/v1/payment_intents";
|
|
|
|
85
|
+ $data = [
|
|
|
|
86
|
+ 'amount' => $amount,
|
|
|
|
87
|
+ 'currency' => $currency,
|
|
|
|
88
|
+ 'payment_method_types[]' => $this->currency_types[$currency],
|
|
|
|
89
|
+ ];
|
|
|
|
90
|
+ return $this->sendRequest($url, 'POST', $data);
|
|
|
|
91
|
+ }
|
|
|
|
92
|
+
|
|
|
|
93
|
+ /**
|
|
|
|
94
|
+ * @remark :查询支付意图
|
|
|
|
95
|
+ * @name :retrievePaymentIntent
|
|
|
|
96
|
+ * @author :lyh
|
|
|
|
97
|
+ * @method :post
|
|
|
|
98
|
+ * @time :2024/12/24 10:38
|
|
|
|
99
|
+ */
|
|
|
|
100
|
+ public function retrievePaymentIntent($paymentIntentId)
|
|
|
|
101
|
+ {
|
|
|
|
102
|
+ $url = "https://api.stripe.com/v1/payment_intents/{$paymentIntentId}";
|
|
|
|
103
|
+ return $this->sendRequest($url, 'GET');
|
|
|
|
104
|
+ }
|
|
|
|
105
|
+
|
|
|
|
106
|
+ /**
|
|
|
|
107
|
+ * @remark :创建客户
|
|
|
|
108
|
+ * @name :createCustomer
|
|
|
|
109
|
+ * @author :lyh
|
|
|
|
110
|
+ * @method :post
|
|
|
|
111
|
+ * @time :2024/12/24 10:41
|
|
|
|
112
|
+ */
|
|
|
|
113
|
+ public function createCustomer($name, $email, $description = '')
|
|
|
|
114
|
+ {
|
|
|
|
115
|
+ $url = "https://api.stripe.com/v1/customers";
|
|
|
|
116
|
+ $data = [
|
|
|
|
117
|
+ 'name' => $name,
|
|
|
|
118
|
+ 'email' => $email,
|
|
|
|
119
|
+ 'description' => $description
|
|
|
|
120
|
+ ];
|
|
|
|
121
|
+ return $this->sendRequest($url, 'POST', $data);
|
|
|
|
122
|
+ }
|
|
|
|
123
|
+
|
|
|
|
124
|
+ /**
|
|
|
|
125
|
+ * @remark :查询客户
|
|
|
|
126
|
+ * @name :retrieveCustomer
|
|
|
|
127
|
+ * @author :lyh
|
|
|
|
128
|
+ * @method :post
|
|
|
|
129
|
+ * @time :2024/12/24 10:41
|
|
|
|
130
|
+ */
|
|
|
|
131
|
+ public function retrieveCustomer($customerId)
|
|
|
|
132
|
+ {
|
|
|
|
133
|
+ $url = "https://api.stripe.com/v1/customers/{$customerId}";
|
|
|
|
134
|
+ return $this->sendRequest($url, 'GET');
|
|
|
|
135
|
+ }
|
|
|
|
136
|
+
|
|
|
|
137
|
+ /**
|
|
|
|
138
|
+ * @remark :删除客户
|
|
|
|
139
|
+ * @name :deleteCustomer
|
|
|
|
140
|
+ * @author :lyh
|
|
|
|
141
|
+ * @method :post
|
|
|
|
142
|
+ * @time :2024/12/24 10:42
|
|
|
|
143
|
+ */
|
|
|
|
144
|
+ public function deleteCustomer($customerId)
|
|
|
|
145
|
+ {
|
|
|
|
146
|
+ $url = "https://api.stripe.com/v1/customers/{$customerId}";
|
|
|
|
147
|
+ return $this->sendRequest($url, 'DELETE');
|
|
|
|
148
|
+ }
|
|
|
|
149
|
+
|
|
|
|
150
|
+ /**
|
|
|
|
151
|
+ * @remark :创建支付方法
|
|
|
|
152
|
+ * @name :createPaymentMethod
|
|
|
|
153
|
+ * @author :lyh
|
|
|
|
154
|
+ * @method :post
|
|
|
|
155
|
+ * @time :2024/12/24 10:42
|
|
|
|
156
|
+ */
|
|
|
|
157
|
+ public function createPaymentMethod($cardNumber, $expMonth, $expYear, $cvc)
|
|
|
|
158
|
+ {
|
|
|
|
159
|
+ $url = "https://api.stripe.com/v1/payment_methods";
|
|
|
|
160
|
+ $data = [
|
|
|
|
161
|
+ 'type' => 'card',
|
|
|
|
162
|
+ 'card[number]' => $cardNumber,
|
|
|
|
163
|
+ 'card[exp_month]' => $expMonth,
|
|
|
|
164
|
+ 'card[exp_year]' => $expYear,
|
|
|
|
165
|
+ 'card[cvc]' => $cvc,
|
|
|
|
166
|
+ ];
|
|
|
|
167
|
+
|
|
|
|
168
|
+ return $this->sendRequest($url, 'POST', $data);
|
|
|
|
169
|
+ }
|
|
|
|
170
|
+
|
|
|
|
171
|
+ /**
|
|
|
|
172
|
+ * @remark :绑定支付方法到客户
|
|
|
|
173
|
+ * @name :attachPaymentMethodToCustomer
|
|
|
|
174
|
+ * @author :lyh
|
|
|
|
175
|
+ * @method :post
|
|
|
|
176
|
+ * @time :2024/12/24 10:42
|
|
|
|
177
|
+ */
|
|
|
|
178
|
+ public function attachPaymentMethodToCustomer($paymentMethodId, $customerId)
|
|
|
|
179
|
+ {
|
|
|
|
180
|
+ $url = "https://api.stripe.com/v1/payment_methods/{$paymentMethodId}/attach";
|
|
|
|
181
|
+ $data = [
|
|
|
|
182
|
+ 'customer' => $customerId,
|
|
|
|
183
|
+ ];
|
|
|
|
184
|
+ return $this->sendRequest($url, 'POST', $data);
|
|
|
|
185
|
+ }
|
|
|
|
186
|
+
|
|
|
|
187
|
+ /**
|
|
|
|
188
|
+ * @remark :创建退款
|
|
|
|
189
|
+ * @name :createRefund
|
|
|
|
190
|
+ * @author :lyh
|
|
|
|
191
|
+ * @method :post
|
|
|
|
192
|
+ * @time :2024/12/24 10:42
|
|
|
|
193
|
+ */
|
|
|
|
194
|
+ public function createRefund($chargeId, $amount = null)
|
|
|
|
195
|
+ {
|
|
|
|
196
|
+ $url = "https://api.stripe.com/v1/refunds";
|
|
|
|
197
|
+ $data = ['charge' => $chargeId];
|
|
|
|
198
|
+ if ($amount) {
|
|
|
|
199
|
+ $data['amount'] = $amount;
|
|
|
|
200
|
+ }
|
|
|
|
201
|
+
|
|
|
|
202
|
+ return $this->sendRequest($url, 'POST', $data);
|
|
|
|
203
|
+ }
|
|
|
|
204
|
+
|
|
|
|
205
|
+ /**
|
|
|
|
206
|
+ * @remark :查询退款
|
|
|
|
207
|
+ * @name :retrieveRefund
|
|
|
|
208
|
+ * @author :lyh
|
|
|
|
209
|
+ * @method :post
|
|
|
|
210
|
+ * @time :2024/12/24 10:42
|
|
|
|
211
|
+ */
|
|
|
|
212
|
+ public function retrieveRefund($refundId)
|
|
|
|
213
|
+ {
|
|
|
|
214
|
+ $url = "https://api.stripe.com/v1/refunds/{$refundId}";
|
|
|
|
215
|
+ return $this->sendRequest($url, 'GET');
|
|
|
|
216
|
+ }
|
|
|
|
217
|
+
|
|
|
|
218
|
+ /**
|
|
|
|
219
|
+ * @remark :创建订阅
|
|
|
|
220
|
+ * @name :createSubscription
|
|
|
|
221
|
+ * @author :lyh
|
|
|
|
222
|
+ * @method :post
|
|
|
|
223
|
+ * @time :2024/12/24 10:42
|
|
|
|
224
|
+ */
|
|
|
|
225
|
+ public function createSubscription($customerId, $priceId)
|
|
|
|
226
|
+ {
|
|
|
|
227
|
+ $url = "https://api.stripe.com/v1/subscriptions";
|
|
|
|
228
|
+ $data = [
|
|
|
|
229
|
+ 'customer' => $customerId,
|
|
|
|
230
|
+ 'items[0][price]' => $priceId,
|
|
|
|
231
|
+ ];
|
|
|
|
232
|
+ return $this->sendRequest($url, 'POST', $data);
|
|
|
|
233
|
+ }
|
|
|
|
234
|
+
|
|
|
|
235
|
+ /**
|
|
|
|
236
|
+ * @remark :取消订阅
|
|
|
|
237
|
+ * @name :cancelSubscription
|
|
|
|
238
|
+ * @author :lyh
|
|
|
|
239
|
+ * @method :post
|
|
|
|
240
|
+ * @time :2024/12/24 10:43
|
|
|
|
241
|
+ */
|
|
|
|
242
|
+ public function cancelSubscription($subscriptionId)
|
|
|
|
243
|
+ {
|
|
|
|
244
|
+ $url = "https://api.stripe.com/v1/subscriptions/{$subscriptionId}";
|
|
|
|
245
|
+ return $this->sendRequest($url, 'DELETE');
|
|
|
|
246
|
+ }
|
|
|
|
247
|
+
|
|
|
|
248
|
+ /**
|
|
|
|
249
|
+ * @remark :处理 Webhook
|
|
|
|
250
|
+ * @name :handleWebhook
|
|
|
|
251
|
+ * @author :lyh
|
|
|
|
252
|
+ * @method :post
|
|
|
|
253
|
+ * @time :2024/12/24 10:43
|
|
|
|
254
|
+ */
|
|
|
|
255
|
+ public static function handleWebhook($payload, $sigHeader, $endpointSecret)
|
|
|
|
256
|
+ {
|
|
|
|
257
|
+ try {
|
|
|
|
258
|
+ $event = json_decode($payload, true);
|
|
|
|
259
|
+ // 检查事件类型
|
|
|
|
260
|
+ return $event; // 返回解析后的事件
|
|
|
|
261
|
+ } catch (Exception $e) {
|
|
|
|
262
|
+ throw new Exception('Webhook Error: ' . $e->getMessage());
|
|
|
|
263
|
+ }
|
|
|
|
264
|
+ }
|
|
|
|
265
|
+} |