$pay_type, 'line_items' => [[ 'price_data' => [ 'currency' => $currency, 'unit_amount' => $amount, 'product_data' => $product ], 'quantity' => 1, ]], 'mode' => 'payment', 'success_url' => $YOUR_DOMAIN . '/succ', // 支付成功后跳转url 'cancel_url' => $YOUR_DOMAIN . '/cancel', // 支付失败后跳转url ]); var_dump($checkout_session);die; return ['id' => $checkout_session->id, 'payment_intent' => $checkout_session->payment_intent]; // payment_intent是识别支付与通知关系的唯一凭证 } public function pay() { $product = [ 'name' => 'Order Number:' . '123123123' , 'images' => [ 'https://rongsp.com/Super/favicon.ico' ] ]; $amount = 100; $result = self::stripePay($product, $amount); if (!isset($result['id']) || !isset($result['payment_intent'])) { returnData(500, 'ERROR! BUSY NETWORK!'); } // 将凭证保存,便于在回调时查询订单 // $update = Db::name('orders')->where('order_id', $order_info['order_id'])->update(['payment_intent' => $result['payment_intent']]); /* if (!$update) { returnData(500, 'ERROR! BUSY NETWORK!'); }*/ // returnData(200, 'SUCCESS!', ['id' => $result['id']]); } // 支付成功通知 public function successNotify() { // Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey(self::$key); $payload = @file_get_contents('php://input'); $event = null; try { $event = \Stripe\Event::constructFrom( json_decode($payload, true) ); } catch(\UnexpectedValueException $e) { // Invalid payload http_response_code(400); exit(); } // Handle the event switch ($event->type) { case 'charge.succeeded': $succeeded = $event->data->object; if ($succeeded->status == 'succeeded'){ $payment_intent = $succeeded->payment_intent; // 查询订单详情 $order_info = Db::name('orders')->where('payment_intent', $payment_intent)->find(); if (!$order_info) { http_response_code(200);exit; } if ($order_info['status'] != 0) { return; } // 订单标记为处理中 Db::name('orders')->where('order_id', $order_info['order_id'])->update(['pay_time' => time(), 'status' => 3]); // 订单处理 // ... // 标记为处理完成 Db::name('orders')->where('order_id', $order_info['order_id'])->update(['status' => 1]); } break; default: echo 'Received unknown event type ' . $event->type; } http_response_code(200); } }