一、创建customers
用这个接口生成cus_HqT0BoZJxXDY5l
$stripe = new \Stripe\StripeClient(
'sk_test_obfuscated_offline_key'
);
$stripe->customers->create([
'description' => 'My First Test Customer (created for API docs)',
]);
{
"id": "cus_HqT0BoZJxXDY5l",
"object": "customer",
"address": null,
"balance": 0,
"created": 1597585145,
"currency": "usd",
"default_source": null,
"delinquent": false,
"description": "My First Test Customer (created for API docs)",
"discount": null,
"email": null,
"invoice_prefix": "FD70421",
"invoice_settings": {
"custom_fields": null,
"default_payment_method": null,
"footer": null
},
"livemode": false,
"metadata": {},
"name": null,
"next_invoice_sequence": 1,
"phone": null,
"preferred_locales": [],
"shipping": null,
"sources": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/customers/cus_HqT0BoZJxXDY5l/sources"
},
"subscriptions": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/customers/cus_HqT0BoZJxXDY5l/subscriptions"
},
"tax_exempt": "none",
"tax_ids": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/customers/cus_HqT0BoZJxXDY5l/tax_ids"
}
}
第二步 拿到src_id
\Stripe\Stripe::setApiKey("sk_test_51HFsuqDbsQ4o4itGIuhoUc084spt6zvQCZ3pw8akHMVqWlVnhw2gfWS2Lk4Eaho3qkCQYZPbDqTweg7UDsE2Xi3Q006j0Q2cT0");
\Stripe\Source::create([
"type" => "card",
"card" => [
"number" => "jenny.rosen@example.com",
"exp_month"=>""
]
]);
{
"type": "card",
"card": {
"number": "************4242",
"cvc": "***",
"exp_month": "02",
"exp_year": "23"
},
"guid": "eb1dcee0-5ed4-467b-8573-611e65b8923a06145b",
"muid": "1eb017c3-7102-4b39-8335-98a82da7297ca1d65f",
"sid": "46ccccc4-beb6-4076-bb25-d872356e937281f7af",
"payment_user_agent": "stripe.js/0c551e51; stripe-js-v3/0c551e51",
"time_on_page": "173724",
"referrer": "https://dashboard.stripe.com/",
"key": "pk_test_51*********************************************************************************************fxcN"
}
{
"id": "src_1HGm0yDbsQ4o4itGjJR3AwGS",
"object": "source",
"amount": null,
"card": {
"exp_month": 2,
"exp_year": 2023,
"last4": "4242",
"country": "US",
"brand": "Visa",
"cvc_check": "unchecked",
"funding": "credit",
"three_d_secure": "optional",
"name": null,
"address_line1_check": null,
"address_zip_check": null,
"tokenization_method": null,
"dynamic_last4": null
}
第三步 PaymentIntent 拿pid
$stripe = new \Stripe\StripeClient(
'sk_test_51HFsuqDbsQ4o4itGIuhoUc084spt6zvQCZ3pw8akHMVqWlVnhw2gfWS2Lk4Eaho3qkCQYZPbDqTweg7UDsE2Xi3Q006j0Q2cT0'
);
$stripe->paymentIntents->create([
'amount' => 2000,
'currency' => 'usd',
'payment_method_types' => ['card'],
]);
请求数据 response
{
"payment_method_types": [
"card"
],
"payment_method_options": {
"card": {
"moto": "true"
}
},
"amount": "10000",
"capture_method": "automatic",
"confirm": "true",
"currency": "usd",
"statement_descriptor": "this is mac book pro",
"source": "src_1HGm0yDbsQ4o4itGjJR3AwGS",
"customer": "cus_HqSuAhFlkmuefE",
"setup_future_usage": "off_session",
"description": "购买苹果电脑"
}
返回数据 request
获得 pi_1HGm0zDbsQ4o4itGLuVjaYL9 参数
{
"id": "pi_1HGm0zDbsQ4o4itGLuVjaYL9",
"object": "payment_intent",
"amount": 10000,
"capture_method": "automatic",
"currency": "usd",
"customer": "cus_HqSuAhFlkmuefE",
"description": "购买苹果电脑",
"last_payment_error": null,
"livemode": false,
"next_action": null,
"payment_method_options": {
"card": {
"installments": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
]
}
第四步 确认付款
$stripe = new \Stripe\StripeClient(
'sk_test_51HFsuqDbsQ4o4itGIuhoUc084spt6zvQCZ3pw8akHMVqWlVnhw2gfWS2Lk4Eaho3qkCQYZPbDqTweg7UDsE2Xi3Q006j0Q2cT0'
);
$stripe->paymentIntents->confirm(
'pi_1HGKkCDbsQ4o4itGYOh2Yumz',
['payment_method' => 'pm_card_visa']
);
namespace app\controller;
use app\BaseController;
use app\Request;
use think\facade\View;
class Index extends BaseController
{
public $sk = 'sk_test_51HFsuqDbsQ4o4itGIuhoUc084spt6zvQCZ3pw8akHMVqWlVnhw2gfWS2Lk4Eaho3qkCQYZPbDqTweg7UDsE2Xi3Q006j0Q2cT0';
public function index()
{
return View::fetch();
}
public function get_cusid()
{
$stripe = new \Stripe\StripeClient(
$this->sk
);
$res = $stripe->customers->create([
'description' => 'My First Test Customer (created for API docs)',
]);
return $res['id'];
}
public function get_srcid()
{
\Stripe\Stripe::setApiKey($this->sk);
$src = \Stripe\Source::create([
"type" => "card",
"card" => [
"number" => "4242424242424242",
"exp_month"=>"02",
"cvc"=>"333",
"exp_year"=>"23"
]
]);
return $src['id'];
}
public function get_pid()
{
$stripe = new \Stripe\StripeClient($this->sk);
$res = $stripe->paymentIntents->create([
'amount' => 14000,
'currency' => 'usd',
'payment_method_types' => ['card'],
'source'=>$this->get_srcid(),
'customer'=>$this->get_cusid(),
'description'=>'mac book pro'
]);
return $res['id'];
}
public function pay()
{
$stripe = new \Stripe\StripeClient($this->sk);
$res = $stripe->paymentIntents->confirm(
$this->get_pid()
);
return $res;
}
}