在ThinkPHP中接入支付宝和微信支付的步骤如下:
首先,需要在支付宝和微信开放平台上注册开发者账号,并创建应用获取相应的AppID、AppSecret、商户号等信息。
在ThinkPHP项目中安装支付宝SDK和微信SDK。
在项目中配置支付宝和微信支付相关的参数,如下所示:
// 支付宝支付配置
$config = [
'app_id' => 'xxx', // 支付宝分配的APPID
'merchant_private_key' => 'xxx', // 商户私钥
'alipay_public_key' => 'xxx', // 支付宝公钥
'notify_url' => 'xxx', // 异步通知地址
'return_url' => 'xxx', // 同步跳转地址
'charset' => 'utf-8', // 编码格式
'sign_type' => 'RSA2', // 签名类型
];
// 微信支付配置
$config = [
'app_id' => 'xxx', // 微信公众账号或开放平台APP的唯一标识
'mch_id' => 'xxx', // 微信支付分配的商户号
'key' => 'xxx', // 商户支付密钥
'notify_url' => 'xxx', // 异步通知地址
'cert_path' => 'xxx', // 证书文件路径
'key_path' => 'xxx', // 证书密钥文件路径
];
// 发起支付宝支付请求
use think\facade\Log;
use Omnipay\Omnipay;
$gateway = Omnipay::create('Alipay_AopApp');
$gateway->initialize($config);
$response = $gateway->purchase([
'out_trade_no' => '201908070001', // 商户订单号
'subject' => '测试商品', // 商品名称
'total_amount' => '0.01', // 订单金额
'time_expire' => '30m', // 订单失效时间
'product_code' => 'QUICK_MSECURITY_PAY', // 产品码
])->send();
if ($response->isSuccessful()) {
// 支付成功
Log::info('支付宝支付成功');
} else {
// 支付失败
Log::error('支付宝支付失败');
}
// 发起微信支付请求
use Omnipay\Omnipay;
$gateway = Omnipay::create('WechatPay_App');
$gateway->initialize($config);
$response = $gateway->purchase([
'out_trade_no' => '201908070001', // 商户订单号
'body' => '测试商品', // 商品描述
'total_fee' => '1', // 订单金额,单位为分
'time_expire' => time() + 1800, // 订单失效时间,单位为秒
'notify_url' => 'http://example.com/notify', // 异步通知地址
])->send();
if ($response->isSuccessful()) {
// 支付成功
Log::info('微信支付成功');
} else {
// 支付失败
Log::error('微信支付失败');
}
需要注意的是,支付宝和微信支付的具体实现方式可能会因版本更新而有所变化,建议开发者在接入时参考官方文档进行操作。