Stripe 是一个聚合支付平台,可以安全、快速地处理包括信用卡、Apple Pay、Google Pay、支付宝等市面上各种支付方式。
Stripe 的文档很齐全,如果不知道选择何种支付方式,或者找不到对应支付方式文档在哪,可以看看 Stripe 的建议。
支付方式
收款
首先安装依赖包:
1
| $ composer require stripe/stripe-php
|
Web
这种方式是通过在网站上添加一个结账按钮,调用 Stripe Api 创建 Checkout Session,将客户重定向到 Stripe Checkout。

这种方式适用于,线上(Web)收款,如果需要通过 App 进行收款,可以继续往下看 App 的收款方式。
服务端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public function createPayOrderByCheckout() { $host = $this->request->host();
$priceId = $this->request->post("price_id"); if (!$priceId) { $this->error(__('请填写 Price Id')); }
try { Stripe::setApiKey($this->stripeSecretKey); $checkoutSession = Session::create([ 'line_items' => [[ 'price' => $priceId, 'quantity' => 1, ]], 'mode' => 'payment', 'success_url' => $host . '/success.html', 'cancel_url' => $host . '/cancel.html', ]);
$this->success("订单创建成功", [ "url" => $checkoutSession->url, "id" => $checkoutSession->id ]); } catch (\Stripe\Exception\ApiErrorException $exception) { $this->error("订单创建失败:". $exception->getMessage()); } }
|
App
这种方式是 App 端确认好商品信息,点击下单按钮,服务端调用 Stripe Api 创建支付订单,并返回支付信息给客户端,拉起支付,用户完成支付,通过设置的 Webhook 进行回调通知。

服务端创建支付订单 Api:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public function createPayOrderByApp(array $orderInfo) { try { $stripe = new StripeClient($this->stripeSecretKey); $paymentIntent = $stripe->paymentIntents->create([ 'amount' => $orderInfo['amount'] * 100, 'currency' => $currency, 'metadata' => [ 'order_no' => $orderInfo['order_no'] ], ]);
$this->success("订单创建成功", [ 'client_secret' => $paymentIntent->client_secret, 'publishable_key' => $this->stripePublishableKey ]); } catch (ApiErrorException $exception) { $this->error("订单创建失败:". $exception->getMessage()); } }
|
服务端回调 Api:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public function webhook() { Stripe::setApiKey($this->stripePublishableKey);
$payload = @file_get_contents('php://input'); $event = null;
try { $event = Event::constructFrom( json_decode($payload, true) ); } catch(\UnexpectedValueException $e) { http_response_code(400); } catch(\Stripe\Exception\SignatureVerificationException $e) { http_response_code(400); }
switch ($event->type) { case 'payment_intent.succeeded': $paymentIntent = $event->data->object;
break; case 'payment_intent.canceled': break;
default: break; }
http_response_code(200); }
|
退款 Api:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public function refund($orderInfo) { Stripe::setApiKey($this->stripeSecretKey); $refund = \Stripe\Refund::create([ 'charge' => $orderInfo['payer_id'], "amount" => $orderInfo['payment_amount'] * 100 ]);
if ($refund->status === "succeeded") { return true; }
return $refund->failure_reason; }
|
参考链接