YunpayController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2017/11/7
  5. * Time: 下午2:41
  6. */
  7. namespace app\payment\controllers;
  8. use app\common\helpers\Url;
  9. use app\common\models\AccountWechats;
  10. use app\common\services\Pay;
  11. use app\payment\PaymentController;
  12. use Yunshop\YunPay\services\YunPayNotifyService;
  13. class YunpayController extends PaymentController
  14. {
  15. private $attach = [];
  16. public function preAction()
  17. {
  18. parent::preAction();
  19. if (empty(\YunShop::app()->uniacid)) {
  20. $this->attach = explode(':', $_POST['orderNo']);
  21. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->attach[1];
  22. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  23. }
  24. }
  25. public function notifyUrl()
  26. {
  27. $parameter = $_POST;
  28. $this->log($parameter);
  29. if(!empty($parameter)){
  30. if($this->getSignResult()) {
  31. if ($_POST['respCode'] == '0006') {
  32. \Log::debug('------验证成功-----');
  33. $data = [
  34. 'total_fee' => floatval($parameter['transAmt']),
  35. 'out_trade_no' => $this->attach[0],
  36. 'trade_no' => $parameter['transactionId'],
  37. 'unit' => 'fen',
  38. 'pay_type' => intval($_POST['productId']) == 112 ? '微信-YZ' : '支付宝-YZ',
  39. 'pay_type_id' => intval($_POST['productId']) == 112 ? 12 : 15
  40. ];
  41. $this->payResutl($data);
  42. \Log::debug('----结束----');
  43. echo 'SUCCESS';
  44. } else {
  45. //其他错误
  46. }
  47. } else {
  48. //签名验证失败
  49. }
  50. }else {
  51. echo 'FAIL';
  52. }
  53. }
  54. public function returnUrl()
  55. {
  56. $trade = \Setting::get('shop.trade');
  57. if (!is_null($trade) && isset($trade['redirect_url']) && !empty($trade['redirect_url'])) {
  58. return redirect($trade['redirect_url'])->send();
  59. }
  60. if (0 == $_GET['state'] && $_GET['errorDetail'] == '成功') {
  61. redirect(Url::absoluteApp('member/payYes', ['i' => $_GET['attach']]))->send();
  62. } else {
  63. redirect(Url::absoluteApp('member/payErr', ['i' => $_GET['attach']]))->send();
  64. }
  65. }
  66. public function frontUrl()
  67. {
  68. $trade = \Setting::get('shop.trade');
  69. if (!is_null($trade) && isset($trade['redirect_url']) && !empty($trade['redirect_url'])) {
  70. return redirect($trade['redirect_url'])->send();
  71. }
  72. if (0 == $_GET['state'] && $_GET['errorDetail'] == '成功') {
  73. redirect(Url::absoluteApp('member', ['i' => $_GET['attach']]))->send();
  74. } else {
  75. redirect(Url::absoluteApp('home', ['i' => $_GET['attach']]))->send();
  76. }
  77. }
  78. public function refundUrl()
  79. {
  80. $parameter = $_POST;
  81. if (!empty($parameter)) {
  82. if ($this->getSignResult()) {
  83. if ($_POST['respCode'] == '0000') {
  84. //验证成功,业务逻辑
  85. } else {
  86. //其他错误
  87. }
  88. } else {
  89. //签名验证失败
  90. }
  91. } else {
  92. echo 'FAIL';
  93. }
  94. }
  95. /**
  96. * 签名验证
  97. *
  98. * @return bool
  99. */
  100. public function getSignResult()
  101. {
  102. $pay = \Setting::get('plugin.yun_pay_set');
  103. $notify = new YunPayNotifyService();
  104. $notify->setKey($pay['key']);
  105. return $notify->verifySign();
  106. }
  107. /**
  108. * 支付日志
  109. *
  110. * @param $post
  111. */
  112. public function log($data)
  113. {
  114. $orderNo = explode(':', $data['orderNo']);
  115. //访问记录
  116. Pay::payAccessLog();
  117. //保存响应数据
  118. Pay::payResponseDataLog($orderNo[0], '芸微信支付', json_encode($data));
  119. }
  120. }