WechatNativePay.php 6.4 KB

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