WechatH5Pay.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2020/4/16
  6. * Time: 13:55
  7. */
  8. namespace app\common\services;
  9. use app\common\exceptions\AppException;
  10. use app\common\facades\EasyWeChat;
  11. use app\common\helpers\Url;
  12. class WechatH5Pay extends Pay
  13. {
  14. protected $notify_url;
  15. protected $paySet;
  16. /**
  17. * WechatH5Pay constructor.
  18. * @throws AppException
  19. */
  20. public function __construct()
  21. {
  22. $this->notify_url = Url::shopSchemeUrl('payment/wechat/notifyH5.php');
  23. $this->paySet = \Setting::get('shop.pay');
  24. if (empty($this->paySet['weixin_mchid']) || empty($this->paySet['weixin_apisecret']) || empty($this->paySet['weixin_appid']) || empty($this->paySet['weixin_secret'])) {
  25. throw new AppException('没有设定支付参数');
  26. }
  27. }
  28. /**
  29. * 创建支付对象
  30. *
  31. * @param $pay
  32. * @return \EasyWeChat\Payment\Payment
  33. */
  34. public function getEasyWeChatApp($pay, $notify_url)
  35. {
  36. $options = [
  37. 'app_id' => $pay['weixin_appid'],
  38. 'secret' => $pay['weixin_secret'],
  39. // payment
  40. 'payment' => [
  41. 'merchant_id' => $pay['weixin_mchid'],
  42. 'key' => $pay['weixin_apisecret'],
  43. 'cert_path' => $pay['weixin_cert'],
  44. 'key_path' => $pay['weixin_key'],
  45. 'notify_url' => $notify_url
  46. ]
  47. ];
  48. $app = EasyWeChat::payment($options);
  49. return $app;
  50. }
  51. /**
  52. * @param $data
  53. * @return mixed|void
  54. */
  55. public function doPay($data)
  56. {
  57. $op = '微信h5支付-订单号:' . $data['order_no'];
  58. $pay_order_model = $this->log($data['extra']['type'], '微信h5支付', $data['amount'], $op, $data['order_no'], Pay::ORDER_STATUS_NON, \YunShop::app()->getMemberId());
  59. if (empty(\YunShop::app()->getMemberId())) {
  60. throw new AppException('无法获取用户ID');
  61. }
  62. //$this->setParameter('appid', $this->paySet['weixin_appid']);
  63. //$this->setParameter('mch_id', $this->paySet['weixin_mchid']);
  64. $this->setParameter('sign_type', 'MD5');
  65. $this->setParameter('trade_type', 'MWEB'); //H5支付的交易类型为MWEB
  66. $this->setParameter('device_info', 'WEB');
  67. $this->setParameter('nonce_str', str_random(16));
  68. $this->setParameter('body', mb_substr($data['subject'], 0, 120));
  69. $this->setParameter('attach', \YunShop::app()->uniacid);
  70. $this->setParameter('out_trade_no', $data['order_no']);
  71. $this->setParameter('total_fee', $data['amount'] * 100); // 单位:分
  72. $this->setParameter('spbill_create_ip', self::getClientIP());
  73. $this->setParameter('notify_url', $this->notify_url);
  74. $this->setParameter('scene_info', json_encode($this->getSceneInfo(), JSON_UNESCAPED_UNICODE));
  75. //请求数据日志
  76. self::payRequestDataLog($data['order_no'], $pay_order_model->type,
  77. $pay_order_model->third_type, $this->getAllParameters());
  78. /**
  79. * @var $app Application
  80. */
  81. $payment = $this->getEasyWeChatApp($this->paySet, $this->notify_url);
  82. $result = $payment->order->unify($this->getAllParameters());
  83. \Log::debug('预下单', $result);
  84. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  85. $mweb_url = $result['mweb_url'];
  86. if ($mweb_url) {
  87. $trade = \Setting::get('shop.trade');
  88. if (!is_null($trade) && isset($trade['redirect_url']) && !empty($trade['redirect_url'])) {
  89. $mweb_url .= '&redirect_url='.urlencode($trade['redirect_url']);
  90. } else {
  91. $url = Url::absoluteApp('member');
  92. $mweb_url .= '&redirect_url='.urlencode($url);
  93. }
  94. }
  95. //mweb_url为拉起微信支付收银台的中间页面,可通过访问该url来拉起微信客户端,完成支付,mweb_url的有效期为5分钟。
  96. return ['mweb_url'=>$mweb_url];
  97. } elseif ($result['return_code'] == 'SUCCESS') {
  98. throw new AppException($result['err_code_des']);
  99. } else {
  100. throw new AppException($result['return_msg']);
  101. }
  102. return false;
  103. }
  104. public function getSceneInfo()
  105. {
  106. $a = [
  107. 'h5_info' => [
  108. 'type' => 'Wap',//场景类型
  109. 'wap_url' => request()->getSchemeAndHttpHost(), //WAP网站URL地
  110. 'wap_name' => '商城', //WAP网站名
  111. ],
  112. ];
  113. return $a;
  114. }
  115. /**
  116. * @param 订单号 $out_trade_no
  117. * @param 订单总金额 $totalmoney
  118. * @param 退款金额 $refundmoney
  119. * @return mixed|void
  120. */
  121. public function doRefund($out_trade_no, $totalmoney, $refundmoney)
  122. {
  123. $out_refund_no = $this->setUniacidNo(\YunShop::app()->uniacid);
  124. $op = '微信退款 订单号:' . $out_trade_no . '退款单号:' . $out_refund_no . '退款金额:' . $refundmoney;
  125. if (empty($out_trade_no)) {
  126. throw new AppException('参数错误');
  127. }
  128. $pay_order_model = $this->refundlog(Pay::PAY_TYPE_REFUND, '微信h5退款', $refundmoney, $op, $out_trade_no, Pay::ORDER_STATUS_NON, 0);
  129. /**
  130. * @var $app Application
  131. * @var $payment \EasyWeChat\Payment\Payment
  132. */
  133. $payment = $this->getEasyWeChatApp($this->paySet, '');
  134. try {
  135. $totalmoney = bcmul($totalmoney, 100,0);
  136. $refundmoney = bcmul($refundmoney, 100,0);
  137. $result = $payment->refund->byOutTradeNumber($out_trade_no, $out_refund_no, $totalmoney, $refundmoney);
  138. } catch (\Exception $e) {
  139. throw new AppException('微信接口错误:' . $e->getMessage());
  140. }
  141. //微信申请退款失败
  142. if (isset($result['result_code']) && strtoupper($result['result_code']) == 'FAIL') {
  143. \Log::debug('---微信退款申请错误---', $result);
  144. throw new AppException('微信退款申请错误:'.$result['err_code'] . '-' . $result['err_code_des']);
  145. }
  146. $this->payResponseDataLog($out_trade_no, '微信h5退款', json_encode($result));
  147. $status = $this->queryRefund($payment, $out_trade_no);
  148. \Log::debug('---微信h5退款状态---'.$status, $result);
  149. if ($status == 'PROCESSING' || $status == 'SUCCESS') {
  150. $this->changeOrderStatus($pay_order_model, Pay::ORDER_STATUS_COMPLETE, $result['transaction_id']);
  151. return true;
  152. } else {
  153. throw new AppException('微信接口错误:'.$result['return_msg'] . '-' . $result['err_code_des'] . '/' . $status);
  154. }
  155. }
  156. private function changeOrderStatus($model, $status, $trade_no)
  157. {
  158. $model->status = $status;
  159. $model->trade_no = $trade_no;
  160. $model->save();
  161. }
  162. /**
  163. * 订单退款查询
  164. * @param $payment
  165. * @param $out_trade_no
  166. * @return mixed
  167. */
  168. public function queryRefund($payment, $out_trade_no)
  169. {
  170. $result = $payment->refund->queryByOutTradeNumber($out_trade_no);
  171. foreach ($result as $key => $value) {
  172. if (preg_match('/refund_status_\d+/', $key)) {
  173. return $value;
  174. }
  175. }
  176. return 'fail';
  177. }
  178. /**
  179. * @param 提现者用户ID $member_id
  180. * @param 提现批次单号 $out_trade_no
  181. * @param 提现金额 $money
  182. * @param 提现说明 $desc
  183. * @param 只针对微信 $type
  184. * @return mixed|void
  185. */
  186. public function doWithdraw($member_id, $out_trade_no, $money, $desc, $type)
  187. {
  188. }
  189. public function buildRequestSign()
  190. {
  191. }
  192. }