| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use AopClient;
- use app\models\AliMiniSign;
- use app\models\Option;
- use app\models\Salesman;
- use app\models\StoreAliMini;
- use app\models\StoreMiniToken;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentCommonSignRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentConfirmRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentCreateRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentMobilepaySignRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAgentOrderQueryRequest;
- use app\utils\CurlHelper;
- use app\utils\QrCode;
- use yii\base\Model;
- include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
- class AlipayThirdMerchantForm extends Model
- {
- public $ali_mini_id;
- public $status;
- public $params_info;
- public $base_info;
- public $template_info;
- public $app_type;
- public function rules()
- {
- return [
- [['ali_mini_id', 'status', 'app_type'], 'integer'],
- [['params_info'], 'safe']
- ];
- }
- //构造函数
- public function __construct($config = [])
- {
- parent::__construct($config);
- //获取三方平台信息
- //三方平台Appid
- $this->base_info['appid'] = Option::get("alipay_appid", 0, 'saas')['value'];
- //获取当前时间戳
- $this->base_info['timestamp'] = date("Y-m-d H:i:s");
- //设置加解密方式
- $this->base_info['sign_type'] = "RSA2";
- //获取私钥
- $this->base_info['private_key'] = Option::get("alipay_app_private_key", 0, 'saas')['value'];
- //获取公钥
- $this->base_info['public_key'] = Option::get("alipay_public_key", 0, 'saas')['value'];
- //获取ase_key,加签方式
- $this->base_info['alipay_aes_key'] = Option::get("alipay_aes_key", 0, 'saas')['value'];
- //获取模板aes加签方式
- $this->template_info['ali_template_aes'] = Option::get("ali_template_aes", 0, 'saas')['value'];
- //获取模板appid
- $this->template_info['ali_template_appid'] = Option::get("ali_template_appid", 0, 'saas')['value'];
- //获取模板auth_token
- $this->template_info['ali_template_token'] = Option::get("ali_template_token", 0, 'saas')['value'];
- }
- public function miniCommon($request, $biz_content = null, $is_hidden_token = 0, $token = '')
- {
- try {
- //模板信息
- $template_info = $this->template_info;
- //请求的基础信息
- $base_info = $this->base_info;
- //模板token
- $auth_token = $template_info['ali_template_token'];
- //小程序ID
- $ali_mini_id = $this->ali_mini_id;
- if (!empty($ali_mini_id)) {
- $store_token = StoreAliMini::findOne($ali_mini_id);
- //如果小程序ID存在,则将token从模板转成商家小程序的token
- if (!empty($store_token->auth_token)) {
- $auth_token = $store_token->auth_token;
- }
- }
- //设置基础请求数据
- $aop = new AopClient();
- //设置appid
- $aop->appId = $base_info['appid'];
- //设置私钥
- $aop->rsaPrivateKey = $base_info['private_key'];
- //设置公钥
- $aop->alipayrsaPublicKey = $base_info['public_key'];
- //设置ase_key,加签方式
- $aop->encryptKey = $base_info['alipay_aes_key'];
- //设置加解密方式
- $aop->signType = $base_info['sign_type'];
- //处理没有biz_content情况
- if ($biz_content) {
- $request->setBizContent($biz_content);
- }
- //处理不需要token情况
- if ($is_hidden_token === 0) {
- //开始请求
- $result = $aop->execute($request, null, ($token ?: $auth_token));
- } else {
- //开始请求
- $result = $aop->execute($request);
- }
- //请求后的数据处理
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return $result->$responseNode;
- } catch (\Exception $e) {
- return [
- 'sub_msg' => $e->getMessage()
- ];
- }
- }
- /**
- * @return array
- * 创建事务
- */
- public function getMerchantPay(): array
- {
- try {
- $ali_mini_id = $this->ali_mini_id;
- $params_info = $this->params_info;
- $ali_sign = AliMiniSign::findOne(['ali_mini_id' => $ali_mini_id, 'type' => 1]) ?: new AliMiniSign();
- $ali_sign->type = 1;
- $ali_sign->ali_mini_id = $ali_mini_id;
- $ali_sign->app_type = intval($this->app_type);
- $store_ali_mini = StoreAliMini::findOne($ali_mini_id);
- if (intval($this->app_type)) {
- $store_ali_token = StoreMiniToken::findOne($ali_mini_id);
- $store_ali_mini = (object)[
- 'alipay_account' => $store_ali_token->account,
- 'contact_name' => $store_ali_token->contact_name,
- 'service_phone' => $store_ali_token->contact_mobile,
- 'service_email' => $store_ali_token->contact_email,
- 'app_name' => $store_ali_token->contact_name . '分账签约应用'
- ];
- }
- //获取事务状态
- $batch_status = intval($ali_sign->batch_status);
- //如果已经开始审核,且没有失败,没有被商户手动拒绝
- if ($batch_status > 4 && $batch_status !== 8) {
- throw new \Exception("审核中,无需操作");
- }
- if (
- empty($params_info['alipay_account']) ||
- empty($params_info['contact_name']) ||
- empty($params_info['service_phone']) ||
- empty($params_info['service_email']) ||
- empty($params_info['license_pic']) ||
- empty($params_info['license_no'])
- ) {
- throw new \Exception("请完善信息");
- }
- $param = json_encode([
- 'account' => $params_info['alipay_account'] ?: $store_ali_mini->alipay_account,
- 'contact_info' => [
- 'contact_name' => $params_info['contact_name'] ?: $store_ali_mini->contact_name,
- 'contact_mobile' => $params_info['service_phone'] ?: $store_ali_mini->service_phone,
- 'contact_email' => $params_info['service_email'] ?: $store_ali_mini->service_email
- ]
- ]);
- //调用接口配置信息
- $request = new AlipayOpenAgentCreateRequest();
- //调用接口
- $mini_token = null;
- if (intval($this->app_type) === 1) {
- $store_token = StoreMiniToken::findOne($ali_mini_id);
- if ($store_token) {
- $mini_token = $store_token->app_auth_token;
- }
- }
- $result = $this->miniCommon($request, $param, 0, $mini_token);
- if ($result->code == 10000) {
- //保存事务编号,用作签约等操作
- $batch_no = $result->batch_no;
- $ali_sign->batch_no = $batch_no;
- if (intval($this->app_type) === 0) {
- $store_ali_mini->alipay_account = $params_info['alipay_account'];
- $store_ali_mini->contact_name = $params_info['contact_name'];
- $store_ali_mini->service_phone = $params_info['service_phone'];
- $store_ali_mini->service_email = $params_info['service_email'];
- //保存信息
- if (!$store_ali_mini->save()) {
- throw new \Exception(json_encode($store_ali_mini->errors));
- }
- }
- //保存状态信息
- $ali_sign->batch_status = 1;
- //保存信息
- if (!$ali_sign->save()) {
- throw new \Exception(json_encode($ali_sign->errors));
- }
- $result = $this->appPay($ali_sign, $store_ali_mini, $batch_no);
- if ($result['code'] !== 0) {
- throw new \Exception($result['msg']);
- }
- $result = $this->openAgentConfirm($ali_sign, $batch_no);
- if ($result['code'] !== 0) {
- throw new \Exception($result['msg']);
- }
- return [
- 'code' => 0,
- 'msg' => $result['msg'],
- 'data' => $result['data']
- ];
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * @param array | AliMiniSign $model
- * @param array | StoreAliMini $ali_model
- * @param string $batch_no
- * @return array
- * 签约分账
- */
- public function appPay($model, $ali_model, string $batch_no = '')
- {
- try {
- $params_info = $this->params_info;
- $request = new AlipayOpenAgentCommonSignRequest();
- $license_pic = $this->saveTempImage($params_info['license_pic']);
- //设置业务编号
- $request->setBatchNo($batch_no);
- //设置商家经营类目编码
- $request->setMccCode("A0002_B0201");
- //特殊资质证书
- if (!empty($params_info['special_license_pic'])) {
- $request->setSpecialLicensePic($params_info['other_data']);
- }
- // $request->setBusinessLicenseNo($params_info['license_no']);
- // $request->setDateLimitation("2099-12-31");
- $request->setProductCode('DOMESTIC_ALLOC');
- $request->setBusinessLicensePic("@". $license_pic);
- $request->setAppName($ali_model->app_name);
- $request->setLongTerm(true);
- $request->setDateLimitation("2999-12-31");
- $request->setBusinessLicenseNo($params_info['license_no']);
- //调用接口
- $result = $this->miniCommon($request,null, 1);
- //如果请求成功或者报同类产品已经签约就显示成功
- if (($result->code == 10000) || $result->sub_code == "ANT_PRODUCT_CONFLICT") {
- $model->batch_status = 2;
- if (intval($this->app_type) === 0) {
- $ali_model->license_pic = $params_info['license_pic'];
- $ali_model->license_no = $params_info['license_no'];
- if (!$ali_model->save()) {
- throw new \Exception(json_encode($model->errors));
- }
- }
- if (!$model->save()) {
- throw new \Exception(json_encode($model->errors));
- }
- return [
- 'code' => 0,
- 'msg' => "操作成功",
- 'batch_status' => $ali_model->batch_status
- ];
- }
- throw new \Exception($result->sub_msg);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * @param array | AliMiniSign $model
- * @param string $batch_no
- * @return array
- * 提交事务
- */
- public function openAgentConfirm($model, string $batch_no = '')
- {
- try {
- //请求参数
- $param = json_encode([
- //业务编号
- 'batch_no' => $batch_no
- ]);
- //请求配置信息
- $request = new AlipayOpenAgentConfirmRequest();
- $result = $this->miniCommon($request, $param, 1);
- //请求成功
- if ($result->code == 10000) {
- $model->batch_status = 3;
- if (!$model->save()) {
- throw new \Exception($model->errors);
- }
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- }
- throw new \Exception($result->sub_msg);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * @return array|void
- * 查询状态
- */
- public function openAgentOrderStatus()
- {
- try {
- $ali_mini_id = $this->ali_mini_id;
- if (intval($this->app_type) === 1) {
- $model = AliMiniSign::findOne(['ali_mini_id' => $ali_mini_id, 'type' => 1]);
- } else {
- $model = AliMiniSign::findOne(['id' => $ali_mini_id, 'type' => 1]);
- }
- if (empty($model)) {
- throw new \Exception('没有待查询的信息');
- }
- $batch_no = $model->batch_no;
- if (empty($batch_no)) {
- throw new \Exception('未创建事务');
- }
- //请求参数
- $param = json_encode([
- //业务编号
- 'batch_no' => $batch_no
- ]);
- $request = new AlipayOpenAgentOrderQueryRequest();
- $result = $this->miniCommon($request, $param, 1);
- if ($result->code == 10000) {
- $status = $result->order_status;
- $msg = "成功";
- switch ($status) {
- case "MERCHANT_INFO_HOLD": //异常单 4
- $model->batch_status = 4;
- $model->fail_reason = "订单异常";
- break;
- case "MERCHANT_AUDITING": //审核中 3
- $model->batch_status = 3;
- $model->fail_reason = "订单审核中";
- break;
- case "MERCHANT_CONFIRM": //待确认 5
- $model->batch_status = 5;
- $model->batch_url = $result->confirm_url;
- $model->fail_reason = "等待商户确认,请注意查收邮件以及短信或访问" . $result->confirm_url . "进行授权";
- break;
- case "MERCHANT_CONFIRM_SUCCESS": //商户确认成功 6
- $model->batch_status = 2;
- $model->fail_reason = "商户确认成功";
- break;
- case "MERCHANT_CONFIRM_TIME_OUT": //商户超时未确认 7
- $model->batch_status = 7;
- $model->fail_reason = "商户超时未确认";
- break;
- case "MERCHANT_APPLY_ORDER_CANCELED": //审核失败或商户拒绝 8
- $model->batch_status = 8;
- $model->fail_reason = $result->reject_reason;
- break;
- }
- if (!$model->save()) {
- throw new \Exception($model->errors);
- }
- return [
- 'code' => 0,
- 'msg' => $msg,
- 'data' => $result
- ];
- }
- throw new \Exception($result->sub_msg);
- } catch (\Exception $e) {
- $file_name = \Yii::$app->runtimePath . '/logs/app_pay.log';
- file_put_contents($file_name, "\r\n" . '[商城申请商家分账支付][' . date('Y-m-d H:i:s') . ']' . $e->getMessage(), FILE_APPEND);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $this->ali_mini_id,
- ];
- }
- }
- public function getAppInfo($type = 0)
- {
- try {
- $ali_mini_id = $this->ali_mini_id;
- $where = [];
- if ($type) { //App分账
- $where = [
- 'app_type' => 1
- ];
- $store_ali_mini = StoreMiniToken::find()->where(['id' => $ali_mini_id])
- ->select('id, account alipay_account, contact_name, contact_mobile service_phone, contact_email service_email, license_pic, license_no')
- ->asArray()->one();
- } else {
- $store_ali_mini = StoreAliMini::find()->where(['id' => $ali_mini_id])
- ->select('id, alipay_account, contact_name, service_phone, service_email, license_pic, license_no')
- ->asArray()->one();
- }
- $ali_sign = AliMiniSign::findOne(array_merge(['ali_mini_id' => $ali_mini_id, 'type' => 1], $where));
- $store_ali_mini['batch_status'] = $ali_sign['batch_status'] ?? 0;
- $store_ali_mini['fail_reason'] = $ali_sign['fail_reason'] ?? '';
- return [
- 'code' => 0,
- 'data' => $store_ali_mini ?: [
- "alipay_account" => '',
- "contact_name" => '',
- "service_phone" => '',
- "service_email" => '',
- "license_pic" => '',
- "license_no" => '',
- "batch_status" => 0
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- //获取网络图片到临时目录
- public function saveTempImage($url)
- {
- if (strpos($url, 'http') === false) {
- $url = 'http:' . trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- }
|