首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[分享]揭秘PHP开发微信支付接口:轻松上手,实现移动支付新体验

发布于 2025-07-16 10:48:52
0
1190

微信支付作为国内领先的移动支付解决方案,已经成为众多企业和开发者青睐的支付方式。本文将带你深入了解如何使用PHP开发微信支付接口,轻松实现移动支付新体验。一、准备工作1. 环境配置PHP版本:推荐使用...

微信支付作为国内领先的移动支付解决方案,已经成为众多企业和开发者青睐的支付方式。本文将带你深入了解如何使用PHP开发微信支付接口,轻松实现移动支付新体验。

一、准备工作

1. 环境配置

  • PHP版本:推荐使用PHP 7.2及以上版本。
  • 安装Composer:用于管理PHP依赖包。

2. 安装微信支付SDK

使用Composer安装官方SDK:

composer require wechatpay/wechatpay

如果因为第三方包导致提示PHP版本问题而安装不了,可以使用以下命令忽略平台要求:

composer require wechatpay/wechatpay --ignore-platform-reqs

二、配置商户信息

在项目中配置商户信息,包括:

  • appid:微信开发平台审核通过的应用ID。
  • merchantId:商户ID。
  • merchantV3PrivateKey:商户v3版本私钥。
  • merchantPrivatePath:商户API私钥文件的绝对路径。
  • merchantCertificateSerial:商户API证书的证书序列号。
  • platformCertificateOrPublicKeyFilePath:微信支付平台证书文件的绝对路径。
  • platformCertificateSerialOrPublicKeyId:微信支付平台证书的证书序列号。

三、构造APIv3客户端实例

创建一个WxPayV3Controller类,并构造一个APIv3客户端实例:

namespace AppHttpControllers;
use WeChatPayCryptoRsa;
use WeChatPayGuzzleMiddlewareVerifySignature;
use WeChatPayGuzzleMiddlewareWithMerchantCertificate;
use WeChatPayGuzzleMiddlewareWithMerchantSerialNumber;
use WeChatPayGuzzleMiddlewareWithMerchantPrivateKey;
use WeChatPayGuzzleMiddlewareWithMerchantPublicKey;
use WeChatPayGuzzleMiddlewareWithMerchantApiV3Key;
use WeChatPayGuzzleMiddlewareWithMerchantApiV3Certificate;
use WeChatPayGuzzleMiddlewareWithMerchantApiV3SerialNumber;
class WxPayV3Controller
{ protected $appid; protected $merchantId; protected $merchantV3PrivateKey; protected $merchantPrivatePath; protected $merchantCertificateSerial; protected $platformCertificateOrPublicKeyFilePath; protected $platformCertificateSerialOrPublicKeyId; public function __construct() { $this->appid = 'wxabc1111123213'; $this->merchantId = '1658899999'; $this->merchantV3PrivateKey = 'V3e123ABC12312AAALDLSFLSDCDSLDF'; $this->merchantPrivatePath = 'D:/EMinxxxcertwxv3v3apiclientkey.pem'; $this->merchantCertificateSerial = '73021A1A1A1A1A1A1A1A1A1A1A1A1A1A1A1A1A1A1A1A'; $this->platformCertificateOrPublicKeyFilePath = 'D:/EMinxxxcertwxv3v3certificateorpublickey.pem'; $this->platformCertificateSerialOrPublicKeyId = '5B1A1A1A1A1A1A1A1A1A1A1A1A1A1A1A'; } public function getWxPayClient() { $privateKey = file_get_contents($this->merchantPrivatePath); $serialNumber = $this->merchantCertificateSerial; $publicKey = file_get_contents($this->platformCertificateOrPublicKeyFilePath); $apiV3Key = file_get_contents($this->merchantPrivatePath); $config = [ 'verify_ssl' => false, 'ssl_cafile' => __DIR__ . '/cacert.pem', 'middleware' => [ new VerifySignature(), new WithMerchantCertificate($serialNumber, $privateKey, $publicKey), new WithMerchantPrivateKey($privateKey), new WithMerchantPublicKey($publicKey), new WithMerchantApiV3Key($apiV3Key), new WithMerchantApiV3SerialNumber($serialNumber), ], ]; return new Client($config); }
}

四、发起支付请求

使用构造好的客户端实例发起支付请求,例如:

use WeChatPayGuzzleMiddlewareVerifySignature;
use WeChatPayGuzzleMiddlewareWithMerchantCertificate;
use WeChatPayGuzzleMiddlewareWithMerchantSerialNumber;
use WeChatPayGuzzleMiddlewareWithMerchantPrivateKey;
use WeChatPayGuzzleMiddlewareWithMerchantPublicKey;
use WeChatPayGuzzleMiddlewareWithMerchantApiV3Key;
use WeChatPayGuzzleMiddlewareWithMerchantApiV3Certificate;
use WeChatPayGuzzleMiddlewareWithMerchantApiV3SerialNumber;
class WxPayV3Controller
{ // ... (其他代码) public function unifiedOrder() { $client = $this->getWxPayClient(); $path = '/v3/pay/transactions/native'; $params = [ 'appid' => $this->appid, 'mchid' => $this->merchantId, 'description' => '商品描述', 'out_trade_no' => '商户订单号', 'amount' => [ 'total' => 1, 'currency' => 'CNY', ], ]; $response = $client->post($path, [ 'json' => $params, ]); return json_decode($response->getBody(), true); }
}

五、支付结果回调

在微信支付接口中,支付结果回调是一个重要的环节。你需要在服务器上设置一个URL,用于接收微信支付服务器发送的回调信息。

use WeChatPayGuzzleMiddlewareVerifySignature;
class WxPayV3Controller
{ // ... (其他代码) public function notify() { $client = $this->getWxPayClient(); $path = '/v3/pay/transactions/native/notify'; $body = file_get_contents('php://input'); $response = $client->post($path, [ 'body' => $body, ]); $notifyData = json_decode($response->getBody(), true); // 验证签名 $this->verifySignature($notifyData); // 处理回调数据 // ... return json_encode(['return_code' => 'SUCCESS']); } private function verifySignature($notifyData) { $sign = $notifyData['sign']; unset($notifyData['sign']); $expectedSign = hash_hmac('sha256', json_encode($notifyData), 'your_secret_key'); if ($sign !== $expectedSign) { throw new Exception('Invalid signature'); } }
}

六、总结

通过以上步骤,你可以在PHP中轻松实现微信支付接口的开发。微信支付SDK提供了丰富的功能,可以满足大部分支付场景的需求。在实际开发过程中,请务必仔细阅读官方文档,以确保支付过程的安全性。

评论
一个月内的热帖推荐
极兔cdn
Lv.1普通用户

3

帖子

6

小组

37

积分

赞助商广告
站长交流