Order.php 3.8 KB

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