对接stripe支付(新版)
fashion
2023-07-11 11:29
2472
新版stripe支付不会在checkout的接口直接返回payment_intent,因此我们需要换一种方式传入我们自定义的数据,来在回调中拿到;
在checkout-session的create方法中传入参数payment_intent_data
\Stripe\Stripe::setApiKey($this->key);
$session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price_data' => [
'currency' => $currency,
'product_data' => [
'name' => $product,
],
'unit_amount' => $amount,
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $success_url,
'cancel_url' => url('/', '', true, true),
'payment_intent_data' => [//这个参数里的内容可以自定义
'metadata' => ['order_sn'=>$orderno],
]
]);
然后在stripe的后台中设置webhook
选择payment_intent.succeeded并配置你的回调地址
在回调方法中,$event→data→object下,可以直接拿到你创建订单时传入的payment_intent_data里的所有数据
\Stripe\Stripe::setApiKey($this->sign_key);
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
echo "fail";
exit();
}
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$object = $event->data->object;
if ($object->status == 'succeeded'){//支付成功
$order_num = $object->metadata->order_sn;
}
break;
}
echo "success";exit;
0 条讨论