在 Laravel 项目中整合微信和支付宝支付可以通过以下几种方式实现,我将为你详细介绍每种方案的优缺点及实现步骤。
安装:
composer require alipay/alipay-sdk-php
基本使用:
use Alipay\EasySDK\Kernel\Config;
use Alipay\EasySDK\Payment\App\Client;
// 配置
$options = new Config();
$options->protocol = 'https';
$options->gatewayHost = 'openapi.alipay.com';
$options->signType = 'RSA2';
$options->appId = 'your-app-id';
$options->merchantPrivateKey = 'your-private-key';
$options->alipayPublicKey = 'alipay-public-key';
$options->notifyUrl = 'your-notify-url';
// 创建支付
$client = new Client();
$client->setOptions($options);
$result = $client->pay('订单标题', '订单号', '金额');
安装:
composer require wechatpay/wechatpay
基本使用:
use WeChatPay\Builder;
use WeChatPay\Util\PemUtil;
// 配置
$merchantId = 'your-merchant-id';
$merchantSerialNumber = 'your-serial-number';
$merchantPrivateKey = PemUtil::loadPrivateKey('file://path/to/your/private/key.pem');
$platformCertificate = PemUtil::loadCertificate('file://path/to/wechatpay/cert.pem');
$instance = Builder::factory([
'mchid' => $merchantId,
'serial' => $merchantSerialNumber,
'privateKey' => $merchantPrivateKey,
'certs' => [
$platformCertificate->getSerialNumber() => $platformCertificate,
],
]);
// 创建支付
$response = $instance->v3->pay->transactions->jsapi->post([
'json' => [
'appid' => 'your-appid',
'mchid' => $merchantId,
'description' => '订单描述',
'out_trade_no' => '订单号',
'notify_url' => '回调地址',
'amount' => [
'total' => 100, // 金额(分)
'currency' => 'CNY'
],
'payer' => [
'openid' => '用户openid'
]
]
]);
推荐使用 yansongda/laravel-pay
包,它封装了微信和支付宝支付:
安装:
composer require yansongda/laravel-pay
配置:
在 config/pay.php
中配置:
return [
'alipay' => [
'default' => [
'app_id' => env('ALIPAY_APP_ID'),
'ali_public_key' => env('ALIPAY_PUBLIC_KEY'),
'private_key' => env('ALIPAY_PRIVATE_KEY'),
'log' => [
'file' => storage_path('logs/alipay.log'),
],
],
],
'wechat' => [
'default' => [
'app_id' => env('WECHAT_PAY_APP_ID'),
'mch_id' => env('WECHAT_PAY_MCH_ID'),
'key' => env('WECHAT_PAY_KEY'),
'cert_client' => resource_path('wechat_pay/apiclient_cert.pem'),
'cert_key' => resource_path('wechat_pay/apiclient_key.pem'),
'log' => [
'file' => storage_path('logs/wechat_pay.log'),
],
],
],
];
使用示例:
// 支付宝支付
$order = [
'out_trade_no' => time(),
'total_amount' => '0.01',
'subject' => '测试订单',
];
$alipay = Pay::alipay()->web($order);
return $alipay; // 跳转到支付宝
// 微信支付
$order = [
'out_trade_no' => time(),
'body' => '测试订单',
'total_fee' => 1, // 单位:分
'openid' => '用户openid',
];
$wechat = Pay::wechat()->mp($order);
return $wechat;
如 Stripe、Ping++ 等聚合支付平台,它们提供了统一的 API 对接多种支付方式。
Ping++ 示例:
// 安装SDK
composer require pingplusplus/pingpp-php
// 创建支付
\Pingpp\Pingpp::setApiKey('your-api-key');
$ch = \Pingpp\Charge::create([
'order_no' => '123456789',
'amount' => 100, // 单位:分
'app' => ['id' => 'your-app-id'],
'channel' => 'alipay', // 或 wx
'currency' => 'cny',
'client_ip' => $_SERVER['REMOTE_ADDR'],
'subject' => 'Your Subject',
'body' => 'Your Body'
]);
无论使用哪种方案,都需要处理支付结果回调:
// 支付宝回调
public function alipayNotify(Request $request)
{
$alipay = Pay::alipay();
try {
$data = $alipay->verify();
// 处理订单逻辑
Log::info('Alipay notify', $data->all());
} catch (\Exception $e) {
// 处理异常
return 'fail';
}
return $alipay->success();
}
// 微信回调
public function wechatNotify(Request $request)
{
$wechat = Pay::wechat();
try {
$data = $wechat->verify();
// 处理订单逻辑
Log::info('Wechat notify', $data->all());
} catch (\Exception $e) {
// 处理异常
return 'fail';
}
return $wechat->success();
}
对于大多数 Laravel 项目,我推荐使用 yansongda/laravel-pay
这个第三方包,它封装了微信和支付宝的支付逻辑,使用简单且维护良好。如果你的项目需要对接更多支付渠道或需要更高级的功能,可以考虑使用 Payment Gateway 服务如 Ping++。
希望这个方案对你有所帮助!如果你有更具体的需求或遇到实现中的问题,可以进一步讨论。