YopsystemController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/3/1
  6. * Time: 10:09
  7. */
  8. namespace app\payment\controllers;
  9. use app\common\components\BaseController;
  10. use app\common\modules\yop\sdk\Util\YopSignUtils;
  11. use Yunshop\YopSystem\common\YopLog;
  12. use Yunshop\YopSystem\models\YopSystemMerchant;
  13. use app\common\models\AccountWechats;
  14. use Illuminate\Support\Facades\DB;
  15. class YopsystemController extends BaseController
  16. {
  17. protected $set;
  18. protected $parameters;
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. if (!app('plugins')->isEnabled('yop-system')) {
  23. echo 'Not turned on yop system';
  24. exit();
  25. }
  26. $this->set = $this->getMerchantNo();
  27. if (empty(\YunShop::app()->uniacid)) {
  28. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->set['uniacid'];
  29. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  30. }
  31. $this->init();
  32. }
  33. private function init()
  34. {
  35. $yop_data = $_REQUEST['response'];
  36. if ($yop_data) {
  37. $response = YopSignUtils::decrypt($yop_data, $this->set['private_key'], $this->set['yop_public_key']);
  38. $this->parameters = json_decode($response, true);
  39. }
  40. }
  41. protected function getMerchantNo()
  42. {
  43. \Log::debug('--------------易宝入网参数--------------', $_REQUEST);
  44. $app_key = $_REQUEST['customerIdentification'];
  45. $merchant_no = substr($app_key, strrpos($app_key, 'OPR:')+4);
  46. $model = DB::table('yz_yop_system')->where('parent_merchant_no', $merchant_no)->first();
  47. if (empty($model)) {
  48. exit('商户不存在');
  49. }
  50. return $model;
  51. }
  52. //子商户入网
  53. public function notifyUrl()
  54. {
  55. \Log::debug('--------------易宝入网--------------', $this->parameters);
  56. $this->yopResponse('子商户入网:'.$this->parameters['requestNo'], $this->parameters);
  57. $son = YopSystemMerchant::where('requestNo', $this->parameters['requestNo'])->first();
  58. if (empty($son)) {
  59. exit('Merchant does not exist');
  60. }
  61. $status = $this->merNetInStatus();
  62. $son->status = $status;
  63. $son->externalId = $this->parameters['externalId'];
  64. $son->remark = $this->parameters['remark'] ?: '';
  65. $bool = $son->save();
  66. if ($bool) {
  67. echo 'SUCCESS';
  68. exit();
  69. } else {
  70. echo '保存出错';
  71. exit();
  72. }
  73. }
  74. protected function merNetInStatus()
  75. {
  76. $status = YopSystemMerchant::INVALID;
  77. if (!empty($this->parameters['merNetInStatus'])) {
  78. switch ($this->parameters['merNetInStatus']) {
  79. case 'PROCESS_SUCCESS': //审核通过
  80. $status = YopSystemMerchant::PROCESS_SUCCESS;
  81. break;
  82. case 'PROCESS_REJECT': //审核拒绝
  83. $status = YopSystemMerchant::PROCESS_REJECT;
  84. break;
  85. case 'PROCESS_BACK': //审核回退
  86. $status = YopSystemMerchant::PROCESS_BACK;
  87. break;
  88. case 'PROCESSING_PRODUCT_INFO_SUCCESS': //审核中-产品提前开通
  89. $status = YopSystemMerchant::PROCESSING_PRODUCT_INFO_SUCCESS;
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. return $status;
  96. }
  97. //聚合报备
  98. public function backUrl()
  99. {
  100. \Log::debug('-------------聚合报备---------------', $this->parameters);
  101. $this->yopResponse('聚合报备:'.$this->parameters['merchantNo'], $this->parameters);
  102. $son = YopSystemMerchant::where('merchantNo', $this->parameters['merchantNo'])->first();
  103. if (empty($son)) {
  104. exit('Merchant does not exist');
  105. }
  106. $report_status = $this->reportStatusCode();
  107. $son->report_status = $report_status;
  108. $bool = $son->save();
  109. if ($bool) {
  110. echo 'SUCCESS';
  111. exit();
  112. } else {
  113. echo '保存出错';
  114. exit();
  115. }
  116. }
  117. protected function reportStatusCode()
  118. {
  119. switch ($this->parameters['reportStatusCode']) {
  120. //报备成功
  121. case '':
  122. case 'NULL':
  123. case '0000':
  124. $report_status = YopSystemMerchant::BACK_SUCCESS;
  125. break;
  126. //处理中
  127. case '1111':
  128. case '1112':
  129. case '3333':
  130. case '710001':
  131. $report_status = YopSystemMerchant::BACK_WAIT;
  132. break;
  133. //失败
  134. default:
  135. $report_status = YopSystemMerchant::BACK_FAIL;
  136. break;
  137. }
  138. return $report_status;
  139. }
  140. protected function yopLog($desc, $data)
  141. {
  142. YopLog::yopLog($desc, $data);
  143. }
  144. protected function yopResponse($desc, $params)
  145. {
  146. YopLog::yopResponse($desc, $params);
  147. }
  148. }