thinkphp5实现微信支付商家红包的接口开发

ThinkPHP5中实现微信支付商家红包的功能,你需要按照以下步骤进行开发。微信支付的红包接口主要用于向用户发放现金红包,因此需要确保你已经获得了相应的权限并配置好了相关的API密钥和证书。

步骤概述

  1. 获取微信支付商户号和API证书

    • 登录微信支付商户平台,获取商户号、API密钥。
    • 下载API证书(apiclient_cert.pem和apiclient_key.pem)。
  2. 安装微信支付SDK

    • 使用Composer安装wechatpay/wechatpay SDK。

      composer require wechatpay/wechatpay
  3. 配置微信支付参数

    • 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
          ],
      ];
  4. 编写红包发送逻辑

    • 创建一个服务类或控制器来处理红包发送逻辑。

      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 WechatRedpackService
      {
          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 sendRedpack($openid, $amount, $sendName, $wishing, $actName, $remark)
          {
              try {
                  $response = $this->client->v3->redpack->send([
                      'json' => [
                          'mch_billno' => uniqid(), // 商户订单号
                          'mch_id' => config('wechat_pay.mch_id'),
                          'wxappid' => 'your_app_id', // 微信开放平台审核通过的应用APPID
                          'send_name' => $sendName, // 红包发送者名称
                          're_openid' => $openid, // 接收红包的用户openid
                          'total_amount' => $amount * 100, // 金额,单位为分
                          'total_num' => 1, // 红包个数
                          'wishing' => $wishing, // 红包祝福语
                          'client_ip' => request()->ip(), // 调用接口的机器Ip地址
                          'act_name' => $actName, // 活动名称
                          'remark' => $remark, // 备注信息
                          'scene_id' => 'PRODUCT_1', // 场景ID
                          'consume_mch_id' => '', // 子商户号
                          'nonce_str' => uniqid(), // 随机字符串
                          'sign_type' => 'HMAC-SHA256', // 签名类型
                      ],
                  ]);
      
                  return $response;
              } catch (\Exception $e) {
                  return ['code' => 500, 'msg' => $e->getMessage()];
              }
          }
      }
  5. 处理回调通知

    • 根据微信支付文档,编写回调处理逻辑。

      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 redpackNotify()
          {
              $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>');
          }
      }
  6. 路由配置

    • 在路由配置文件中添加回调通知的路由。

      use think\facade\Route;
      
      Route::post('notify_url', 'NotifyController@redpackNotify');

注意事项

  1. 权限申请

    • 发放红包需要商户具备相应的权限,确保你已经在微信支付商户平台上申请并通过了红包功能的审核。
  2. 证书管理

    • 确保API证书的安全,不要泄露证书路径和私钥内容。
  3. 签名和验证

    • 确保所有请求和响应都经过正确的签名和验证,以保证数据的安全性。
  4. 测试环境

    • 在正式上线前,务必在微信支付提供的沙箱环境中进行充分的测试。

通过以上步骤,你应该能够在ThinkPHP5中实现微信支付商家红包的接口开发。如果有任何疑问或遇到问题,建议参考微信支付官方文档或联系微信支付技术支持。

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我