Order.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WePay;
  16. use WeChat\Contracts\BasicWePay;
  17. use WeChat\Contracts\Tools;
  18. /**
  19. * 微信商户订单
  20. * Class Order
  21. * @package WePay
  22. */
  23. class Order extends BasicWePay
  24. {
  25. /**
  26. * 统一下单
  27. * @param array $options
  28. * @return array
  29. * @throws \WeChat\Exceptions\InvalidResponseException
  30. * @throws \WeChat\Exceptions\LocalCacheException
  31. */
  32. public function create(array $options)
  33. {
  34. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  35. return $this->callPostApi($url, $options, false, 'MD5');
  36. }
  37. /**
  38. * 刷卡支付
  39. * @param array $options
  40. * @return array
  41. * @throws \WeChat\Exceptions\InvalidResponseException
  42. * @throws \WeChat\Exceptions\LocalCacheException
  43. */
  44. public function micropay(array $options)
  45. {
  46. $url = 'https://api.mch.weixin.qq.com/pay/micropay';
  47. return $this->callPostApi($url, $options, false, 'MD5');
  48. }
  49. /**
  50. * 查询订单
  51. * @param array $options
  52. * @return array
  53. * @throws \WeChat\Exceptions\InvalidResponseException
  54. * @throws \WeChat\Exceptions\LocalCacheException
  55. */
  56. public function query(array $options)
  57. {
  58. $url = 'https://api.mch.weixin.qq.com/pay/orderquery';
  59. return $this->callPostApi($url, $options);
  60. }
  61. /**
  62. * 关闭订单
  63. * @param string $outTradeNo 商户订单号
  64. * @return array
  65. * @throws \WeChat\Exceptions\InvalidResponseException
  66. * @throws \WeChat\Exceptions\LocalCacheException
  67. */
  68. public function close($outTradeNo)
  69. {
  70. $url = 'https://api.mch.weixin.qq.com/pay/closeorder';
  71. return $this->callPostApi($url, ['out_trade_no' => $outTradeNo]);
  72. }
  73. /**
  74. * 创建JsApi及H5支付参数
  75. * @param string $prepayId 统一下单预支付码
  76. * @return array
  77. */
  78. public function jsapiParams($prepayId)
  79. {
  80. $option = [];
  81. $option["appId"] = $this->config->get('appid');
  82. $option["timeStamp"] = (string)time();
  83. $option["nonceStr"] = Tools::createNoncestr();
  84. $option["package"] = "prepay_id={$prepayId}";
  85. $option["signType"] = "MD5";
  86. $option["paySign"] = $this->getPaySign($option, 'MD5');
  87. $option['timestamp'] = $option['timeStamp'];
  88. return $option;
  89. }
  90. /**
  91. * 获取支付规则二维码
  92. * @param string $productId 商户定义的商品id或者订单号
  93. * @return string
  94. */
  95. public function qrcParams($productId)
  96. {
  97. $data = [
  98. 'appid' => $this->config->get('appid'),
  99. 'mch_id' => $this->config->get('mch_id'),
  100. 'time_stamp' => (string)time(),
  101. 'nonce_str' => Tools::createNoncestr(),
  102. 'product_id' => (string)$productId,
  103. ];
  104. $data['sign'] = $this->getPaySign($data, 'MD5');
  105. return "weixin://wxpay/bizpayurl?" . http_build_query($data);
  106. }
  107. /**
  108. * 获取微信App支付秘需参数
  109. * @param string $prepayId 统一下单预支付码
  110. * @return array
  111. */
  112. public function appParams($prepayId)
  113. {
  114. $data = [
  115. 'appid' => $this->config->get('appid'),
  116. 'partnerid' => $this->config->get('mch_id'),
  117. 'prepayid' => (string)$prepayId,
  118. 'package' => 'Sign=WXPay',
  119. 'timestamp' => (string)time(),
  120. 'noncestr' => Tools::createNoncestr(),
  121. ];
  122. $data['sign'] = $this->getPaySign($data, 'MD5');
  123. return $data;
  124. }
  125. /**
  126. * 刷卡支付 撤销订单
  127. * @param array $options
  128. * @return array
  129. * @throws \WeChat\Exceptions\InvalidResponseException
  130. * @throws \WeChat\Exceptions\LocalCacheException
  131. */
  132. public function reverse(array $options)
  133. {
  134. $url = 'https://api.mch.weixin.qq.com/secapi/pay/reverse';
  135. return $this->callPostApi($url, $options, true);
  136. }
  137. /**
  138. * 刷卡支付 授权码查询openid
  139. * @param string $authCode 扫码支付授权码,设备读取用户微信中的条码或者二维码信息
  140. * @return array
  141. * @throws \WeChat\Exceptions\InvalidResponseException
  142. * @throws \WeChat\Exceptions\LocalCacheException
  143. */
  144. public function queryAuthCode($authCode)
  145. {
  146. $url = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid';
  147. return $this->callPostApi($url, ['auth_code' => $authCode], false, 'MD5', false);
  148. }
  149. /**
  150. * 刷卡支付 交易保障
  151. * @param array $options
  152. * @return array
  153. * @throws \WeChat\Exceptions\InvalidResponseException
  154. * @throws \WeChat\Exceptions\LocalCacheException
  155. */
  156. public function report(array $options)
  157. {
  158. $url = 'https://api.mch.weixin.qq.com/payitil/report';
  159. return $this->callPostApi($url, $options);
  160. }
  161. }