| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace addons\Alitopay\common\services;
- use addons\Alitopay\common\models\Apply;
- use addons\Alitopay\common\logic\MiddleApi;
- use common\models\common\MiddleMch;
- use yii\base\BaseObject;
- class ApplyService extends BaseObject
- { public $error;
- public function error($msg){
- $this->error = $msg;
- return false;
- }
- // 获取申请信息
- public function getApply($mall_id){
- $model = Apply::find()->where(['mall_id'=>$mall_id])->one();
- if(empty($model)){
- return [];
- }
- $data = $model->toArray();
- $data['statusText'] = $model->getStatusText();
- $data['authStatusText'] = $model->getAuthStatusText();
- return $data;
- }
- // 申请
- public function apply($mall_id,Apply $model){
- $host = \Yii::$app->services->setting->get('system.api_url');
- // api 接口提交
- $api_data = [
- 'alipay_app_id'=>$model['alipay_app_id'],
- 'company'=>$model['company'],
- 'contacts'=>$model['company'],
- 'phone'=>$model['phone'],
- 'notify_url'=>$host.'/web/index.php?r=plugin/ali_to_pay/api/notify/auth',
- 'callback_url'=>$host.'/web/index.php?r=plugin/ali_to_pay/mall/config/index',
- ];
- $mch = MiddleMch::getMch($mall_id);
- try {
- $api = new MiddleApi($mch);
- $res = $api->apply($api_data);
- return true;
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 授权、审核异步通知
- * @desc
- *
- * @param $data
- */
- public function ApplyAuthNotify($data){
- $alipay_app_id = $data['alipay_app_id'] ?? '';
- $model = Apply::find()->where(['alipay_app_id'=>$alipay_app_id,'is_delete'=>0])->orderBy('id desc')->one();
- if(empty($model)){
- $this->error = "$alipay_app_id 记录不存在";
- return false;
- }
- $msg_type = $data['msg_type'] ?? '';
- if($msg_type == 1){
- // 审核通知
- if($data['status'] == 2){
- // 审核成功
- $model->status = 2;
- $model->auth_status = 1;
- $model->auth_url = $data['auth_url'];
- }else{
- // 审核失败
- $model->status = 3;
- $model->reason = $data['reason'];
- }
- return $model->save();
- }else if($msg_type == 2){
- // 授权通知
- // 审核通知
- if($data['auth_status'] == 2){
- // 授权成功
- $model->auth_status = 2;
- $model->auth_time = $data['auth_time'];
- $model->auth_expires_time = $data['auth_expires_time'];
- }
- return $model->save();
- }
- }
- }
|