Order.php 5.4 KB

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