store_id = $store_id; // 第三方应用代理调用支付宝相关接口 // 相关配置为第三方应用所属,但必须加上授权token(app_auth_token) $alipay_config = Option::getSaasAlipay(); $data = ['store_id' => $store_id, 'is_cancel' => 0]; if (\Yii::$app->prod_is_dandianpu()) { if (self_mini() === true) { $data['is_use'] = 1; } } else { $data['is_use'] = 1; } //兼容支付宝平台运营app支付 if ($is_app) { $store_token = StoreMiniToken::find()->where(['batch_status' => [2, 6], 'store_id' => $store_id])->select('app_auth_token')->asArray()->one(); $store_mini['auth_token'] = $store_token['app_auth_token']; if (empty($store_mini['auth_token'])) { $store_mini = StoreAliMini::find()->where($data)->orderBy('id desc')->select('auth_token')->asArray()->one(); } } else { $store_mini = StoreAliMini::find()->where($data)->orderBy('id desc')->select('auth_token')->asArray()->one(); } self::$alipay_config = $alipay_config; $config = [ 'app_id' => $alipay_config['app_id'], 'return_url' => $return_url, 'notify_url' => pay_notify_url(self::$notify_url), 'private_key' => $alipay_config['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_config['alipay_public_key'], 'app_auth_token' => $store_mini['auth_token'] ?: Store::findOne($store_id)->app_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 ($type) { $key = Option::ALIPAY_CONFIG_CERT; if (\Yii::$app->prod_is_dandianpu() && is_h5()){ $key = OptionSetting::ALIPAY_PAY_DANDIANPUH5; if(!Store::hasIncoming($store_id, 'alipay', is_h5())){ $store_id = 0; } } $config_cache = Option::get($key, $store_id, 'alipay')['value']; $config_cache = json_decode($config_cache, true); if (!empty($config_cache)) { $config = [ 'app_id' => $config_cache['app_id'], 'notify_url' => pay_notify_url(self::$notify_url), 'return_url' => $return_url, '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 天 ], 'private_key' => $config_cache['app_private_key'], 'app_cert_public_key' => $this->saveTempImage($config_cache['app_public_cert']), // 应用公钥证书路径 'ali_public_key' => $this->saveTempImage($config_cache['alipay_public_cert']), // 支付宝公钥证书路径 'alipay_root_cert' => $this->saveTempImage($config_cache['alipay_root_cert']), // 支付宝根证书路径 ]; } } self::$alipay = Pay::alipay($config); } /** * 静态方法调用construct方法 * @param $return_url */ public static function init($store_id, $return_url = '', $is_app = false, $type = 0) { return new self($store_id, $return_url, $is_app, $type); } //获取网络图片到临时目录 public function saveTempImage($url) { if (strpos($url, 'http') === false) { $url = 'http:' . trim($url); } if (!is_dir(\Yii::$app->runtimePath . '/crt')) { mkdir(\Yii::$app->runtimePath . '/crt'); } $save_path = \Yii::$app->runtimePath . '/crt/' . md5($url) . '.crt'; CurlHelper::download($url, $save_path); return $save_path; } /** * 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, $type = '', $total_pay_price = 0,$balance_price = 0, $return_url = '') { self::init($order->store_id, $return_url, true); $order1 = $order; 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($order1) . '笔订单合并支付', 'extend_params' => [ 'royalty_freeze' => 'true' ] ]; $order_union = new OrderUnion(); $order_union->store_id = get_store_id(); $order_union->user_id = get_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 ($order1 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, 'extend_params' => [ 'royalty_freeze' => 'true' ] ]; } \Yii::error($order); try { $res = self::$alipay->app($order)->getContent(); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } if($type == OrderNo::ORDER_UNION){ foreach($order1 as $value){ $value->order_union_id = $order_union->id; $value->save(); } } return [ 'code' => 0, 'msg' => 'success', 'data' => $res, 'res' => $res ]; } /** * 订单查询 * @param $order * @param $type 默认:wap: 普通订单,refund:退款订单,transfer: 转账订单 * @return array | Collection */ public static function find($order, $type = 'wap') { if (!in_array($type, self::$validFlag)) { return [ 'code' => 1, 'msg' => 'Params are wrong' ]; } self::init($order->store_id); /** 逻辑开始 **/ $order = [ 'out_trade_no' => $order->order_no, 'query_options' => ['trade_settle_info'] ]; if ($type == self::FLAG_REFUND) { // 后端生成,唯一即可 $order['out_request_no'] = $order->order_no; } try { $res = self::$alipay->find($order, $type); 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() ]; } } /** * wap支付 * @param $order * @param $subject * @param $return_url * @return array */ public static function wap($order, $subject, $return_url = '', $type = '', $total_pay_price = 0, $balance_price = 0, $payData = []) { if(!$return_url){ $return_url = pay_return_url(self::$h5_url); } self::init($order->store_id, $return_url, false, 1); $order1 = $order; 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($order1) . '笔订单合并支付', 'buyer_id' => get_user()->alipay_open_id ]; $order_union = new OrderUnion(); $order_union->store_id = get_store_id(); $order_union->user_id = get_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 ($order1 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' => $order->pay_price, 'subject' => $subject, 'buyer_id' => get_user()->alipay_open_id ]; } try { $res = self::$alipay->wap($order); if ($res->getStatusCode() == 40004 || $res->getStatusCode() == 'ACQ.CONTEXT_INCONSISTENT') { $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL); $order->save(); return self::wap($order, $subject, $return_url, $type, $total_pay_price, $balance_price, $payData); } if($type == OrderNo::ORDER_UNION){ foreach($order1 as $value){ $value->order_union_id = $order_union->id; $value->save(); } } return [ 'code' => 0, 'msg' => 'success', 'data' => $res->getContent() ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => '支付失败'. $e->getMessage() ]; } } /** * web支付 * @param $order * @param $subject * @param $return_url * @return Response | false | string */ public static function web($order, $subject, $return_url = '') { self::init($order->store_id, $return_url); /** 逻辑开始 **/ $order = [ 'body' => $subject, 'out_trade_no' => $order->order_no, 'total_amount' => $order->pay_price, 'subject' => $subject, ]; $res = self::$alipay->web($order)->send(); return $res->getContent(); } /** * 扫码支付 * @param $order * @param $subject * @param $return_url * @return Collection | array */ public static function scan($order, $subject, $return_url = '') { self::init($order->store_id, $return_url); /** 逻辑开始 **/ $order = [ 'body' => $subject, 'out_trade_no' => $order->order_no, 'total_amount' => $order->pay_price, 'subject' => $subject, ]; try { $res = self::$alipay->scan($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 $subject 订单标题 * @param $return_url * @return Collection | array */ public static function scanNew($order, $subject, $return_url = '', $auth_code,$total_pay_price = 0,$balance_price = 0) { self::init($order->store_id,$return_url); /** 逻辑开始 **/ $data = [ 'out_trade_no' => $order->order_no,//订单号 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price, 'subject' => $subject,// 订单标题 'auth_code' => $auth_code, 'scene' => 'bar_code', ]; try { $res = self::$alipay->pos($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 $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($order->store_id, $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() ]; } } /** * 批量转账 * @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($order->store_id, $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 = '', $is_food = false, $total_pay_price = 0, $balance_price = 0, $payData = []) { $self = self::init($order->store_id, $return_url); if (!$order || ($type != OrderNo::ORDER_UNION && empty($subject)) || !$user) { return [ 'code' => 1, 'msg' => '参数有误' ]; } $order1 = $order; 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($order1) . '笔订单合并支付', 'buyer_id' => $user->alipay_open_id ]; $order_union = new OrderUnion(); $order_union->store_id = get_store_id(); $order_union->user_id = get_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 ($order1 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 { try{ $goods_detail = []; $details = OrderDetail::findAll(['order_id' => $order->id]); foreach ($details as $detail) { $goods = Goods::findOne($detail->goods_id); $goods_detail[] = [ 'goods_id' => $goods->goods_no, 'goods_name' => $detail->goods_name, 'quantity' => $detail->num, 'price' => $detail->total_price / $detail->num, ]; } } catch (\Exception $e) { \Yii::error([__METHOD__, $order, $e]); } $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_openId ?: $user->alipay_open_id), 'store_id' => $order->store_id ]; $goods_detail && $order['goods_detail'] = $goods_detail; if ($is_food) { $order['product_code'] = 'FACE_TO_FACE_PAYMENT'; $order['extend_params'] = [ 'food_order_type' => 'qr_order', 'sys_service_provider_id' => self::$alipay_config['user_id'] ]; } else { $store = Store::findOne($order['store_id']); if($store->transfer_profit > 0){ $order['extend_params'] = [ 'royalty_freeze' => 'true' ]; } $order['product_code'] = 'JSAPI_PAY'; } } try { if($payData->ali_requireOrder && $payData->ali_sourceId){ $form = new \app\modules\admin\models\AlipayThirdForm(); $form->bind_store_id = $order->store_id ?: get_store_id(); $form->mini_id = $self->mini_id; $openOrder = $form->AlipayOpenMiniOrderCreate($order1, $order, $payData); if($openOrder['code'] != 0){ return $openOrder; } $order['product_code'] = 'JSAPI_PAY'; $order['extend_params'] = array_merge((array)$order['extend_params'], [ 'trade_component_order_id' => $openOrder['data'], ]); foreach($openOrder['biz_content']['order_detail']['item_infos'] as $item){ $order['goods_detail'][] = [ 'goods_id' => $item['goods_id'], 'goods_name' => $item['goods_name'], 'quantity' => $item['item_cnt'], 'price' => $item['sale_price'], 'out_item_id' => $item['out_item_id'], 'out_sku_id' => $item['out_sku_id'], ]; } } $res = self::$alipay->mini($order); if ($res && $res->code === '10000' && $res->msg === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res->toArray() ]; } if ($type == OrderNo::ORDER_UNION) { foreach ($order1 as $value) { $value->order_union_id = $value->id; $value->save(); } } 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() ]; } } /** * token * @param $authCode * @return array | Collection */ public static function token($authCode) { if (empty($authCode)) { return [ 'code' => 1, 'msg' => 'code不能为空' ]; } $alipay_config = Option::getSaasAlipay(); $config = [ 'app_id' => $alipay_config['app_id'], 'return_url' => 'https://quanqudao.we10.cn', 'notify_url' => pay_notify_url(self::$notify_url), 'private_key' => $alipay_config['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_config['alipay_public_key'], // '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,设置此参数,将进入沙箱模式 ]; $alipay = Pay::alipay($config); $params = [ 'grant_type' => 'authorization_code', 'code' => $authCode ]; /** 逻辑开始 **/ try { $res = $alipay->token($params)->get('alipay_open_auth_token_app_response'); if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res['tokens'][0]['app_auth_token'] ]; } 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() ]; } } /** * 拼接授权链接供商户提供授权 */ public static function authLink() { $store_id = get_store_id(); $app_id = Option::get('alipay_appid', 0, 'saas')['value']; if (empty($app_id)) { return [ 'code' => 1, 'msg' => '服务商appid不存在' ]; } $type = join(',', self::$validAuthType); $redirect_uri = \Yii::$app->request->hostInfo . '/index.php/alipay/callback'; $callback_url = self::AUTH_URL . '?app_id=' . $app_id . '&application_type='. $type . '&redirect_uri=' . $redirect_uri . '&state=' . $store_id; return [ 'code' => 0, 'data' => ['url' => $callback_url] ]; } /** * 绑定关系 * @param string $out_trade_no * @param array $receiver_list * @return array */ public static function bind($out_trade_no, $receiver_list, $is_app = false) { // 绑定关系必须为真实姓名。个人账户为个人真实姓名,企业账户为企业名称 \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' => '接收方为空' ]; } $store_id = get_store_id(); // self::init($store_id, '', $is_app); $params = [ 'receiver_list' => $receiver_list, 'out_request_no' => $out_trade_no ]; //原来逻辑会报错验签失败,所以用新款的 START $data = ['store_id' => $store_id, 'is_cancel' => 0]; if (\Yii::$app->prod_is_dandianpu()) { if (self_mini() === true) { $data['is_use'] = 1; } } else { $data['is_use'] = 1; } if ($is_app) { $store_token = StoreMiniToken::find()->where(['batch_status' => [2, 6], 'store_id' => $store_id])->select('app_auth_token')->asArray()->one(); $store_mini['auth_token'] = $store_token['app_auth_token']; if (empty($store_mini['auth_token'])) { $store_mini = StoreAliMini::find()->where($data)->orderBy('id desc')->select('auth_token')->asArray()->one(); } } else { $store_mini = StoreAliMini::find()->where($data)->orderBy('id desc')->select('auth_token')->asArray()->one(); } $form = new AlipayThirdAppForm(); $request = new AlipayTradeRoyaltyRelationBindRequest(); $request->setBizContent(json_encode($params, JSON_UNESCAPED_UNICODE)); $result = $form->miniCommon($request, null, 0, $store_mini['auth_token']); debug_log(ArrayHelper::toArray($result), 'Alipaybind.log'); if ($result->code == 10000) { return [ 'code' => 0, 'msg' => '绑定关系成功' ]; } else { return [ 'code' => 1, 'msg' => $result->sub_msg ]; } //END \Yii::warning(json_encode($params)); /** 逻辑开始 **/ 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() ]; } } /** * 解绑关系 * @param string $out_trade_no * @param array $receiver_list * @return array */ public static function unbind($out_trade_no, $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)) { unset($receiver_list[$index]); \Yii::warning('<========================> 订单号:' . $out_trade_no . '添加分账关系校验参数错误:' . json_encode($receiver)); } } 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->unbind($params)->get('alipay_trade_royalty_relation_unbind_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() ]; } } /** * queryBind * @param integer $page * @param integer $page_size * @param string $out_trade_no * @return array */ public static function queryBind($out_trade_no, $page = 1, $page_size = 10) { if (empty($out_trade_no)) { return [ 'code' => 1, 'msg' => '商户订单号不能为空' ]; } self::init(get_store_id()); $params = [ 'page_num' => $page, 'page_size' => $page_size, 'out_request_no' => $out_trade_no ]; /** 逻辑开始 **/ try { $res = self::$alipay->queryBind($params)->get('alipay_trade_royalty_relation_batchquery_response'); \Yii::warning($res); if ($res && $res['code'] === '10000' && $res['msg'] === 'Success' && $res['result_code'] == 'SUCCESS') { return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $res['receiver_list'], 'total_page_num' => $res['total_page_num'], 'total_record_num' => $res['total_record_num'], 'current_page_num' => $res['current_page_num'], 'current_page_size' => $res['current_page_size'] ] ]; } 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', $is_app = 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, '', $is_app); /** 逻辑开始 **/ 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() ]; } } /** * qrcode 小程序二维码 * @param string $url_param 页面地址 page/index/index * @param string $query_param 页面参数,示例:x=1 * @param string $describe 二维码描述 * @return array */ public static function qrcode($url_param, $query_param, $describe) { if (empty($url_param) || empty($query_param) || empty($describe)) { return [ 'code' => 1, 'msg' => '参数有误' ]; } $params = [ 'url_param' => $url_param, 'query_param' => $query_param, 'describe' => $describe ]; self::init(get_store_id()); /** 逻辑开始 **/ try { $res = self::$alipay->qrcode($params)->get('alipay_open_app_qrcode_create_response'); \Yii::warning($res); if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res['qr_code_url'] ]; } 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 string $image_type 图片类型 * @param string $image_content 图片内容 二进制 * @return array */ public static function image($image_type, $image_content) { if (empty($image_type) || empty($image_content)) { return [ 'code' => 1, 'msg' => '参数有误' ]; } $params = [ 'image_type' => $image_type, 'image_content' => $image_content ]; self::init(get_store_id()); /** 逻辑开始 **/ try { $res = self::$alipay->image($params)->get('ant_merchant_expand_indirect_image_upload_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() ]; } } /** * 创建店铺 * @param array $params * @return array */ public static function shop_create($params) { if (empty($params)) { return [ 'code' => 1, 'msg' => '参数有误' ]; } self::init(get_store_id()); /** 逻辑开始 **/ try { $res = self::$alipay->shop($params)->get('ant_merchant_expand_shop_create_response'); \Yii::warning($res); if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') { return [ 'code' => 0, 'msg' => 'success', 'data' => $res['order_id'] ]; } 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 string $order_id 订单id * @return array */ public static function shop_query($order_id) { if (empty($order_id)) { return [ 'code' => 1, 'msg' => '参数有误' ]; } $params = [ 'order_id' => $order_id ]; self::init(get_store_id()); /** 逻辑开始 **/ try { $res = self::$alipay->queryShop($params)->get('ant_merchant_expand_order_query_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() ]; } } /** * 同步订单 * @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() ]; } } /** * 上传商品文件 * @param string $scene 业务场景描述 * @param string $file_content 文件二进制字节流 * @return array */ public static function file_upload_old($file_content, $scene = 'SYNC_ORDER') { if (empty($file_content)) { return [ 'code' => 1, 'msg' => '参数有误' ]; } self::init(get_store_id()); /** 逻辑开始 **/ $params = [ 'scene' => $scene, 'file_content' => $file_content ]; try { $res = self::$alipay->itemUpload($params)->get('alipay_merchant_item_file_upload_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() ]; } } /** * 原生上传图片 * @param $image * @return array * @throws \Exception */ public static function file_upload($image) { self::init(get_store_id()); self::aopClient(); $request = new \AlipayMerchantItemFileUploadRequest(); $request->setScene("SYNC_ORDER"); $request->setFileContent("@". $image); $result = self::$aop->execute ( $request, null, Store::findOne(get_store_id())->app_auth_token); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultNode = $result->$responseNode; return (array)($resultNode); } private static function aopClient() { $aop = new \AopClient(); $aop->gatewayUrl = self::$gatewayUrl; $aop->appId = self::$alipay_config['app_id']; $aop->rsaPrivateKey = self::$alipay_config['app_private_key']; $aop->alipayrsaPublicKey = self::$alipay_config['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset='utf-8'; $aop->format='json'; self::$aop = $aop; } }