Index.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace app\admin\controller;
  3. use WeChatPay\Builder;
  4. use WeChatPay\Crypto\Rsa;
  5. use WeChatPay\Transformer;
  6. use WeChatPay\Util\PemUtil;
  7. class Index
  8. {
  9. public function index()
  10. {
  11. return redirect('/admin.php/home');
  12. }
  13. public function t1()
  14. {
  15. // 商户号
  16. $merchantId = config('wxconfig.payMchId');
  17. // 从本地文件中加载「商户API私钥」,「商户API私钥」会用来生成请求的签名
  18. $merchantPrivateKeyFilePath = file_get_contents(root_path('/extend/wechat-del/cert') . 'apiclient_key.pem');
  19. $merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
  20. // 「商户API证书」的「证书序列号」
  21. $merchantCertificateSerial = config('wxconfig.serial');
  22. // 从本地文件中加载「微信支付平台证书」,用来验证微信支付应答的签名
  23. $platformCertificateFilePath = file_get_contents(root_path('/extend/wechat-del/cert') . 'apiclient_cert.pem');
  24. $platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
  25. // 从「微信支付平台证书」中获取「证书序列号」
  26. $platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);
  27. // 构造一个 APIv3 客户端实例
  28. $instance = Builder::factory([
  29. 'mchid' => $merchantId,
  30. 'serial' => $merchantCertificateSerial,
  31. 'privateKey' => $merchantPrivateKeyInstance,
  32. 'certs' => [
  33. $platformCertificateSerial => $platformPublicKeyInstance,
  34. ],
  35. ]);
  36. // 发送请求
  37. $resp = $instance->chain('v3/certificates')->get(
  38. ['debug' => true] // 调试模式,https://docs.guzzlephp.org/en/stable/request-options.html#debug
  39. );
  40. halt($resp->getBody());
  41. }
  42. }