Order.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. namespace WePayV3;
  14. use WeChat\Contracts\Tools;
  15. use WeChat\Exceptions\InvalidArgumentException;
  16. use WePayV3\Contracts\BasicWePay;
  17. use WePayV3\Contracts\DecryptAes;
  18. /**
  19. * 订单支付接口
  20. * Class Order
  21. * @package WePayV3
  22. */
  23. class Order extends BasicWePay
  24. {
  25. const WXPAY_H5 = 'h5';
  26. const WXPAY_APP = 'app';
  27. const WXPAY_JSAPI = 'jsapi';
  28. const WXPAY_NATIVE = 'native';
  29. /**
  30. * 创建支付订单
  31. * @param string $type 支付类型
  32. * @param array $data 支付参数
  33. * @return array
  34. * @throws \WeChat\Exceptions\InvalidResponseException
  35. */
  36. public function create($type, $data)
  37. {
  38. $types = [
  39. 'h5' => '/v3/pay/transactions/h5',
  40. 'app' => '/v3/pay/transactions/app',
  41. 'jsapi' => '/v3/pay/transactions/jsapi',
  42. 'native' => '/v3/pay/transactions/native',
  43. ];
  44. if (empty($types[$type])) {
  45. throw new InvalidArgumentException("Payment {$type} not defined.");
  46. } else {
  47. // 创建预支付码
  48. $result = $this->doRequest('POST', $types[$type], json_encode($data, JSON_UNESCAPED_UNICODE), true);
  49. if (empty($result['prepay_id'])) return $result;
  50. // 支付参数签名
  51. $time = (string)time();
  52. $appid = $this->config['appid'];
  53. $prepayId = $result['prepay_id'];
  54. $nonceStr = Tools::createNoncestr();
  55. if ($type === 'app') {
  56. $sign = $this->signBuild(join("\n", [$appid, $time, $nonceStr, $prepayId, '']));
  57. return ['partnerId' => $this->config['mch_id'], 'prepayId' => $prepayId, 'package' => 'Sign=WXPay', 'nonceStr' => $nonceStr, 'timeStamp' => $time, 'sign' => $sign];
  58. } elseif ($type === 'jsapi') {
  59. $sign = $this->signBuild(join("\n", [$appid, $time, $nonceStr, "prepay_id={$prepayId}", '']));
  60. return ['appId' => $appid, 'timeStamp' => $time, 'nonceStr' => $nonceStr, 'package' => "prepay_id={$prepayId}", 'signType' => 'RSA', 'paySign' => $sign];
  61. } else {
  62. return $result;
  63. }
  64. }
  65. }
  66. /**
  67. * 支付订单查询
  68. * @param string $orderNo 订单单号
  69. * @return array
  70. * @throws \WeChat\Exceptions\InvalidResponseException
  71. */
  72. public function query($orderNo)
  73. {
  74. $pathinfo = "/v3/pay/transactions/out-trade-no/{$orderNo}";
  75. return $this->doRequest('GET', "{$pathinfo}?mchid={$this->config['mch_id']}", '', true);
  76. }
  77. /**
  78. * 支付通知
  79. * @return array
  80. * @throws \WeChat\Exceptions\InvalidDecryptException
  81. */
  82. public function notify(array $parameters = [])
  83. {
  84. if (empty($parameters)) {
  85. $body = file_get_contents('php://input');
  86. $data = json_decode($body, true);
  87. } else {
  88. $data = $parameters;
  89. }
  90. if (isset($data['resource'])) {
  91. $aes = new DecryptAes($this->config['mch_v3_key']);
  92. $data['result'] = $aes->decryptToString(
  93. $data['resource']['associated_data'],
  94. $data['resource']['nonce'],
  95. $data['resource']['ciphertext']
  96. );
  97. }
  98. return $data;
  99. }
  100. }