WftController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\payment\controllers;
  3. use app\common\helpers\Url;
  4. use app\common\models\AccountWechats;
  5. use app\payment\PaymentController;
  6. use app\frontend\modules\finance\models\BalanceRecharge;
  7. use app\common\services\Pay;
  8. use app\common\models\Order;
  9. use app\common\models\OrderPay;
  10. class WftController extends PaymentController
  11. {
  12. //原始数据
  13. private $xml;
  14. private $key;
  15. private $parameters = [];
  16. public function preAction()
  17. {
  18. parent::preAction();
  19. if (empty(\YunShop::app()->uniacid)) {
  20. $this->xml = file_get_contents('php://input');
  21. $obj = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  22. $this->parameters = json_decode(json_encode($obj), true);
  23. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->parameters['attach'];
  24. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  25. }
  26. }
  27. //微信公众号支付通知
  28. public function notifyUrl()
  29. {
  30. \Log::debug('------------威富通微信异步通知---------------->');
  31. $this->log($this->parameters, '威富通微信');
  32. $set = \Setting::get('plugin.wft_pay');
  33. $this->setKey($set['key']);
  34. if($this->getSignResult()) {
  35. \Log::info('------威富通微信验证成功-----');
  36. if ($this->getParameter('status') == 0 && $this->getParameter('result_code') == 0) {
  37. \Log::info('-------威富通微信支付开始---------->');
  38. $data = [
  39. 'total_fee' => floatval($this->getParameter('total_fee')),
  40. 'out_trade_no' => $this->getParameter('out_trade_no'),
  41. 'trade_no' => 'wft_pay',
  42. 'unit' => 'fen',
  43. 'pay_type' => '威富通微信支付',
  44. 'pay_type_id' => 20,
  45. ];
  46. $this->payResutl($data);
  47. \Log::info('<---------威富通微信支付结束-------');
  48. echo 'success';
  49. exit();
  50. } else {
  51. //支付失败
  52. echo 'failure';
  53. exit();
  54. }
  55. } else {
  56. //签名验证失败
  57. echo 'failure';
  58. exit();
  59. }
  60. }
  61. //支付宝支付通知
  62. public function alipayNotifyUrl()
  63. {
  64. \Log::debug('------------威富通支付宝异步通知---------------->');
  65. $this->log($this->parameters, '威富通支付宝');
  66. $set = \Setting::get('plugin.wft_alipay');
  67. $this->setKey($set['key']);
  68. if($this->getSignResult()) {
  69. \Log::info('------威富通支付宝验证成功-----');
  70. if ($this->getParameter('status') == 0 && $this->getParameter('result_code') == 0) {
  71. \Log::info('-------威富通支付宝支付开始---------->');
  72. $data = [
  73. 'total_fee' => floatval($this->getParameter('total_fee')),
  74. 'out_trade_no' => $this->getParameter('out_trade_no'),
  75. 'trade_no' => 'wft_alipay',
  76. 'unit' => 'fen',
  77. 'pay_type' => '威富通支付宝',
  78. 'pay_type_id' => 21,
  79. ];
  80. $this->payResutl($data);
  81. \Log::info('<---------威富通支付宝支付结束-------');
  82. echo 'success';
  83. exit();
  84. } else {
  85. //支付失败
  86. echo 'failure';
  87. exit();
  88. }
  89. } else {
  90. //签名验证失败
  91. echo 'failure';
  92. exit();
  93. }
  94. }
  95. /**
  96. * 签名验证
  97. *
  98. * @return bool
  99. */
  100. public function getSignResult()
  101. {
  102. $swiftpassSign = strtolower($this->getParameter('sign'));
  103. $md5Sign = $this->getMD5Sign();
  104. return $swiftpassSign == $md5Sign;
  105. }
  106. //MD5签名
  107. public function getMD5Sign() {
  108. $signPars = "";
  109. ksort($this->parameters);
  110. foreach($this->parameters as $k => $v) {
  111. if("sign" != $k && "" != $v) {
  112. $signPars .= $k . "=" . $v . "&";
  113. }
  114. }
  115. $signPars .= "key=" . $this->getKey();
  116. return strtolower(md5($signPars));
  117. }
  118. /**
  119. *设置密钥
  120. */
  121. public function setKey($key) {
  122. $this->key = $key;
  123. }
  124. /**
  125. * @param 获取密钥
  126. * @return mixed
  127. */
  128. public function getKey()
  129. {
  130. return $this->key;
  131. }
  132. /**
  133. *获取参数值
  134. */
  135. public function getParameter($parameter) {
  136. return isset($this->parameters[$parameter])?$this->parameters[$parameter] : '';
  137. }
  138. /**
  139. * 支付日志
  140. *
  141. * @param $post
  142. */
  143. public function log($data, $msg = '威富通支付')
  144. {
  145. //访问记录
  146. Pay::payAccessLog();
  147. //保存响应数据
  148. Pay::payResponseDataLog($this->getParameter('out_trade_no'), $msg, json_encode($data));
  149. }
  150. }