WechatNativePay.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/12/30
  6. * Time: 17:42
  7. */
  8. namespace app\common\services;
  9. use app\common\exceptions\AppException;
  10. use app\common\helpers\Url;
  11. use app\common\facades\EasyWeChat;
  12. class WechatNativePay 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/notifyPc.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 = '微信扫码支付-订单号:' . $data['order_no'];
  58. $pay_order_model = $this->log($data['extra']['type'], '微信扫码支付', $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', 'NATIVE'); //NATIVE -Native支付
  66. $this->setParameter('product_id', $data['order_no']);//trade_type=NATIVE时,此参数必传。此参数为二维码中包含的商品ID,商户自行定义。
  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. $payment = $this->getEasyWeChatApp($this->paySet, $this->notify_url);
  79. $result = $payment->order->unify($this->getAllParameters());
  80. \Log::debug('预下单', $result);
  81. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  82. //code_ur 此url用于生成支付二维码,然后提供给用户进行扫码支付,有效期为2小时
  83. return ['code_url'=>$result['code_url']];
  84. } elseif ($result['return_code'] == 'SUCCESS') {
  85. throw new AppException($result['err_code_des']);
  86. } else {
  87. throw new AppException($result['return_msg']);
  88. }
  89. return false;
  90. }
  91. public function getSceneInfo()
  92. {
  93. $a = [
  94. 'store_info' => [
  95. 'id' => '0',//门店id
  96. 'name' => '商城', //门店名称
  97. 'area_code' => '商城', //门店行政区划码
  98. 'address' => '商城', //门店详细地址
  99. ],
  100. ];
  101. return $a;
  102. }
  103. /**
  104. * @param 订单号 $out_trade_no
  105. * @param 订单总金额 $totalmoney
  106. * @param 退款金额 $refundmoney
  107. * @return mixed|void
  108. */
  109. public function doRefund($out_trade_no, $totalmoney, $refundmoney)
  110. {
  111. $out_refund_no = $this->setUniacidNo(\YunShop::app()->uniacid);
  112. $op = '微信退款 订单号:' . $out_trade_no . '退款单号:' . $out_refund_no . '退款金额:' . $refundmoney;
  113. if (empty($out_trade_no)) {
  114. throw new AppException('参数错误');
  115. }
  116. $pay_order_model = $this->refundlog(Pay::PAY_TYPE_REFUND, '微信扫码支付退款', $refundmoney, $op, $out_trade_no, Pay::ORDER_STATUS_NON, 0);
  117. $payment = $this->getEasyWeChatApp($this->paySet, $this->notify_url);
  118. try {
  119. $totalmoney = bcmul($totalmoney, 100,0);
  120. $refundmoney = bcmul($refundmoney, 100,0);
  121. $result = $payment->refund->byOutTradeNumber($out_trade_no, $out_refund_no, $totalmoney, $refundmoney);
  122. } catch (\Exception $e) {
  123. throw new AppException('微信接口错误:' . $e->getMessage());
  124. }
  125. //微信申请退款失败
  126. if (isset($result['result_code']) && strtoupper($result['result_code']) == 'FAIL') {
  127. \Log::debug('---微信退款申请错误---', $result);
  128. throw new AppException('微信退款申请错误:'.$result['err_code'] . '-' . $result['err_code_des']);
  129. }
  130. $this->payResponseDataLog($out_trade_no, '微信扫码支付退款', json_encode($result));
  131. $status = $this->queryRefund($payment, $out_trade_no);
  132. \Log::debug('---微信扫码支付退款状态---'.$status, $result);
  133. if ($status == 'PROCESSING' || $status == 'SUCCESS') {
  134. $this->changeOrderStatus($pay_order_model, Pay::ORDER_STATUS_COMPLETE, $result->transaction_id);
  135. return true;
  136. } else {
  137. throw new AppException('微信接口错误:'.$result->return_msg . '-' . $result->err_code_des . '/' . $status);
  138. }
  139. }
  140. private function changeOrderStatus($model, $status, $trade_no)
  141. {
  142. $model->status = $status;
  143. $model->trade_no = $trade_no;
  144. $model->save();
  145. }
  146. /**
  147. * 订单退款查询
  148. * @param $payment
  149. * @param $out_trade_no
  150. * @return mixed
  151. */
  152. public function queryRefund($payment, $out_trade_no)
  153. {
  154. $result = $payment->refund->queryByOutTradeNumber($out_trade_no);
  155. foreach ($result as $key => $value) {
  156. if (preg_match('/refund_status_\d+/', $key)) {
  157. return $value;
  158. }
  159. }
  160. return 'fail';
  161. }
  162. /**
  163. * @param 提现者用户ID $member_id
  164. * @param 提现批次单号 $out_trade_no
  165. * @param 提现金额 $money
  166. * @param 提现说明 $desc
  167. * @param 只针对微信 $type
  168. * @return mixed|void
  169. */
  170. public function doWithdraw($member_id, $out_trade_no, $money, $desc, $type)
  171. {
  172. }
  173. public function buildRequestSign()
  174. {
  175. }
  176. }