SandpayController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/12/21
  8. * Time: 10:50
  9. */
  10. namespace app\payment\controllers;
  11. use app\common\helpers\Url;
  12. use app\common\models\PayOrder;
  13. use app\payment\PaymentController;
  14. class SandpayController extends PaymentController
  15. {
  16. const SANDPAY_ALIPAY = 81;
  17. const SANDPAY_WECHAT = 82;
  18. // 微信同步回调
  19. public function returnUrlWechat()
  20. {
  21. $this->returnResponse();
  22. }
  23. // 支付宝同步回调
  24. public function returnUrlAlipay()
  25. {
  26. $this->returnResponse();
  27. }
  28. // 支付宝支付成功回调
  29. public function notifyUrlAlipay()
  30. {
  31. $data = $this->verifyHandle();
  32. $total_fee = $this->handlerFee($data->body->totalAmount);
  33. $data = $this->setData($data->body->orderCode, $data->body->tradeNo, $total_fee, self::SANDPAY_ALIPAY, '杉德微信支付');
  34. \Log::debug('-------- 杉德支付宝支付: 验证数据正常->更新订单状态 start --------');
  35. $this->payResutl($data);
  36. \Log::debug('-------- 杉德支付宝支付: 验证数据正常->更新订单状态 end --------');
  37. echo $this->success();
  38. exit;
  39. }
  40. // 微信退款成功回调
  41. public function refundNotifyUrlAlipay()
  42. {
  43. $this->verifyHandle();
  44. \Log::debug('-------- 杉德支付宝: 退款通过 --------');
  45. echo $this->success();
  46. exit;
  47. }
  48. // 微信支付成功回调
  49. public function notifyUrlWechat()
  50. {
  51. $data = $this->verifyHandle();
  52. $total_fee = $this->handlerFee($data->body->totalAmount);
  53. $data = $this->setData($data->body->orderCode, $data->body->tradeNo, $total_fee, self::SANDPAY_WECHAT, '杉德微信支付');
  54. \Log::debug('-------- 杉德微信支付: 验证数据正常->更新订单状态 start --------');
  55. $this->payResutl($data);
  56. \Log::debug('-------- 杉德微信支付: 验证数据正常->更新订单状态 end --------');
  57. echo $this->success();
  58. exit;
  59. }
  60. // 微信退款成功回调
  61. public function refundNotifyUrlWechat()
  62. {
  63. $this->verifyHandle();
  64. \Log::debug('-------- 杉德微信: 退款通过 --------');
  65. echo $this->success();
  66. exit;
  67. }
  68. public function returnResponse()
  69. {
  70. if ($_SERVER['REDIRECT_STATUS'] == 200) {
  71. redirect(Url::absoluteApp('member/payYes', ['i' => request()->input('i')]))->send();
  72. } else {
  73. redirect(Url::absoluteApp('member/payErr', ['i' => request()->input('i')]))->send();
  74. }
  75. }
  76. protected function validator($param)
  77. {
  78. if (is_null($_POST[$param])) {
  79. \Log::debug("-------- 获取杉德支付回调失败 未检测到第三方参数: $param--------");
  80. exit;
  81. }
  82. return $_POST[$param];
  83. }
  84. // 获取公众号
  85. protected function getUniacid($orderCode)
  86. {
  87. $payOrder = PayOrder::select('uniacid')->where('out_order_no', $orderCode)->first();
  88. if ($payOrder) {
  89. return $payOrder->uniacid;
  90. }
  91. \Log::debug('商城订单号未找到: ' . json_encode($orderCode));
  92. exit;
  93. }
  94. /**
  95. * 支付回调参数
  96. *
  97. * @param $order_no
  98. * @param $parameter
  99. * @return array
  100. */
  101. public function setData($order_no, $trade_no, $total_fee, $pay_type_id, $pay_type)
  102. {
  103. return [
  104. 'total_fee' => $total_fee * 100,
  105. 'out_trade_no' => $order_no,
  106. 'trade_no' => $trade_no,
  107. 'unit' => 'fen',
  108. 'pay_type' => $pay_type,
  109. 'pay_type_id' => $pay_type_id,
  110. ];
  111. }
  112. // 杉德获取回来的金额是12位数的. 需要做处理 '000000000001' = '0.01'
  113. public function handlerFee($amount)
  114. {
  115. return bcdiv($amount, 100,2);
  116. }
  117. public function success()
  118. {
  119. return 'respCode=000000';
  120. }
  121. /**
  122. * @return mixed|void
  123. * @throws \app\common\exceptions\ShopException
  124. */
  125. public function verifyHandle()
  126. {
  127. list($dataJson, $data, $sign) = $this->verifyParams();
  128. $uniacid = $this->getUniacid($data->body->orderCode);
  129. \YunShop::app()->uniacid = $uniacid;
  130. $payConfig = new \Yunshop\Sandpay\services\CommonPayConfig;
  131. $verifyRes = $payConfig->verify($dataJson, $sign);
  132. if (!$verifyRes) {
  133. \Log::debug('杉德支付----验证回调签名未通过');
  134. exit;
  135. }
  136. return $data;
  137. }
  138. /**
  139. * @return array|void
  140. */
  141. public function verifyParams()
  142. {
  143. $dataJson = $this->validator('data');
  144. $data = json_decode($dataJson);
  145. $sign = $this->validator('sign');
  146. if ($data->head->respCode !== '000000') {
  147. \Log::debug(self::class . ' 杉德错误代码: ' . $data->head->respCode);
  148. \Log::debug(self::class . ' 杉德错误代码: ' . $data->head->respMsg);
  149. exit;
  150. }
  151. return array($dataJson, $data, $sign);
  152. }
  153. }