接入Stripe支付
快速开始 https://docs.stripe.com/payments/quickstart
使用第一个示例
1、示例中的PRICE_ID需要去Stripe控制台->产品目录创建产品
1、 添加产品
2、点击查看创建的产品详情
4、这个API ID就是demo中的PRICE_ID
注意:需要注意的是,测试模式和生产模式中的 $stripeSecretKey 需要对应上。简而言之就是不能生产模式的产品API ID 对应测试模式的密钥
5、示例中的密钥从,Stripe控制台->开发人员->API密钥->密钥 获取
2、下载示例:
1、打开项目查看README.md
2、替换public下checkout.php中的配置
1 $checkout_session = \Stripe\Checkout\Session::create([ 2 'line_items' => [[ 3 # Provide the exact Price ID (e.g. pr_1234) of the product you want to sell 4 'price' => '对应产品目录的API ID', 5 'quantity' => 10, // 数量默认为1,我这边产品单价是1, 6 ]], 7 'mode' => 'payment', // 结账模式 付款 8 'success_url' => $YOUR_DOMAIN . '/success.html', // 支付成功跳转的页面 9 'cancel_url' => $YOUR_DOMAIN . '/cancel.html', // 取消支付跳转的页面 10 'automatic_tax' => [ 11 'enabled' => true, 12 ], 13 ]);
3、如果要传递自己的自定义参数metadata传递
1 $checkout_session = \Stripe\Checkout\Session::create([ 2 'line_items' => [[ 3 # Provide the exact Price ID (e.g. pr_1234) of the product you want to sell 4 'price' => '对应产品目录的API ID', 5 'quantity' => 10, // 数量默认为1,我这边产品单价是1, 6 ]], 7 'mode' => 'payment', // 结账模式 付款 8 'success_url' => $YOUR_DOMAIN . '/success.html', // 支付成功跳转的页面 9 'cancel_url' => $YOUR_DOMAIN . '/cancel.html', // 取消支付跳转的页面 10 'automatic_tax' => [ 11 'enabled' => true, 12 ], 13 // 元数据 14 'metadata' => [ 15 'order_id' => 'you order id', 16 'product_name' => 'prodct name' 17 ] 18 ]);
5、浏览器访问 http://127.0.0.1:4242/checkout.html
6、$20是页面展示的静态金额这边不做详解
7、拉取支付,测试模式下,可以用Stripe提供的测试卡号
8、支付成功之后,需要Stripe设置WebHook端点来监听事件,
3、Stripe->开发人员->WebHook
1、创建成功之后,点击进去获取密钥签名(用来验证接收的事件的合法性)
1 // 回调端点密钥,从webhook获取 2 $endpointSecret = '端点密钥'; 3 // 获取参数 4 $payload = @file_get_contents('php://input'); 5 $sigHeader = $_SERVER['HTTP_STRIPE_SIGNATURE']; 6 // 签名验证 7 try { 8 $event = Webhook::constructEvent( 9 $payload, 10 $sigHeader, 11 $endpointSecret 12 ); 13 } catch (SignatureVerificationException $e) { 14 // 签名验证失败 15 log_message('签名验证失败', 'log', 'pay'); 16 http_response_code(400); 17 exit(); 18 } 19 // 订单处理 20 if ($event->type != 'checkout.session.completed') { 21 log_message('返回的type是:' . $event->type, 'log', 'pay'); 22 http_response_code(400); 23 exit(); 24 } 25 26 // todo 此处处理成功之后的业务逻辑 27 28 // 处理成功返回200状态码 29 http_response_code(200); 30
3、WebHook 端点返回数据示例:
事件数据中,返回的金额是x100的如果要对订单做比对,记得除于100
{ "id": "evt_你看不见", "object": "event", "api_version": "2023-10-16", "created": 1709170566, "data": { "object": { "id": "cs_test_你看不见", "object": "checkout.session", "after_expiration": null, "allow_promotion_codes": null, "amount_subtotal": 1000, "amount_total": 1000, "automatic_tax": { "enabled": true, "liability": { "type": "self" }, "status": "complete" }, "billing_address_collection": null, "cancel_url": "http://localhost:4242/cancel.html", "client_reference_id": null, "client_secret": null, "consent": null, "consent_collection": null, "created": 1709170549, "currency": "cny", "currency_conversion": null, "custom_fields": [ ], "custom_text": { "after_submit": null, "shipping_address": null, "submit": null, "terms_of_service_acceptance": null }, "customer": null, "customer_creation": "if_required", "customer_details": { "address": { "city": null, "country": "CN", "line1": null, "line2": null, "postal_code": null, "state": null }, "email": "你看不见", "name": "你看不见", "phone": null, "tax_exempt": "none", "tax_ids": [ ] }, "customer_email": null, "expires_at": 1709256949, "invoice": null, "invoice_creation": { "enabled": false, "invoice_data": { "account_tax_ids": null, "custom_fields": null, "description": null, "footer": null, "issuer": null, "metadata": { }, "rendering_options": null } }, "livemode": false, "locale": null, "metadata": { "order_id": "you order id", "product_name": "prodct name" }, "mode": "payment", "payment_intent": "pi_你看不见", "payment_link": null, "payment_method_collection": "if_required", "payment_method_configuration_details": { "id": "pmc_你看不见", "parent": null }, "payment_method_options": { }, "payment_method_types": [ "card", "alipay", "wechat_pay", "link" ], "payment_status": "paid", "phone_number_collection": { "enabled": false }, "recovered_from": null, "setup_intent": null, "shipping_address_collection": null, "shipping_cost": null, "shipping_details": null, "shipping_options": [ ], "status": "complete", "submit_type": null, "subscription": null, "success_url": "http://localhost:4242/success.html", "total_details": { "amount_discount": 0, "amount_shipping": 0, "amount_tax": 0 }, "ui_mode": "hosted", "url": null } }, "livemode": false, "pending_webhooks": 1, "request": { "id": null, "idempotency_key": null }, "type": "checkout.session.completed" }
注意:如果使用Custom payment flow这个示例的话,webhook需要侦听的是 payment_intent.succeeded 事件端点,Custom payment flow示例可以直接定义,不需要创建产品API ID