tripe 的 PHP 接口(API)使用方法,
一、Stripe PHP SDK 简介
Stripe 提供官方的 PHP SDK,用来访问所有 Stripe 的功能(收款、退款、订阅、分账等)。
安装命令(推荐用 Composer): composer require stripe/stripe-php
有 Composer,可以去:https://github.com/stripe/stripe-php 下载后手动引入 init.php。
二、初始化配置
在你的后端 PHP 中初始化 Stripe 客户端:
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_你的私钥'); // 后台获取 Secret Key
在 Stripe 控制台:
- Secret Key(后端用) → sk_test_xxx
- Publishable Key(前端用) → pk_test_xxx
三、创建支付意图(PaymentIntent)
Stripe 的核心机制是 PaymentIntent,它代表一次支付流程。
示例:创建支付订单
try {
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => 5000, // 单位是分,比如 5000 = $50.00
'currency' => 'usd',
'description' => '测试订单 #123',
'automatic_payment_methods' => [
'enabled' => true,
],
]);
echo json_encode([
'clientSecret' => $paymentIntent->client_secret,
]);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
}
这个接口会返回一个 client_secret,前端 Stripe.js 会用它调起支付页面。

