thinkphp5实现微信支付商家转账到零钱的接口开发
2024-12-12 09:43:41
要在ThinkPHP5中实现微信支付商家转账到零钱的功能,你需要按照以下步骤进行开发:
获取微信支付商户号和API证书:
- 登录微信支付商户平台,获取商户号、API密钥。
- 下载API证书(apiclient_cert.pem和apiclient_key.pem)。
安装微信支付SDK:
使用Composer安装
wechatpay/wechatpay
SDK。composer require wechatpay/wechatpay
配置微信支付参数:
在
config.php
或其他配置文件中添加微信支付的相关配置。return [ // ...其他配置... 'wechat_pay' => [ 'mch_id' => 'your_merchant_id', // 商户号 'key' => 'your_api_key', // API密钥 'cert_path' => '/path/to/apiclient_cert.pem', // 证书路径 'key_path' => '/path/to/apiclient_key.pem', // 私钥路径 'notify_url' => 'http://yourdomain.com/notify_url', // 回调URL ], ];
编写转账逻辑:
创建一个服务类或控制器来处理转账逻辑。
namespace app\service; use WeChatPay\Builder; use WeChatPay\Crypto\Rsa; use WeChatPay\Crypto\RsaSigner; use WeChatPay\Crypto\RsaVerifier; use WeChatPay\HttpService\Curl; use WeChatPay\Util\PemUtil; class WechatTransferService { protected $client; public function __construct() { $config = config('wechat_pay'); $mchId = $config['mch_id']; $serialNo = 'your_certificate_serial_no'; // 证书序列号 $privateKey = PemUtil::loadPrivateKey($config['key_path']); $certificates = [ $serialNo => PemUtil::loadCertificate($config['cert_path']), ]; $this->client = Builder::factory([ 'mchid' => $mchId, 'serial' => $serialNo, 'privateKey' => $privateKey, 'certificates' => $certificates, ]); } public function transferToWallet($openid, $amount, $desc) { try { $response = $this->client->v3->transfer->transfers->create([ 'json' => [ 'sp_appid' => 'your_app_id', // 微信开放平台审核通过的应用APPID 'sub_appid' => '', // 子商户公众账号ID 'mchid' => config('wechat_pay.mch_id'), 'sub_mchid' => '', // 子商户号 'nonce_str' => uniqid(), // 随机字符串 'partner_trade_no' => uniqid(), // 商户订单号 'openid' => $openid, // 用户openid 'check_name' => 'NO_CHECK', // NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名 OPTION_CHECK:选择性校验真实姓名 're_user_name' => '', // 收款用户真实姓名(check_name为FORCE_CHECK或OPTION_CHECK时必填) 'amount' => $amount * 100, // 金额,单位为分 'desc' => $desc, // 企业付款备注 'spbill_create_ip' => request()->ip(), // 调用接口的机器Ip地址 ], ]); return $response; } catch (\Exception $e) { return ['code' => 500, 'msg' => $e->getMessage()]; } } }
处理回调通知:
根据微信支付文档,编写回调处理逻辑。
namespace app\controller; use think\Controller; use WeChatPay\Builder; use WeChatPay\Crypto\Rsa; use WeChatPay\Crypto\RsaSigner; use WeChatPay\Crypto\RsaVerifier; use WeChatPay\HttpService\Curl; use WeChatPay\Util\PemUtil; class NotifyController extends Controller { public function transferNotify() { $config = config('wechat_pay'); $serialNo = 'your_certificate_serial_no'; // 证书序列号 $publicKey = PemUtil::loadCertificate($config['cert_path']); $verifier = new RsaVerifier(function ($serial) use ($publicKey) { if ($serial === $serialNo) { return $publicKey; } throw new \Exception("Unknown certificate serial number"); }); $rawBody = file_get_contents('php://input'); $signature = $_SERVER['HTTP_WECHATPAY_SIGNATURE']; $timestamp = $_SERVER['HTTP_WECHATPAY_TIMESTAMP']; $nonce = $_SERVER['HTTP_WECHATPAY_NONCE']; $body = $verifier->verify( $signature, $timestamp, $nonce, $rawBody ); $data = json_decode($body, true); // 处理回调数据 // ... return response()->create('', 'xml', 200, ['Content-Type' => 'application/xml'])->data('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'); } }
路由配置:
在路由配置文件中添加回调通知的路由。
use think\facade\Route; Route::post('notify_url', 'NotifyController@transferNotify');
以上步骤涵盖了从配置到实现转账功能的基本流程。请根据实际需求调整代码,并确保遵循微信支付的安全规范。
还没有人发表评论