YeepayController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/12/22
  6. * Time: 9:51
  7. */
  8. namespace app\payment\controllers;
  9. use app\common\events\withdraw\WithdrawSuccessEvent;
  10. use app\payment\PaymentController;
  11. use Yunshop\YeePay\models\YeePayNotifyLogModel;
  12. use Yunshop\YeePay\models\YeePayWithdrawModel;
  13. use Yunshop\YeePay\services\YeePayApiService;
  14. use app\common\services\Pay;
  15. class YeepayController extends PaymentController
  16. {
  17. protected $parameters;
  18. /**
  19. * @var YeePayWithdrawModel
  20. */
  21. protected $withdraw;
  22. /**
  23. * @var YeePayNotifyLogModel
  24. */
  25. protected $notifyLog;
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $this->setParameter();
  30. //访问记录
  31. Pay::payAccessLog();
  32. //保存响应数据
  33. Pay::payResponseDataLog('', '易宝代付提现回调', json_encode($this->parameters));
  34. if (!app('plugins')->isEnabled('yee-pay')) {
  35. exit('success');
  36. }
  37. }
  38. /**
  39. * @return YeePayApiService
  40. */
  41. private function api()
  42. {
  43. return YeePayApiService::current();
  44. }
  45. private function setParameter()
  46. {
  47. $this->parameters = request()->input();
  48. }
  49. /**
  50. * 添加回调日志
  51. */
  52. private function addLog($type)
  53. {
  54. $this->notifyLog = new YeePayNotifyLogModel();
  55. $this->notifyLog->request_param = request()->input();
  56. $this->notifyLog->type = $type;
  57. $this->notifyLog->save();
  58. }
  59. private function editLog($uniacid)
  60. {
  61. $this->notifyLog->uniacid = $uniacid;
  62. $this->notifyLog->save();
  63. }
  64. /**
  65. * @param $trade_number
  66. */
  67. private function setWithdraw($trade_number)
  68. {
  69. $this->withdraw = YeePayWithdrawModel::where('order_sn',$trade_number)->first();
  70. if (!$this->withdraw) {
  71. \Log::debug('易宝代付提现订单回调通知:提现订单信息未找到',$trade_number);
  72. exit('success');
  73. }
  74. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->withdraw->uniacid;
  75. $this->editLog($this->withdraw->uniacid);
  76. }
  77. /**
  78. * @param $withdraw_sn
  79. */
  80. private function setWithdrawSn($withdraw_sn)
  81. {
  82. $this->withdraw = YeePayWithdrawModel::where('withdraw_sn',$withdraw_sn)->first();
  83. if (!$this->withdraw) {
  84. \Log::debug('易宝代付提现订单回调通知:提现订单信息未找到',$withdraw_sn);
  85. exit('success');
  86. }
  87. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->withdraw->uniacid;
  88. $this->editLog($this->withdraw->uniacid);
  89. }
  90. //异步创建易宝代付订单通知
  91. public function notifyUrl()
  92. {
  93. if (empty($this->parameters['body']) && !empty($this->parameters['type']) && $this->parameters['type'] == 'payment_order') {
  94. //付款回调
  95. $this->payNotify();
  96. } elseif (!empty($this->parameters['body'])) {
  97. //订单落地回调
  98. $this->parameters = json_decode($this->parameters['body'],true);
  99. if (empty($this->parameters['type']) || $this->parameters['type'] <> 'create_batch_order') {
  100. \Log::debug('易宝代付提现回调通知:参数异常',$this->parameters);
  101. exit('success');
  102. }
  103. $this->createOrderNotify();
  104. } else {
  105. \Log::debug('易宝代付提现回调通知:参数异常',$this->parameters);
  106. exit('success');
  107. }
  108. }
  109. private function createOrderNotify()
  110. {
  111. $this->addLog(1);
  112. if (empty($this->parameters['trade_number'])) {
  113. \Log::debug('----易宝代付订单落地回调----trade_number为空',$this->parameters);
  114. exit('success');
  115. }
  116. $this->setWithdraw($this->parameters['trade_number']);
  117. try {
  118. if (empty($this->parameters['return_code']) || $this->parameters['return_code'] <> 'SUCCESS') {
  119. throw new \Exception($this->parameters['return_msg']?:'return_code不为success:'.$this->parameters['return_msg']);
  120. }
  121. //todo 易宝返回的data里是二维数组,我们每次只创建一个订单,所以只取0键位数组数据即可
  122. if ($this->parameters['data'][0]['result_code'] <> 'SUCCESS') {
  123. throw new \Exception('result_code不为success:'.$this->parameters['data'][0]['result_code']);
  124. }
  125. if (empty($this->parameters['data'][0]['enterprise_order_id'])) {
  126. throw new \Exception('创建的批次ID为空');
  127. }
  128. $order = $this->api()->findOrderFromRequestNo($this->withdraw->withdraw_sn);
  129. if (!$order) {
  130. throw new \Exception('易宝接口查询订单未找到订单信息');
  131. }
  132. $this->withdraw->enterprise_order_id = $this->parameters['data'][0]['enterprise_order_id'];
  133. $this->withdraw->status = 1;
  134. $this->withdraw->save();
  135. exit('success');
  136. } catch (\Exception $e) {
  137. $this->withdraw->fail_text = $e->getMessage();
  138. $this->withdraw->status = -1;
  139. $this->withdraw->save();
  140. exit('success');
  141. }
  142. }
  143. private function payNotify()
  144. {
  145. $this->addLog(2);
  146. if (empty($this->parameters['request_no'])) {
  147. \Log::debug('----易宝代付提现打款回调----request_no为空',$this->parameters);
  148. exit('success');
  149. }
  150. $this->setWithdrawSn(($this->parameters['request_no']));
  151. if (!$this->api()->verifySign($this->parameters)) {
  152. \Log::debug('----易宝代付提现打款回调----验签失败');
  153. exit('success');
  154. }
  155. try {
  156. if ($this->parameters['withdrawal_status'] == 'TRADE_FINISHED') {
  157. event(new WithdrawSuccessEvent($this->withdraw->withdraw_sn));
  158. \Log::debug('----易宝代付提现打款回调----');
  159. $this->withdraw->pay_status = 3;
  160. $this->withdraw->save();
  161. exit('success');
  162. } else {
  163. switch ($this->parameters['withdrawal_status']) {
  164. case 'TRADE_FAILED':
  165. throw new \Exception('提现失败');
  166. case 'WITHDRAWAL_SUBMITTED':
  167. throw new \Exception('已受理');
  168. case 'REFUND_TICKET':
  169. throw new \Exception('退票成功');
  170. default:
  171. throw new \Exception('未知状态');
  172. }
  173. }
  174. } catch (\Exception $e) {
  175. $this->withdraw->fail_text = $e->getMessage();
  176. $this->withdraw->pay_status = 2;
  177. $this->withdraw->save();
  178. exit('success');
  179. }
  180. }
  181. }