这是我第一次接触支付,leader让我开发一个新项目选用stripe作为支付平台
先简单描述一下过程,只需要创建一个html页面,引入两个js文件,一个php文件即可创建订单并跳转到第三方页面
账号就别问我怎么注册了,咱这是一个记录开发过程
下面这个就是登陆后的界面
需要在html页面引入两个js文件,具体看完整案例
stripe官方完整php案例
$stripe = new StripeClient('密钥,不是那个可发布密钥,是第二个');
//创建一个函数存储请求成功返回的信息,后续可以存储到订单中
$session = $stripe->checkout->sessions->create([
'success_url' => DOMAIN_NAME.'/user-page/recharge-order',
'cancel_url' => DOMAIN_NAME.'/user-page/recharge',
'payment_method_types' => ['card'],
'line_items' => [
[
'currency' => 'usd',
'amount' => $params['money']*100,
'name' => $params['user_id'],
'quantity' => 1,
],
],
'mode' => 'payment',
]);
请求POST主体(在stripe管理者平台的日志中可以看到)
{
"success_url": "http://*****/user-page/recharge-order",
"cancel_url": "http://****/user-page/recharge",
"payment_method_types": {
"0": "card"
},
"line_items": {
"0": {
"currency": "usd",
"amount": "6200",
"name": "23",
"quantity": "1"
}
},
"mode": "payment"
}
响应正文
{
"id": "cs_test_bj5amGjLeYQNUzSt4Hoku80gQnJQ4DXJJlnUlYT1nn5f8EFu0KJyUK2T",
"object": "checkout.session",
"allow_promotion_codes": null,
"amount_subtotal": 6200,
"amount_total": 6200,
"billing_address_collection": null,
"cancel_url": "http://*****/user-page/recharge",
"client_reference_id": null,
"currency": "usd",
"customer": null,
"customer_email": null,
"display_items": [
{
"amount": 6200,
"currency": "usd",
"custom": {
"description": null,
"images": null,
"name": "23"
},
"quantity": 1,
"type": "custom"
}
],
"livemode": false,
"locale": null,
"metadata": {
},
"mode": "payment",
//订单号,一般存这个到你数据库里面
"payment_intent": "pi_1HPPwrEYKdmCGR3wEfTUoIbZ",
"payment_method_types": [
"card"
],
"payment_status": "unpaid",
"setup_intent": null,
"shipping": null,
"shipping_address_collection": null,
"submit_type": null,
"subscription": null,
"success_url": "http://*****/user-page/recharge-order",
"total_details": {
"amount_discount": 0,
"amount_tax": 0
}
}
然后就直接填信息就好啦,就是如此简单;
测试环境的卡号看这里测试卡号
界面简洁如图
选择一个你要监听的事件
这里我选的checkout.session,当有支付成功事件的时候就会带加密后的参数访问我给他的接口
Stripe::setApiKey('密钥');
$endpoint_secret = '回调密钥签名';
$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 payload
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
exit();
}
switch ($event->type) {
case 'charge.succeeded':
$succeeded = $event->data->object;
if ($succeeded->status == 'succeeded'){
//目前先直接拿金额进行对比,后续再考虑汇率换算
$dbOrder = OrderModel::find()->select([])->where(['payment_order_id'=>$succeeded->payment_intent])->asArray()->one();
if ($dbOrder['money']*100 == $succeeded->amount){
OrderModel::updateInfo(
['status'=>OrderModel::ALREADY_PAYMENT,'pay_time'=>$succeeded->created,'payment_channel_name'=>\Yii::$app->request->get('payment_channel_name')],
['payment_order_id'=>$succeeded->payment_intent,'status'=>OrderModel::NON_PAYMENT]);
$package = OrderService::getInstance()->packageConvert($dbOrder['product_name']);
// 更新用户次数并且记录ip行为
ActionLogModel::updateProxyTimes($package['product_name'],$dbOrder['user_id'],ActionLogModel::RECHARGE_PROXY_TIME);
}
}
break;
default:
// Unexpected event type
http_response_code(400);
exit();
}
http_response_code(200);
到这里就结束了,后续再补充,有什么需要补充的也可以评论留言