EplusController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2019/4/24
  5. * Time: 下午3:10
  6. */
  7. namespace app\payment\controllers;
  8. use app\common\helpers\Cache;
  9. use app\common\helpers\Url;
  10. use app\common\models\AccountWechats;
  11. use app\common\models\finance\BalanceRecharge;
  12. use app\common\models\Member;
  13. use app\common\models\OrderPay;
  14. use app\common\modules\orderGoods\OrderGoodsCollection;
  15. use app\common\services\Pay;
  16. use app\common\services\PayFactory;
  17. use app\common\services\wechat\lib\WxPayConfig;
  18. use app\common\services\wechat\lib\WxPayResults;
  19. use app\payment\PaymentController;
  20. use Illuminate\Support\Facades\Redis;
  21. use Yunshop\ConvergePay\services\NotifyService;
  22. use app\common\events\withdraw\WithdrawSuccessEvent;
  23. use Yunshop\EplusPay\services\RequestService;
  24. use Yunshop\StoreCashier\frontend\store\models\PreOrder;
  25. use Yunshop\StoreCashier\frontend\store\models\PreOrderGoods;
  26. class EplusController extends PaymentController
  27. {
  28. private $attach = [];
  29. private $parameter = [];
  30. public $decode_data = [];
  31. // public function preAction()
  32. // {
  33. // parent::preAction();
  34. //
  35. // if (empty(\YunShop::app()->uniacid)) {
  36. // $post = $this->getResponseResult();
  37. // if (\YunShop::request()->attach) {
  38. // \Setting::$uniqueAccountId = \YunShop::app()->uniacid = \YunShop::request()->attach['uniacid'];
  39. // } else {
  40. //// $this->attach = explode(':', $post['attach']);
  41. // $this->attach = $post['attach'];
  42. // \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->attach['uniacid'];
  43. // }
  44. // }
  45. // }
  46. public function getPayMethod()
  47. {
  48. switch ($this->decode_data['payMethod']) {
  49. case 'WECHATPAY_MINIPROGRAM':
  50. return PayFactory::EPLUS_MINI_PAY;
  51. break;
  52. case 'SCAN_ALIPAY':
  53. return PayFactory::EPLUS_ALI_PAY;
  54. break;
  55. default:
  56. return PayFactory::EPLUS_WECHAT_PAY;
  57. }
  58. }
  59. public function getPayMethodName()
  60. {
  61. $pay_method = $this->getPayMethod();
  62. switch ($pay_method) {
  63. case PayFactory::EPLUS_MINI_PAY:
  64. return '微信小程序支付(智E+)';
  65. break;
  66. case 'SCAN_ALIPAY':
  67. return '支付宝支付(智E+)';
  68. break;
  69. default:
  70. return '微信支付(智E+)';
  71. }
  72. }
  73. /**
  74. * @throws \app\common\exceptions\AppException
  75. * @throws \app\common\exceptions\ShopException
  76. * @throws \app\common\services\wechat\lib\WxPayException
  77. */
  78. public function notifyUrl()
  79. {
  80. \Log::debug('智E+后端支付回调', request()->all());
  81. $this->decode_data = json_decode(request()->bizContent, true);
  82. $this->log(request()->all());
  83. $pay_sn = $this->decode_data['customOrderNo'];
  84. \Log::debug('智E+后端支付回调单号', $pay_sn);
  85. $key = 'eplus_pay_result_' . $pay_sn;
  86. if (Redis::setnx($key, 1)) {
  87. Redis::expire($key, 10);
  88. } else {
  89. return;
  90. }
  91. if ($order_pay = OrderPay::where('pay_sn', $pay_sn)->first()){
  92. $member_id = $order_pay->uid;
  93. }elseif ($order_pay = BalanceRecharge::withoutGlobalScopes()->where('ordersn', $pay_sn)->first()){
  94. $member_id = $order_pay->member_id;
  95. }else{
  96. \Log::debug('智E+支付回调支付单号不存在', $pay_sn);
  97. return;
  98. }
  99. $member = Member::find($member_id);
  100. if (!$member) {
  101. \Log::debug('智E+支付回调查询会员失败', $pay_sn);
  102. return;
  103. }
  104. \YunShop::app()->uniacid = \Setting::$uniqueAccountId = $member->uniacid;
  105. $res = (new RequestService())->request('payResult', [
  106. 'pay_sn' => $pay_sn
  107. ]);
  108. if ($res['result'] && $res['data']['payStatus'] === '00') {
  109. $data = [
  110. 'total_fee' => $res['data']['amount'],
  111. 'out_trade_no' => $pay_sn,
  112. 'trade_no' => $res['data']['bizOrderNo'],
  113. 'unit' => 'fen',
  114. 'pay_type' => $this->getPayMethodName(),
  115. 'pay_type_id' => $this->getPayMethod(),
  116. ];
  117. $this->payResutl($data);
  118. echo 'SUCCESS';
  119. }
  120. }
  121. //
  122. // /**
  123. // * 支付日志
  124. // *
  125. // * @param $post
  126. // */
  127. public function log($post)
  128. {
  129. //访问记录
  130. Pay::payAccessLog();
  131. //保存响应数据
  132. Pay::payResponseDataLog($this->decode_data['customOrderNo'], $this->getPayMethodName(), json_encode($post));
  133. }
  134. }