WechatH5Pay.php 8.0 KB

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