YunfastPay.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace addons\YunfastPay\common\globals;
  3. use addons\YunfastPay\common\enums\YunfastPayEnum;
  4. use addons\YunfastPay\common\enums\YunfastPayTypeEnum;
  5. use addons\YunfastPay\common\models\PaymentTrade;
  6. use addons\YunfastPay\common\models\WithdrawLog;
  7. use addons\YunfastPay\common\services\AgentService;
  8. use addons\YunfastPay\common\services\PayService;
  9. use addons\YunfastPay\common\services\RefundService;
  10. use addons\YunfastPay\common\services\SettingService;
  11. use common\models\order\OrderRefund;
  12. use yii\helpers\Json;
  13. /**
  14. * 银典支付全局调用执行类
  15. * @package addons\YunfastPay\common\globals
  16. */
  17. class YunfastPay
  18. {
  19. /**
  20. * 全局获取提现方式
  21. *
  22. * @param array $params
  23. * [
  24. * mall_id => int,
  25. * ]
  26. * @return array
  27. */
  28. public static function getWithdrawWay($params = [])
  29. {
  30. if (empty($params['mall_id'])) return [];
  31. $service = new SettingService(['mall_id' => $params['mall_id']]);
  32. if ($service->isOpen() && $service->isOpenAgent()) {
  33. return [YunfastPayEnum::YUNFAST_PAY => YunfastPayEnum::YUNFAST_PAY_NAME];
  34. }
  35. return [];
  36. }
  37. /**
  38. * 执行代付
  39. *
  40. * @param array $params
  41. * [
  42. * ids => array 提现的IDs
  43. * mall_id => int
  44. * ]
  45. * @return array 返回已提交打款记录IDs
  46. */
  47. public static function execAgent($params = [])
  48. {
  49. if (empty($mall_id = $params['mall_id'])) return [];
  50. if (empty($ids = $params['ids'])) return [];
  51. // 查询记录
  52. $field = ['id', 'user_id', 'extra', 'order_no', 'actual_num'];
  53. $list = WithdrawLog::getListByIdsAndAgree($mall_id, $ids, $field);
  54. if (empty($list)) return [];
  55. // 全部修改为待打款
  56. WithdrawLog::statusToWaitingByIds($mall_id, array_column($list, 'id'));
  57. // 支付类
  58. $service = new AgentService(['mall_id' => $mall_id]);
  59. $success_ids = [];
  60. foreach ($list as $log) {
  61. $id = $service->agentPay($log);
  62. $id && $success_ids[] = $id;
  63. }
  64. return $success_ids;
  65. }
  66. /**
  67. * 获取支付方式
  68. *
  69. * @param array $params
  70. * [
  71. * mall_id => int
  72. * ]
  73. * @return array
  74. */
  75. public static function getPayment($params = [])
  76. {
  77. if (empty($params['mall_id'])) return [];
  78. if (empty($params['app_platform'])) return [];
  79. $app_platform = $params['app_platform'];
  80. $req_source = $params['req_source'] ?? 1;
  81. $service = new SettingService(['mall_id' => $params['mall_id']]);
  82. $setting = $service->getSetting();
  83. // 未开启支付插件
  84. if (!$service->isOpen()) return [];
  85. $payment_name = YunfastPayTypeEnum::YUNFAST_PAY;
  86. $payment_type = YunfastPayTypeEnum::YUNFAST_PAY_NUM;
  87. $current_payment = YunfastPayTypeEnum::currentPayment();
  88. $trade_type = YunfastPayTypeEnum::getTypeByPlatform($app_platform, $req_source);
  89. if (empty($trade_type)) return [];
  90. $enable_payment = [];
  91. foreach ($trade_type as $item) {
  92. // 微信
  93. if (in_array($item, YunfastPayTypeEnum::getWechatPay())) {
  94. $new_pay['name'] = $setting['wechat_trade_name'] ?? YunfastPayEnum::YUNFAST_PAY_NAME . '-' . '微信支付';
  95. $new_pay['img'] = str_replace($payment_name, 'wechat', $current_payment['img']);
  96. $new_pay['pay_type'] = $payment_type;
  97. $new_pay['trade_type'] = $item;
  98. $new_pay['action'] = YunfastPayTypeEnum::action($payment_type);
  99. $enable_payment[] = $new_pay;
  100. }
  101. // 阿里
  102. if (in_array($item, YunfastPayTypeEnum::getAliPay())) {
  103. $new_pay['name'] = $setting['alipay_trade_name'] ?? YunfastPayEnum::YUNFAST_PAY_NAME . '-' . '支付宝支付';
  104. $new_pay['img'] = str_replace($payment_name, 'alipay', $current_payment['img']);
  105. $new_pay['pay_type'] = $payment_type;
  106. $new_pay['trade_type'] = $item;
  107. $new_pay['action'] = YunfastPayTypeEnum::action($payment_type);
  108. $enable_payment[] = $new_pay;
  109. }
  110. }
  111. // 当前渠道是否开启支付
  112. if (!$service->isSelectChannel($req_source)) return [];
  113. // 获取当前开启的支付方式
  114. $enable_payment = $service->getOpenPayment($enable_payment);
  115. return $enable_payment;
  116. }
  117. /**
  118. * 去支付
  119. *
  120. * @param array $params
  121. * @return array
  122. * @throws \Exception
  123. */
  124. public static function toPay($params = [])
  125. {
  126. if (
  127. empty($params['mall_id']) ||
  128. empty($params['action']) ||
  129. empty($params['trade_type']) ||
  130. empty($params['trade']) ||
  131. !in_array($params['pay_type'], [YunfastPayTypeEnum::YUNFAST_PAY_NUM])
  132. ) {
  133. return [];
  134. }
  135. // 支付信息
  136. $trade = $params['trade'];
  137. $mall_id = $params['mall_id'];
  138. $trade_type = $params['trade_type'];
  139. $openid = $params['openid'];
  140. $order_type = $params['order_type'] ?? '';
  141. $order_arr = $params['order_no_arr'] ?? [];
  142. // 是否支持该支付方式
  143. if (!in_array($trade_type, YunfastPayTypeEnum::getConstants())) return [];
  144. // 支付
  145. $service = new PayService(['mall_id' => $mall_id]);
  146. // 当支付订单是收银台订单
  147. if (YunfastPayEnum::CASHIER_ORDER == $order_type) {
  148. $store_id = PaymentTrade::getStoreIdByCashierOrderNos($order_arr);
  149. $service->setStoreId($store_id);
  150. }
  151. $res = $service->orderPay($trade, $trade_type, $openid);
  152. if ($res['respCode'] == '0000') {
  153. // 获取进行支付的数据
  154. return $service->getPayData();
  155. } else {
  156. throw new \Exception($res['respMsg'] ?? '支付数据有误');
  157. }
  158. return [];
  159. }
  160. /**
  161. * 全局调用退款退款
  162. *
  163. * @param array $params
  164. * [
  165. * id => int
  166. * mall_id => int
  167. * refund => common\models\order\OrderRefund
  168. * ]
  169. * @return boolean|array
  170. */
  171. public static function refund(array $params)
  172. {
  173. // 退款参数
  174. $refund_params = $params['params'];
  175. // 是否是回调调用
  176. $is_notify = $params['is_notify'] ?? false;
  177. if ($is_notify == true) {
  178. // 回调模式直接往下执行
  179. return true;
  180. }
  181. if (empty($params['mall_id'])) return false;
  182. $refund_source = $params['refund_source'] ?? YunfastPayEnum::MALL_ORDER;
  183. if (!($params['refund'] instanceof OrderRefund)) return false;
  184. // 退款服务类
  185. $service = new RefundService(['mall_id' => $params['mall_id']]);
  186. // 判断订单是否属于插件 不属于插件返回false
  187. if (!$service->isAddonsOrder($params['refund'], $params['order_no'], $refund_source)) {
  188. return false;
  189. }
  190. // 已退款
  191. if ($service->isRefund($params['refund'], $refund_source)) {
  192. return [true, 'ok'];
  193. }
  194. // 执行退款
  195. if ($service->orderRefund($params['refund'], $refund_params, $params['order_no'], $refund_source) === false) {
  196. return [false, $service->getErrorMsg()];
  197. }
  198. return [true, 'ok'];
  199. }
  200. /**
  201. * 获取支付方式
  202. *
  203. * @param array $params
  204. * [
  205. * mall_id => int
  206. * ]
  207. * @return array
  208. */
  209. public static function getPaymentType($params = [])
  210. {
  211. if (empty($params['mall_id'])) return [];
  212. $service = new SettingService(['mall_id' => $params['mall_id']]);
  213. $setting = $service->getSetting();
  214. // 未开启支付插件
  215. if (!$service->isOpen()) return [];
  216. $enable_pay_type[] = [
  217. "name" => YunfastPayEnum::YUNFAST_PAY_NAME,
  218. "pay_type" => YunfastPayTypeEnum::YUNFAST_PAY_NUM
  219. ];
  220. return $enable_pay_type;
  221. }
  222. }