WechatScanPayService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2019/6/3
  5. * Time: 下午3:10
  6. */
  7. namespace app\common\services\wechat;
  8. use app\common\exceptions\AppException;
  9. use app\common\helpers\Url;
  10. use app\common\models\McMappingFans;
  11. use app\common\models\PayOrder;
  12. use app\common\services\Pay;
  13. use app\common\models\OrderPay;
  14. use app\common\models\PayType;
  15. use app\common\services\wechat\lib\WxPayApi;
  16. use app\common\services\wechat\lib\WxPayConfig;
  17. use app\common\services\wechat\lib\WxPayMicroPay;
  18. use app\common\services\wechat\lib\WxPayOrderQuery;
  19. use app\common\services\wechat\lib\WxPayRefund;
  20. class WechatScanPayService extends Pay
  21. {
  22. public $set = null;
  23. public $config = null;
  24. public function __construct()
  25. {
  26. $this->config = new WxPayConfig();
  27. $this->set = $set = \Setting::get('shop.wechat_set');
  28. }
  29. /**
  30. * 支付
  31. * @param array $data
  32. * @return mixed|string
  33. * @throws AppException
  34. * @throws \app\common\services\wechat\lib\WxPayException
  35. */
  36. public function doPay($data = [])
  37. {
  38. // if (\YunShop::request()->type != 9) {
  39. // throw new AppException('不是商家APP 微信扫码支付不可用');
  40. // }
  41. $pay_name = $data['pay_type'] == 'wechat_scan' ? '微信扫码支付' : '微信人脸支付';
  42. $op = $pay_name.' 订单号:' . $data['order_no'];
  43. $pay_order_model = $this->log(1, $pay_name, $data['amount'] / 100, $op, $data['order_no'], Pay::ORDER_STATUS_NON, $this->getMemberId());
  44. /* 支付请求对象 */
  45. $wxPay = new WxPayMicroPay();
  46. //设置商品或支付单简要描述
  47. $wxPay->SetBody($data['body']);
  48. //设置商家数据包,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
  49. $wxPay->SetAttach($data['extra']);
  50. //设置商户系统内部的订单号
  51. $wxPay->SetOut_trade_no($data['pay_sn']);
  52. //设置订单总金额
  53. $wxPay->SetTotal_fee($data['amount']);
  54. //设置扫码支付授权码
  55. $wxPay->SetAuth_code($data['auth_code']);
  56. $response = WxPayApi::micropay(new WxPayConfig(), $wxPay);
  57. if ($response['result_code'] != 'SUCCESS') {
  58. // todo 订单取消
  59. throw new AppException('微信支付失败:'.$response['err_code_des']);
  60. }
  61. //更新openid
  62. $response = $this->setOpenId($response);
  63. $response['profit_sharing'] = (new WxPayConfig())->GetProfitSharing() == 'Y' ?1:0;
  64. //请求数据日志
  65. self::payRequestDataLog($data['order_no'], $pay_order_model->type,
  66. $pay_order_model->third_type, json_encode($response));
  67. return $response;
  68. }
  69. /**
  70. * 退款
  71. */
  72. public function doRefund($out_trade_no, $totalmoney, $refundmoney)
  73. {
  74. $totalmoney = intval($totalmoney * 100);
  75. $refundmoney = intval($refundmoney * 100);
  76. $this->getSubMchId($out_trade_no);
  77. $wxPayApi = new WxPayApi;
  78. $config = new WxPayConfig;
  79. $wxPayRefund = new WxPayRefund;
  80. $wxPayRefund->SetOut_trade_no($out_trade_no);
  81. $wxPayRefund->SetTotal_fee($totalmoney);
  82. $wxPayRefund->SetRefund_fee($refundmoney);
  83. $out_refund_no = $this->setUniacidNo(\YunShop::app()->uniacid);
  84. $wxPayRefund->SetOut_refund_no($out_refund_no);
  85. if (!$config->GetSubMerchantId()) {
  86. throw new AppException('请先配置门店子商户参数');
  87. }
  88. $wxPayRefund->SetSubMchId($config->GetSubMerchantId());
  89. $pay_type_id = OrderPay::get_paysn_by_pay_type_id($out_trade_no);
  90. $pay_type_name = PayType::get_pay_type_name($pay_type_id);
  91. $op = '微信退款 订单号:' . $out_trade_no . '退款单号:' . $out_refund_no . '退款总金额:' . $totalmoney;
  92. $payOrderModel = $this->refundlog(Pay::PAY_TYPE_REFUND, $pay_type_name, $refundmoney, $op, $out_trade_no, Pay::ORDER_STATUS_NON, 0);
  93. $result = $wxPayApi::refund($config, $wxPayRefund);
  94. \Log::debug('微信扫码退款记录', $result);
  95. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  96. $payOrderModel->status = Pay::ORDER_STATUS_COMPLETE;
  97. $payOrderModel->trade_no = $result['transaction_id'];
  98. $payOrderModel->save();
  99. return true;
  100. } elseif ($result['return_code'] == 'SUCCESS') {
  101. throw new AppException($result['err_code_des']);
  102. } else {
  103. throw new AppException($result['return_msg']);
  104. }
  105. }
  106. /**
  107. * 提现
  108. */
  109. public function doWithdraw($member_id, $out_trade_no, $money, $desc, $type)
  110. {
  111. }
  112. /**
  113. * 支付回调操作
  114. *
  115. * @param $data
  116. */
  117. public function payResult($data)
  118. {
  119. }
  120. public function getMemberId()
  121. {
  122. return \YunShop::app()->getMemberId() ? : 0;
  123. }
  124. /**
  125. * 构造签名
  126. *
  127. * @return mixed
  128. */
  129. function buildRequestSign()
  130. {
  131. // TODO: Implement buildRequestSign() method.
  132. }
  133. /**
  134. *获取带参数的请求URL
  135. */
  136. function getRequestURL() {
  137. $this->buildRequestSign();
  138. $reqPar =json_encode($this->parameters);
  139. \Log::debug('-----请求参数----', $reqPar);
  140. $requestURL = $this->getGateURL() . "?data=".base64_encode($reqPar);
  141. return $requestURL;
  142. }
  143. function setOpenId($data)
  144. {
  145. if (!$this->set['is_independent'] && $this->set['sub_appid'] && $this->set['sub_mchid']) {
  146. $data['openid'] = $data['sub_openid'];
  147. }
  148. return $data;
  149. }
  150. // 获取门店子商户号
  151. private function getSubMchId($outTradeNo)
  152. {
  153. if (app('plugins')->isEnabled('store-cashier')) {
  154. $orderPay = OrderPay::where('pay_sn', $outTradeNo)->first();
  155. $storeId = \Yunshop\StoreCashier\common\models\StoreOrder::where('order_id', $orderPay->orders->first()->id)->value('store_id');
  156. if ($storeId) {
  157. request()->offsetSet('store_id', $storeId);
  158. }
  159. }
  160. if (!isset($storeId)) {
  161. throw new AppException('请确认订单是否属于门店订单');
  162. }
  163. }
  164. }