where(['store_id' => $store_id ?: get_store_id()])->select('id, auth_token, userid')->orderBy('id desc')->asArray()->one(); if (empty($store_mini)) { $store_mini = StoreAliMini::find()->where(['and', ['<>', 'auth_token', ''], ['<>', 'userid', '']])->orderBy('id desc')->asArray()->one(); if (empty($store_mini)) { return [ 'code' => 1, 'msg' => "获取不到商户的支付信息=-=" ]; } } self::$store_mini = $store_mini; } // if ($is_app) { // $store_token = StoreMiniToken::find()->where(['batch_status' => [2, 6], 'store_id' => get_store_id()])->select('app_auth_token')->asArray()->one(); // $store_mini['auth_token'] = $store_token['app_auth_token']; // } $alipay_appid = Option::get('alipay_appid', '0', 'saas')['value']; $alipay_public_key = Option::get('alipay_public_key', '0', 'saas')['value']; $alipay_app_private_key = Option::get('alipay_app_private_key', '0', 'saas')['value']; //三方平台信息 $config = [ 'app_id' => $alipay_appid, 'notify_url' => pay_notify_url(self::$notify_url), 'return_url' => $return_url, 'private_key' => $alipay_app_private_key, 'log' => [ // optional 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log', 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug 'type' => 'single', // optional, 可选 daily. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天 ], 'ali_public_key' => $alipay_public_key, 'app_auth_token' => $store_mini['auth_token'] // 'app_cert_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/app_cert_public_key.crt', // 应用公钥证书路径 // 'ali_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_cert_public_key.crt', // 支付宝公钥证书路径 // 'alipay_root_cert' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_root_cert.crt', // 支付宝根证书路径 // 'mode' => 'dev' // optional,设置此参数,将进入沙箱模式 ]; if ((int)$store_id === 0) { $keys = [ 'alipay_platform' ]; $data = Option::get($keys, 0, 'saas'); if(isset($data[0]['value'])) { $alipay_config = json_decode($data[0]['value'],true); $alipay_appid = $alipay_config['app_id']; $alipay_public_key = $alipay_config['alipay_public_key']; $alipay_app_private_key = $alipay_config['app_private_key']; self::$seller_id = $alipay_config['user_id']; } $config = [ 'app_id' => $alipay_appid, 'notify_url' => pay_notify_url(self::$notify_url), 'return_url' => $return_url, 'private_key' => $alipay_app_private_key, 'log' => [ // optional 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log', 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug 'type' => 'single', // optional, 可选 daily. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天 ], 'ali_public_key' => $alipay_public_key, // 'app_auth_token' => $store_mini['auth_token'] // 'app_cert_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/app_cert_public_key.crt', // 应用公钥证书路径 // 'ali_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_cert_public_key.crt', // 支付宝公钥证书路径 // 'alipay_root_cert' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_root_cert.crt', // 支付宝根证书路径 // 'mode' => 'dev' // optional,设置此参数,将进入沙箱模式 ]; } self::$alipay = Pay::alipay($config); } /** * 静态方法调用construct方法 * @param $return_url */ public static function init($return_url = '', $is_app = 'false', $store_id = 0) { new self($return_url, $is_app, $store_id); } /** * 扫码支付 * @param $order * @param $subject * @param $return_url * @return Collection | array */ public static function scan($order, $subject, $return_url = '', $user_id = null, $store_id = 0) { self::init($return_url, 0, $store_id); $store_mini = self::$store_mini; if ((int)$store_id !== 0) { $seller_id = $store_mini['userid']; } else { $seller_id = self::$seller_id; } /** 逻辑开始 **/ $order = [ 'body' => $subject, 'out_trade_no' => $order->order_no, 'total_amount' => $order->pay_price, 'subject' => $subject, 'buyer_id' => $user_id, 'seller_id' => $seller_id ]; try { $res = self::$alipay->mini($order); if ($res && $res->code === '10000' && $res->msg === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res->toArray() ]; } return [ 'code' => $res->code, 'msg' => $res->msg, ]; } catch (GatewayException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidSignException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidConfigException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 转账 * @param $order * @param $mail * @param $return_url * @return Collection | array */ public static function transfer($order, $mail, $return_url = '') { if (empty($mail)) { return [ 'code' => 1, 'msg' => 'Params are wrong' ]; } self::init($return_url); /** 逻辑开始 **/ $order = [ 'out_biz_no' => $order->order_no, 'total_amount' => $order->pay_price, 'product_code' => 'TRANS_ACCOUNT_NO_PWD', 'payee_info' => [ 'identity' => $mail, 'identity_type' => 'ALIPAY_LOGON_ID', ], ]; try { $res = self::$alipay->transfer($order); if ($res && $res->code === '10000' && $res->msg === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res->toArray() ]; } return [ 'code' => $res->code, 'msg' => $res->msg, ]; } catch (GatewayException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidSignException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidConfigException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * app支付 * @param $order * @param User $user * @param $subject * @param $balance_price * @param $return_url * @return Response | false | string | array */ public static function app($order, $subject, $user, $balance_price = 0, $return_url = '') { self::init($return_url, true); /** 逻辑开始 **/ $order = [ 'body' => $subject, 'out_trade_no' => $order->order_no, 'total_amount' => $balance_price > 0 ? ($order->pay_price - $balance_price) : $order->pay_price, 'subject' => $subject, 'extend_params' => [ 'royalty_freeze' => 'true' ] ]; \Yii::error($order); try { $res = self::$alipay->app($order)->getContent(); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } return [ 'code' => 0, 'msg' => 'success', 'data' => $res, 'res' => $res ]; } /** * 批量转账 * @param $params * @return Collection | array */ public static function batchTransfer($params) { self::init(''); /** 逻辑开始 **/ $order = [ 'out_batch_no' => $params['out_batch_no'], 'product_code' => 'BATCH_API_TO_ACC_NO_PWD', 'biz_scene' => 'MESSAGE_BATCH_PAY', 'total_trans_amount' => $params['total_trans_amount'], 'total_count' => 1, 'trans_order_list' => [ [ 'out_biz_no' => $params['out_batch_no'], 'trans_amount' => $params['total_trans_amount'], 'payee_info' => [ 'identity' => $params['account'], 'identity_type' => 'ALIPAY_LOGON_ID', 'name' => $params['name'] ], 'remark' => $params['title'] ], ], 'order_title' => $params['title'], 'remark' => '转账', ]; try { $res = self::$alipay->batchtransfer($order); \Yii::error($res); if ($res && $res->code === '10000' && $res->msg === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res->toArray() ]; } return [ 'code' => $res->code, 'msg' => $res->msg, ]; } catch (GatewayException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidSignException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidConfigException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 退款 * @param $order * @param $return_url * @param $refund_fee * @return Collection | array */ public static function refund($order, $refund_fee, $return_url = '') { self::init($return_url); /** 逻辑开始 **/ $data = [ 'out_trade_no' => $order->order_no, 'refund_amount' => $refund_fee, ]; try { $res = self::$alipay->refund($data); if ($res && $res->code === '10000' && $res->msg === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res->toArray() ]; } return [ 'code' => $res->code, 'msg' => $res->msg, ]; } catch (GatewayException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidSignException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidConfigException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序支付 * @param $order * @param $subject * @param User $user * @param $return_url * @param $is_food * @return Collection | array */ public static function mini($order, $subject, $user, $return_url = '', $type = '', $total_pay_price = 0, $balance_price = 0) { self::init($return_url); if (!$order || ($type != OrderNo::ORDER_UNION && empty($subject)) || !$user) { return [ 'code' => 1, 'msg' => '参数有误' ]; } if ($type == OrderNo::ORDER_UNION) { $order = [ 'body' => '订单合并支付', 'out_trade_no' => OrderNo::getOrderNo(OrderNo::ORDER_UNION), 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price, 'subject' => count($order) . '笔订单合并支付', 'buyer_id' => $user->ali_user_id ]; $order_union = new OrderUnion(); $order_union->store_id = get_store_id(); $order_union->user_id = get_saas_user()->id; $order_union->order_no = $order['out_trade_no']; $order_union->price = $order['total_amount']; $order_union->is_pay = 0; $order_union->created_at = time(); $order_union->is_delete = 0; $order_id_list = []; foreach ($order as $value) { $order_id_list[] = $value->id; } $order_union->order_id_list = json_encode($order_id_list); if (!$order_union->save()) { foreach ($order_union->errors as $error) { return [ 'code' => 1, 'msg' => $error ]; } } } else { $order = [ 'body' => $subject, 'out_trade_no' => $order->order_no, 'total_amount' => $balance_price > 0 ? ($order->pay_price - $balance_price) : $order->pay_price, 'subject' => $subject, 'buyer_id' => $user->ali_user_id ]; } try { $res = self::$alipay->mini($order); if ($res && $res->code === '10000' && $res->msg === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res->toArray() ]; } return [ 'code' => $res->code, 'msg' => $res->msg, ]; } catch (GatewayException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidSignException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidConfigException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 绑定关系 * @param string $out_trade_no * @param array $receiver_list * @return array */ public static function bind($out_trade_no, $receiver_list) { // 绑定关系必须为真实姓名。个人账户为个人真实姓名,企业账户为企业名称 \Yii::warning($receiver_list); $valid_receive_type = array_keys(SharingReceiver::$validAlipayReceiveType); foreach ($receiver_list as $index => $receiver) { if (empty($receiver['account']) || !in_array($receiver['type'], $valid_receive_type) || empty($receiver['name'])) { unset($receiver_list[$index]); \Yii::warning('<========================> 订单号:' . $out_trade_no . '添加分账关系校验参数错误:' . json_encode($receiver)); continue; } $receiver_list[$index]['type'] = SharingReceiver::$validAlipayReceiveType[$receiver['type']]; } if (empty($receiver_list)) { return [ 'code' => 1, 'msg' => '接收方为空' ]; } self::init(get_store_id()); $params = [ 'receiver_list' => $receiver_list, 'out_request_no' => $out_trade_no ]; /** 逻辑开始 **/ try { $res = self::$alipay->bind($params)->get('alipay_trade_royalty_relation_bind_response'); \Yii::warning($res); if ($res && $res['code'] === '10000' && $res['msg'] === 'Success' && $res['result_code'] == 'SUCCESS') { return [ 'code' => 0, 'msg' => '绑定关系成功' ]; } return [ 'code' => 1, 'msg' => $res['sub_msg'], 'res' => $res ]; } catch (GatewayException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidSignException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidConfigException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * settle 分账结算 * @param integer $page * @param integer $page_size * @param string $out_trade_no * @param array $parameters * @param string $is_end * @return array */ public static function settle($out_trade_no, $alipay_trade_no, $parameters, $store_id, $is_end = 'false') { if (empty($out_trade_no) || empty($alipay_trade_no) || empty($parameters)) { return [ 'code' => 1, 'msg' => '参数有误' ]; } $params = [ 'out_request_no' => $out_trade_no, 'trade_no' => $alipay_trade_no, 'royalty_parameters' => $parameters, 'extend_params' => [ 'royalty_finish' => $is_end ] ]; self::init($store_id); /** 逻辑开始 **/ try { $res = self::$alipay->settle($params)->get('alipay_trade_order_settle_response'); \Yii::warning($res); if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') { return [ 'code' => 0, 'msg' => 'success' ]; } return [ 'code' => 1, 'msg' => $res['sub_msg'], 'res' => $res ]; } catch (GatewayException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidSignException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidConfigException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 同步订单 * @param array $params 订单信息 * @return array */ public static function order_sync($params, $store_id) { if (empty($params)) { return [ 'code' => 1, 'msg' => '参数有误' ]; } self::init($store_id); /** 逻辑开始 **/ try { $res = self::$alipay->orderSync($params)->get('alipay_merchant_order_sync_response'); \Yii::warning($res); if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res ]; } return [ 'code' => 1, 'msg' => $res['sub_msg'], 'res' => $res ]; } catch (GatewayException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidSignException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } catch (InvalidConfigException $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }