ApplyService.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace addons\Alitopay\common\services;
  3. use addons\Alitopay\common\models\Apply;
  4. use addons\Alitopay\common\logic\MiddleApi;
  5. use common\models\common\MiddleMch;
  6. use yii\base\BaseObject;
  7. class ApplyService extends BaseObject
  8. { public $error;
  9. public function error($msg){
  10. $this->error = $msg;
  11. return false;
  12. }
  13. // 获取申请信息
  14. public function getApply($mall_id){
  15. $model = Apply::find()->where(['mall_id'=>$mall_id])->one();
  16. if(empty($model)){
  17. return [];
  18. }
  19. $data = $model->toArray();
  20. $data['statusText'] = $model->getStatusText();
  21. $data['authStatusText'] = $model->getAuthStatusText();
  22. return $data;
  23. }
  24. // 申请
  25. public function apply($mall_id,Apply $model){
  26. $host = \Yii::$app->services->setting->get('system.api_url');
  27. // api 接口提交
  28. $api_data = [
  29. 'alipay_app_id'=>$model['alipay_app_id'],
  30. 'company'=>$model['company'],
  31. 'contacts'=>$model['company'],
  32. 'phone'=>$model['phone'],
  33. 'notify_url'=>$host.'/web/index.php?r=plugin/ali_to_pay/api/notify/auth',
  34. 'callback_url'=>$host.'/web/index.php?r=plugin/ali_to_pay/mall/config/index',
  35. ];
  36. $mch = MiddleMch::getMch($mall_id);
  37. try {
  38. $api = new MiddleApi($mch);
  39. $res = $api->apply($api_data);
  40. return true;
  41. }catch (\Exception $e){
  42. return $this->error($e->getMessage());
  43. }
  44. }
  45. /**
  46. * 授权、审核异步通知
  47. * @desc
  48. *
  49. * @param $data
  50. */
  51. public function ApplyAuthNotify($data){
  52. $alipay_app_id = $data['alipay_app_id'] ?? '';
  53. $model = Apply::find()->where(['alipay_app_id'=>$alipay_app_id,'is_delete'=>0])->orderBy('id desc')->one();
  54. if(empty($model)){
  55. $this->error = "$alipay_app_id 记录不存在";
  56. return false;
  57. }
  58. $msg_type = $data['msg_type'] ?? '';
  59. if($msg_type == 1){
  60. // 审核通知
  61. if($data['status'] == 2){
  62. // 审核成功
  63. $model->status = 2;
  64. $model->auth_status = 1;
  65. $model->auth_url = $data['auth_url'];
  66. }else{
  67. // 审核失败
  68. $model->status = 3;
  69. $model->reason = $data['reason'];
  70. }
  71. return $model->save();
  72. }else if($msg_type == 2){
  73. // 授权通知
  74. // 审核通知
  75. if($data['auth_status'] == 2){
  76. // 授权成功
  77. $model->auth_status = 2;
  78. $model->auth_time = $data['auth_time'];
  79. $model->auth_expires_time = $data['auth_expires_time'];
  80. }
  81. return $model->save();
  82. }
  83. }
  84. }