YopmerchantController.php 5.7 KB

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