Stripe支付 :Stripe Login | Sign in to the Stripe Dashboard
1. 安装Stripe:
composer require stripe/stripe-php
2. 获取密钥:https://dashboard.stripe.com/test/apikeys
3. 创建产品:Stripe Login | Sign in to the Stripe Dashboard
4. php 代码:
官方发起支付请求示例:https://stripe.com/docs/checkout/quickstart
/**
* stripe支付
*/
public function actionStripe()
{
$_key = 'sk_test_51Jyo2iHnJH***********************************';
// stripe 生成订单
$url = 'https://www.baidu.com';
\Stripe\Stripe::setApiKey($_key);
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price' => {{PRICE_ID}}, // 产品id
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $url . '/success.html',
'cancel_url' => $url . '/cancel.html',
'automatic_tax' => [
'enabled' => true,
],
]);
$arrGoodsInfo['payment_intent'] = $checkout_session->payment_intent;
// 创建本地订单操作,并payment_intent保留
// ......
//$checkout_session->url stripe跳转支付url
// $checkout_session->payment_intent是识别支付与通知关系的唯一凭证
return ['url' => $checkout_session->url, 'id' => $checkout_session->id, 'payment_intent' => $checkout_session->payment_intent];
}
出现tax_behavior 相关问题时需查看产品是否配置税务信息
var_dump($checkout_session
);
结果:
array(
'id' => 'cs_test_a1SsLDn1NtwTzLa0JlOqH5Xr3nEUGvC2iQui0EFyedgTkpSrQhJDowfZ0E',
'object' => 'checkout.session',
'after_expiration' => NULL,
'allow_promotion_codes' => NULL,
'amount_subtotal' => 400,
'amount_total' => 400,
'automatic_tax' =>
array(
'enabled' => false,
'status' => NULL,
),
'billing_address_collection' => NULL,
'cancel_url' => 'https://www.baidu.com/cancel.html',
'client_reference_id' => NULL,
'consent' => NULL,
'consent_collection' => NULL,
'currency' => 'hkd',
'customer' => NULL,
'customer_creation' => 'always',
'customer_details' => NULL,
'customer_email' => NULL,
'expires_at' => 1654681622,
'livemode' => false,
'locale' => NULL,
'metadata' =>
array(),
'mode' => 'payment',
'payment_intent' => 'pi_3L7z07IUnSe35qBJ0VC7FqLy',
'payment_link' => NULL,
'payment_method_options' =>
array(),
'payment_method_types' =>
array(
0 => 'card',
),
'payment_status' => 'unpaid',
'phone_number_collection' =>
array(
'enabled' => false,
),
'recovered_from' => NULL,
'setup_intent' => NULL,
'shipping' => NULL,
'shipping_address_collection' => NULL,
'shipping_options' =>
array(),
'shipping_rate' => NULL,
'status' => 'open',
'submit_type' => NULL,
'subscription' => NULL,
'success_url' => 'https://www.baidu.com/success.html',
'total_details' =>
array(
'amount_discount' => 0,
'amount_shipping' => 0,
'amount_tax' => 0,
),
'url' => 'https://checkout.stripe.com/pay/cs_test_a1SsLDn1NtwTzLa0JlOqH5Xr3nEUGvC2iQui0EFyedgTkpSrQhJDowfZ0E#fidkdWxOYHwnPyd1blpxYHZxWjA0STJfUkFMUGtWYDYwdEdPdWRrfUo9a3xNZndOYVJfTGxwdE5ATHdPfEAxaVA3Q1xNbnFMVUt2bzZQYGpvYHBmf11BPGRtZ2cyRFNHSGxBYl9jakNDSDU1NTV9aXNRPWNdXycpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPyd2bGtiaWBabHFgaCcpJ2BrZGdpYFVpZGZgbWppYWB3dic%2FcXdwYHgl',
);
官方提供得测试账号:Testing | Stripe Documentation
支付成功会自动跳转到success.html
回调:
<?php
/**
* 支付回调
*/
namespace api\controllers;
use yii\web\Controller;
use Yii;
class PayNotifyController extends Controller
{
/**
* stripe支付回调
*/
public function actionStripe()
{
$_key = 'sk_test_51Jyo2iHnJH***********************************';
\Stripe\Stripe::setApiKey($_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;
$content = "=========".date('Y-m-d H:i:s',time())."==========\r\n";
$content .= json_encode($succeeded);
file_put_contents(dirname(__DIR__).'/runtime/logs/stripe_success.log',$content . "\r\n",FILE_APPEND);
if ($succeeded->status == 'succeeded'){
$payment_intent = $succeeded->payment_intent;
}
break;
default:
echo 'Received unknown event type ' . $event->type;
break;
}
return true;
}
}