| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace addons\YunfastPay\common\globals;
- use addons\YunfastPay\common\enums\YunfastPayEnum;
- use addons\YunfastPay\common\enums\YunfastPayTypeEnum;
- use addons\YunfastPay\common\models\PaymentTrade;
- use addons\YunfastPay\common\models\WithdrawLog;
- use addons\YunfastPay\common\services\AgentService;
- use addons\YunfastPay\common\services\PayService;
- use addons\YunfastPay\common\services\RefundService;
- use addons\YunfastPay\common\services\SettingService;
- use common\models\order\OrderRefund;
- use yii\helpers\Json;
- /**
- * 银典支付全局调用执行类
- * @package addons\YunfastPay\common\globals
- */
- class YunfastPay
- {
- /**
- * 全局获取提现方式
- *
- * @param array $params
- * [
- * mall_id => int,
- * ]
- * @return array
- */
- public static function getWithdrawWay($params = [])
- {
- if (empty($params['mall_id'])) return [];
- $service = new SettingService(['mall_id' => $params['mall_id']]);
- if ($service->isOpen() && $service->isOpenAgent()) {
- return [YunfastPayEnum::YUNFAST_PAY => YunfastPayEnum::YUNFAST_PAY_NAME];
- }
- return [];
- }
- /**
- * 执行代付
- *
- * @param array $params
- * [
- * ids => array 提现的IDs
- * mall_id => int
- * ]
- * @return array 返回已提交打款记录IDs
- */
- public static function execAgent($params = [])
- {
- if (empty($mall_id = $params['mall_id'])) return [];
- if (empty($ids = $params['ids'])) return [];
- // 查询记录
- $field = ['id', 'user_id', 'extra', 'order_no', 'actual_num'];
- $list = WithdrawLog::getListByIdsAndAgree($mall_id, $ids, $field);
- if (empty($list)) return [];
- // 全部修改为待打款
- WithdrawLog::statusToWaitingByIds($mall_id, array_column($list, 'id'));
- // 支付类
- $service = new AgentService(['mall_id' => $mall_id]);
- $success_ids = [];
- foreach ($list as $log) {
- $id = $service->agentPay($log);
- $id && $success_ids[] = $id;
- }
- return $success_ids;
- }
- /**
- * 获取支付方式
- *
- * @param array $params
- * [
- * mall_id => int
- * ]
- * @return array
- */
- public static function getPayment($params = [])
- {
- if (empty($params['mall_id'])) return [];
- if (empty($params['app_platform'])) return [];
- $app_platform = $params['app_platform'];
- $req_source = $params['req_source'] ?? 1;
- $service = new SettingService(['mall_id' => $params['mall_id']]);
- $setting = $service->getSetting();
- // 未开启支付插件
- if (!$service->isOpen()) return [];
- $payment_name = YunfastPayTypeEnum::YUNFAST_PAY;
- $payment_type = YunfastPayTypeEnum::YUNFAST_PAY_NUM;
- $current_payment = YunfastPayTypeEnum::currentPayment();
- $trade_type = YunfastPayTypeEnum::getTypeByPlatform($app_platform, $req_source);
- if (empty($trade_type)) return [];
- $enable_payment = [];
- foreach ($trade_type as $item) {
- // 微信
- if (in_array($item, YunfastPayTypeEnum::getWechatPay())) {
- $new_pay['name'] = $setting['wechat_trade_name'] ?? YunfastPayEnum::YUNFAST_PAY_NAME . '-' . '微信支付';
- $new_pay['img'] = str_replace($payment_name, 'wechat', $current_payment['img']);
- $new_pay['pay_type'] = $payment_type;
- $new_pay['trade_type'] = $item;
- $new_pay['action'] = YunfastPayTypeEnum::action($payment_type);
- $enable_payment[] = $new_pay;
- }
- // 阿里
- if (in_array($item, YunfastPayTypeEnum::getAliPay())) {
- $new_pay['name'] = $setting['alipay_trade_name'] ?? YunfastPayEnum::YUNFAST_PAY_NAME . '-' . '支付宝支付';
- $new_pay['img'] = str_replace($payment_name, 'alipay', $current_payment['img']);
- $new_pay['pay_type'] = $payment_type;
- $new_pay['trade_type'] = $item;
- $new_pay['action'] = YunfastPayTypeEnum::action($payment_type);
- $enable_payment[] = $new_pay;
- }
- }
- // 当前渠道是否开启支付
- if (!$service->isSelectChannel($req_source)) return [];
- // 获取当前开启的支付方式
- $enable_payment = $service->getOpenPayment($enable_payment);
- return $enable_payment;
- }
- /**
- * 去支付
- *
- * @param array $params
- * @return array
- * @throws \Exception
- */
- public static function toPay($params = [])
- {
- if (
- empty($params['mall_id']) ||
- empty($params['action']) ||
- empty($params['trade_type']) ||
- empty($params['trade']) ||
- !in_array($params['pay_type'], [YunfastPayTypeEnum::YUNFAST_PAY_NUM])
- ) {
- return [];
- }
- // 支付信息
- $trade = $params['trade'];
- $mall_id = $params['mall_id'];
- $trade_type = $params['trade_type'];
- $openid = $params['openid'];
- $order_type = $params['order_type'] ?? '';
- $order_arr = $params['order_no_arr'] ?? [];
- // 是否支持该支付方式
- if (!in_array($trade_type, YunfastPayTypeEnum::getConstants())) return [];
- // 支付
- $service = new PayService(['mall_id' => $mall_id]);
- // 当支付订单是收银台订单
- if (YunfastPayEnum::CASHIER_ORDER == $order_type) {
- $store_id = PaymentTrade::getStoreIdByCashierOrderNos($order_arr);
- $service->setStoreId($store_id);
- }
- $res = $service->orderPay($trade, $trade_type, $openid);
- if ($res['respCode'] == '0000') {
- // 获取进行支付的数据
- return $service->getPayData();
- } else {
- throw new \Exception($res['respMsg'] ?? '支付数据有误');
- }
- return [];
- }
- /**
- * 全局调用退款退款
- *
- * @param array $params
- * [
- * id => int
- * mall_id => int
- * refund => common\models\order\OrderRefund
- * ]
- * @return boolean|array
- */
- public static function refund(array $params)
- {
- // 退款参数
- $refund_params = $params['params'];
- // 是否是回调调用
- $is_notify = $params['is_notify'] ?? false;
- if ($is_notify == true) {
- // 回调模式直接往下执行
- return true;
- }
- if (empty($params['mall_id'])) return false;
- $refund_source = $params['refund_source'] ?? YunfastPayEnum::MALL_ORDER;
- if (!($params['refund'] instanceof OrderRefund)) return false;
- // 退款服务类
- $service = new RefundService(['mall_id' => $params['mall_id']]);
- // 判断订单是否属于插件 不属于插件返回false
- if (!$service->isAddonsOrder($params['refund'], $params['order_no'], $refund_source)) {
- return false;
- }
- // 已退款
- if ($service->isRefund($params['refund'], $refund_source)) {
- return [true, 'ok'];
- }
- // 执行退款
- if ($service->orderRefund($params['refund'], $refund_params, $params['order_no'], $refund_source) === false) {
- return [false, $service->getErrorMsg()];
- }
- return [true, 'ok'];
- }
- /**
- * 获取支付方式
- *
- * @param array $params
- * [
- * mall_id => int
- * ]
- * @return array
- */
- public static function getPaymentType($params = [])
- {
- if (empty($params['mall_id'])) return [];
- $service = new SettingService(['mall_id' => $params['mall_id']]);
- $setting = $service->getSetting();
- // 未开启支付插件
- if (!$service->isOpen()) return [];
- $enable_pay_type[] = [
- "name" => YunfastPayEnum::YUNFAST_PAY_NAME,
- "pay_type" => YunfastPayTypeEnum::YUNFAST_PAY_NUM
- ];
- return $enable_pay_type;
- }
- }
|