- 参考文档 :
stripe文档
stripe/stripe-php
stripe api 文档
目录
- 一 获取关键参数
- 二 安装Stripe库
- 三 代码示例
一 获取关键参数
SCRIPE_SECRET_KEY (调用api秘钥)
NOTIFY_SIGN (签名 支付回调使用)
二 安装Stripe库
# Install the PHP library via Composer
composer require stripe/stripe-php
三 代码示例
// 支付请求主要代码$stripe = new StripeClient(env('SCRIPE_SECRET_KEY'));$checkoutSession = $stripe->checkout->sessions->create(['success_url' => $success_url, // 付款或设置完成后,Stripe 应将客户发送到的 URL。'cancel_url' => $cancel_url, // 如果客户决定取消付款并返回您的网站,他们将被定向到的 URL。'payment_method_types' => ['card','alipay'], // 选择支付方式'locale'=>'auto', // auto:Stripe检测浏览器的语言环境'line_items' => [['currency' => 'usd', // 支付货币类型(每个货币的 ISO 代码)'amount' => 100, //所有 API 请求都期望金额以货币的最小单位提供。例如,要收款 10 USD,提供一个值为 1000 的 amount(即 1000 美分)'name' => 'Payment', //产品的名称,旨在向客户展示。每当通过订阅销售此产品时,名称将显示在相关的发票行项目描述中。'quantity' => 1, //正在购买的项目的数量'description'=>"自定义支付页面提示信息", //产品的描述,旨在向客户展示。使用此字段可选择存储所售产品的长格式说明,用于您自己的渲染目的。],],'mode' => 'payment', // payment:接受卡、iDEAL 等的一次性付款。setup:保存付款详细信息,以便以后向您的客户收费。subscription:使用 Stripe Billing 设置固定价格订阅。]);// 支付回调主要代码
Stripe::setApiKey(env('SCRIPE_SECRET_KEY'));$endpoint_secret = env('NOTIFY_SIGN');
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null; // 回调返回的数据
try {$event = Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
} catch(\UnexpectedValueException $e) {// Invalid payloadhttp_response_code(400);exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {// Invalid signaturehttp_response_code(400);\Log::info('签名异常信息'.$e->getMessage());exit();
}
// Handle the event
switch ($event->type) {case 'payment_intent.succeeded':$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntenthandlePaymentIntentSucceeded($paymentIntent);break;case 'payment_method.attached':$paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethodhandlePaymentMethodAttached($paymentMethod);break;// ... handle other event typesdefault:echo 'Received unknown event type ' . $event->type;
}