当前位置:   article > 正文

通过反射类打造通用微信支付回调_php 获取wechatpay-nonce

php 获取wechatpay-nonce
  1. // 支付回调接收
  2. public function notify()
  3. {
  4. $inWechatpaySignature = $this->request->getHeaderLine('Wechatpay-Signature'); // 请根据实际情况获取
  5. $inWechatpayTimestamp = $this->request->getHeaderLine('Wechatpay-Timestamp'); // 请根据实际情况获取
  6. $inWechatpaySerial = $this->request->getHeaderLine('Wechatpay-Serial'); // 请根据实际情况获取
  7. $inWechatpayNonce = $this->request->getHeaderLine('Wechatpay-Nonce'); // 请根据实际情况获取
  8. $inBody = $this->request->getBody()->getContents(); // 请根据实际情况获取,例如: file_get_contents('php://input');
  9. $params = [
  10. 'inWechatpaySignature' => $inWechatpaySignature,
  11. 'inWechatpayTimestamp' => $inWechatpayTimestamp,
  12. 'inWechatpaySerial' => $inWechatpaySerial,
  13. 'inWechatpayNonce' => $inWechatpayNonce,
  14. 'inBody' => $inBody,
  15. ];
  16. try {
  17. $res = $this->wxAppPayNotifyVerify($params, self::class, 'callbackNotify');
  18. } catch (\Exception $e) {
  19. var_dump($e->getMessage());
  20. return $this->response->withStatus(500)->json([
  21. 'code' => 'FAIL',
  22. 'message' => '失败',
  23. ]);
  24. }
  25. if ($res) {
  26. return $this->response->json([
  27. 'code' => 'SUCCESS',
  28. 'message' => '成功',
  29. ]);
  30. } else {
  31. return $this->response->withStatus(500)->json([
  32. 'code' => 'FAIL',
  33. 'message' => '失败',
  34. ]);
  35. }
  36. }
  37. // 支付回调执行方法
  38. public function callbackNotify(array $params = [])
  39. {
  40. // 获取附加消息
  41. $attach = isset($params['attach']) ? json_decode($params['attach'], true) : [];
  42. if (!$attach) return false;
  43. // 校验附加消息
  44. $id = $attach['id'] ?? '';
  45. if (!$id) return false;
  46. // 业务处理
  47. return true;
  48. }
  49. /**
  50. * 微信小程序支付回调
  51. * wxAppPayNotifyVerify($params, self::class, 'callbackNotify');
  52. *
  53. * @authors: Msy
  54. * @Created-Time: 2023/2/9 13:28
  55. * @param array $notifyData 回调返回的参数数组
  56. * @param $class 回调所在的类
  57. * @param $method 回调方法名
  58. * @return false|mixed|void
  59. * @throws \ReflectionException
  60. */
  61. public function wxAppPayNotifyVerify(array $notifyData, $class, $method)
  62. {
  63. // start
  64. 这里执行支付回调解密 , 自行查看官方文档
  65. https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
  66. // end
  67. // 反射类执行回调
  68. $classObj = new \ReflectionClass($class);
  69. $class_instantce = $classObj->newInstance(); // 通过反射对象创建类的实例
  70. $method = $classObj->getMethod($method);
  71. // 下两行注释也可达到同样效果 , 方法参数名需要对应 , 由于我的方法只有一个参数,所以直接用invoke了
  72. // $params = ['方法参数名' => $inBodyResourceArray];
  73. // return $method->invokeArgs($class_instantce, $params);
  74. return $method->invoke($class_instantce, $inBodyResourceArray);
  75. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/184018
推荐阅读
相关标签
  

闽ICP备14008679号