AlipayJsapiPayService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/17
  6. * Time: 下午12:01
  7. */
  8. namespace app\common\services\alipay;
  9. use app\common\exceptions\AppException;
  10. use app\common\helpers\Url;
  11. use app\common\models\OrderPay;
  12. use app\common\models\PayOrder;
  13. use app\common\models\PayType;
  14. use app\common\services\Pay;
  15. use app\common\services\Utils;
  16. use Yunshop\StoreCashier\store\common\service\RefreshToken;
  17. use Yunshop\StoreCashier\store\models\StoreAlipaySetting;
  18. class AlipayJsapiPayService extends Pay
  19. {
  20. /**
  21. * 订单支付
  22. * @param $data
  23. * @param $payType
  24. * @return mixed
  25. * @throws \Exception
  26. */
  27. public function doPay($data = [], $payType = 49)
  28. {
  29. $op = "支付宝订单支付 订单号:" . $data['order_no'];
  30. $pay_type_name = PayType::get_pay_type_name($payType);
  31. $this->log($data['extra']['type'], $pay_type_name, $data['amount'], $op, $data['order_no'], Pay::ORDER_STATUS_NON, \YunShop::app()->getMemberId());
  32. $alipay_set = \Setting::get('shop.alipay_set');
  33. $config = [
  34. 'app_id' => $alipay_set['app_id'],
  35. 'ali_public_key' => $alipay_set['alipay_public_key'],
  36. 'private_key' => $alipay_set['merchant_private_key'],
  37. 'notify_url' => Url::shopSchemeUrl('payment/alipay/jsapiNotifyUrl.php'),
  38. 'return_url' => Url::shopSchemeUrl('payment/alipay/returnUrl.php'),
  39. 'app_auth_token' => ''
  40. ];
  41. $order = [
  42. 'body' => \YunShop::app()->uniacid,
  43. 'subject' => mb_substr($data['subject'], 0, 256),
  44. 'out_trade_no' => $data['order_no'] . '_' . \YunShop::app()->uniacid . '_' . $this->getRoyalty($alipay_set),
  45. 'total_amount' => $data['amount'],
  46. 'http_method' => 'GET'
  47. ];
  48. if ($this->getRoyalty($alipay_set)) {
  49. $order['extend_params'] = ['royalty_freeze' => true];
  50. }
  51. if (!$alipay_set['app_type']) {
  52. //第三方应用授权令牌,商户授权系统商开发模式下使用
  53. $config['app_auth_token'] = $this->getAuthToken($alipay_set);
  54. $order['sys_service_provider_id'] = $alipay_set['pid'];
  55. }
  56. return \Yansongda\Pay\Pay::alipay($config)->wap($order)->getTargetUrl();
  57. }
  58. public function doRefund($out_trade_no, $totalmoney, $refundmoney = '0')
  59. {
  60. if ($refundmoney <= 0) {
  61. throw new AppException('退款金额不能小于等于0');
  62. }
  63. if (app('plugins')->isEnabled('store-cashier')) {
  64. $orderPay = OrderPay::where('pay_sn', $out_trade_no)->first();
  65. $storeOrder = \Yunshop\StoreCashier\common\models\StoreOrder::where('order_id', $orderPay->orders->first()->id)->first();
  66. if (!$storeOrder) {
  67. throw new AppException('请确认订单是否为门店订单');
  68. }
  69. } else {
  70. throw new AppException('未开启门店收银');
  71. }
  72. $out_refund_no = $this->setUniacidNo(\YunShop::app()->uniacid);
  73. $op = '支付宝退款 订单号:' . $out_trade_no . '退款单号:' . $out_refund_no . '退款总金额:' . $totalmoney;
  74. if (empty($out_trade_no)) {
  75. throw new AppException('参数错误');
  76. }
  77. $pay_type_id = OrderPay::get_paysn_by_pay_type_id($out_trade_no);
  78. $pay_type_name = PayType::get_pay_type_name($pay_type_id);
  79. $refund_order = $this->refundlog(Pay::PAY_TYPE_REFUND, $pay_type_name, $totalmoney, $op, $out_trade_no, Pay::ORDER_STATUS_NON, 0);
  80. //支付宝交易单号
  81. $pay_order_model = PayOrder::getPayOrderInfo($out_trade_no)->first();
  82. if (empty($pay_order_model)) {
  83. return false;
  84. }
  85. $refund_data = array(
  86. 'out_trade_no' => $pay_order_model->out_order_no,
  87. 'trade_no' => $pay_order_model->trade_no,
  88. 'refund_amount' => $refundmoney,
  89. 'refund_reason' => '正常退款',
  90. 'out_request_no' => $out_refund_no
  91. );
  92. // 获取人脸支付插件设置
  93. $alipaySet = \Setting::get('shop.alipay_set');
  94. if (!$alipaySet) {
  95. throw new AppException('请检查人脸支付设置');
  96. }
  97. $alipayStoreSet = StoreAlipaySetting::uniacid()->where('store_id', $storeOrder->store_id)->first();
  98. if (!$alipayStoreSet) {
  99. throw new AppException('请检查门店人脸支付设置');
  100. }
  101. Utils::dataDecrypt($set);
  102. $config = [
  103. 'app_id' => $alipaySet['app_id'],
  104. 'ali_public_key' => $alipaySet['alipay_public_key'],
  105. 'private_key' => $alipaySet['merchant_private_key'],
  106. 'app_auth_token' => $alipayStoreSet['app_auth_token'],
  107. ];
  108. $result = \Yansongda\Pay\Pay::alipay($config)->refund($refund_data);
  109. if (!empty($result) && $result['code'] == '10000') {
  110. $refund_order->status = Pay::ORDER_STATUS_COMPLETE;
  111. $refund_order->trade_no = $result['trade_no'];
  112. $refund_order->save();
  113. $this->payResponseDataLog($out_trade_no, '支付宝(服务商)wap退款', json_encode($result));
  114. return true;
  115. }
  116. \Log::debug('---alipay-app---', [$refund_data, $result]);
  117. throw new AppException($result['msg'] . '-' . $result['sub_msg']);
  118. }
  119. public function doWithdraw($member_id, $out_trade_no, $money, $desc = '', $type = 1)
  120. {
  121. return false;
  122. }
  123. private function getRoyalty($set)
  124. {
  125. $result = 0;
  126. if ($set['royalty']) {
  127. $sub_set = StoreAlipaySetting::where('store_id', request()->store_id)->first();
  128. if ($sub_set->royalty && !$sub_set->no_authorized_royalty) {
  129. $result = 1;
  130. }
  131. }
  132. return $result;
  133. }
  134. /**
  135. * @return mixed
  136. * @throws \Exception
  137. */
  138. private function getAuthToken($set)
  139. {
  140. $app_auth_token = '';
  141. if (!$set['app_type']) {
  142. $storeAlipaySetting = StoreAlipaySetting::uniacid()->where('store_id', request()->store_id)->first();
  143. if (!$storeAlipaySetting) {
  144. throw new AppException('门店未授权支付宝');
  145. }
  146. if ($storeAlipaySetting->expires_in < time()) {
  147. $storeAlipaySetting = RefreshToken::refreshToken();
  148. }
  149. $app_auth_token = $storeAlipaySetting->app_auth_token;
  150. }
  151. return $app_auth_token;
  152. }
  153. public function buildRequestSign()
  154. {
  155. // TODO: Implement buildRequestSign() method.
  156. }
  157. }