DianbangscanController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 DianbangscanController extends PaymentController
  11. {
  12. private $parameters = [];
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. if (empty(\YunShop::app()->uniacid)) {
  17. $this->parameters = $_POST;
  18. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->parameters['billDesc'];
  19. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  20. }
  21. }
  22. //微信公众号支付通知
  23. public function notifyUrl()
  24. {
  25. \Log::debug('------------店帮微信异步通知---------------->');
  26. $this->log($this->parameters, '店帮微信');
  27. $set = \Setting::get('plugin.dian-bang-scan');
  28. $this->setKey($set['secret']);
  29. $order_no = explode('-', $this->getParameter('billNo'));
  30. if($this->verify($this->parameters)) {
  31. $billPayment = json_decode($this->parameters['billPayment'],true);
  32. if ($billPayment['status'] == 'TRADE_SUCCESS') {
  33. \Log::debug('-------店帮微信支付开始---------->');
  34. $data = [
  35. 'total_fee' => floatval($this->getParameter('totalAmount')),
  36. 'out_trade_no' => $order_no[1],
  37. 'trade_no' => $billPayment['targetOrderId'],
  38. 'unit' => 'fen',
  39. 'pay_type' => '店帮微信支付',
  40. 'pay_type_id' => 24,
  41. ];
  42. $this->payResutl($data);
  43. \Log::debug('<---------店帮微信支付结束-------');
  44. echo 'SUCCESS';
  45. exit();
  46. } else {
  47. //支付失败
  48. echo 'FAILED';
  49. exit();
  50. }
  51. } else {
  52. //签名验证失败
  53. echo 'FAILED';
  54. exit();
  55. }
  56. }
  57. //支付宝支付通知
  58. // public function alipayNotifyUrl()
  59. // {
  60. // \Log::debug('------------店帮支付宝异步通知---------------->');
  61. // $this->log($this->parameters, '店帮支付宝');
  62. //
  63. // $set = \Setting::get('plugin.dian-bang-scan');
  64. // $this->setKey($set['key']);
  65. //
  66. // if($this->getSignResult()) {
  67. // \Log::info('------店帮支付宝验证成功-----');
  68. // if ($this->getParameter('status') == 0 && $this->getParameter('result_code') == 0) {
  69. // \Log::info('-------店帮支付宝支付开始---------->');
  70. // $data = [
  71. // 'total_fee' => floatval($this->getParameter('total_fee')),
  72. // 'out_trade_no' => $this->getParameter('out_trade_no'),
  73. // 'trade_no' => 'dian-bang-scan',
  74. // 'unit' => 'fen',
  75. // 'pay_type' => '店帮支付宝',
  76. // 'pay_type_id' => 24,
  77. // ];
  78. // $this->payResutl($data);
  79. // \Log::info('<---------店帮支付宝支付结束-------');
  80. // echo 'success';
  81. // exit();
  82. // } else {
  83. // //支付失败
  84. // echo 'failure';
  85. // exit();
  86. // }
  87. // } else {
  88. // //签名验证失败
  89. // echo 'failure';
  90. // exit();
  91. // }
  92. // }
  93. public function returnUrl()
  94. {
  95. \Log::debug('<--------_GET-------->',$_GET);
  96. $trade = \Setting::get('shop.trade');
  97. if (!is_null($trade) && isset($trade['redirect_url']) && !empty($trade['redirect_url'])) {
  98. return redirect($trade['redirect_url'])->send();
  99. }
  100. redirect(Url::absoluteApp('member/payYes', ['i' => $_GET['i']]))->send();
  101. }
  102. /**
  103. * 验证签名是否正确
  104. * @param $data
  105. * @return bool
  106. */
  107. function verify($data) {
  108. //返回参数生成sign
  109. $signType = empty($data['signType']) ? 'md5' : $data['signType'];
  110. $sign = $this->generateSign($data, $signType);
  111. //返回的sign
  112. $returnSign = $data['sign'];
  113. if ($returnSign != $sign) {
  114. return false;
  115. }
  116. return true;
  117. }
  118. public function generateSign($params, $signType = 'md5') {
  119. return $this->sign($this->getSignContent($params), $signType);
  120. }
  121. /**
  122. * 生成signString
  123. * @param $params
  124. * @return string
  125. */
  126. public function getSignContent($params) {
  127. //sign不参与计算
  128. $params['sign'] = '';
  129. //排序
  130. ksort($params);
  131. $paramsToBeSigned = [];
  132. foreach ($params as $k=>$v) {
  133. if ($v !== '')
  134. {
  135. if (is_array($v))
  136. {
  137. $paramsToBeSigned[] = $k.'='.str_replace("\\/", "/", json_encode($v,JSON_UNESCAPED_UNICODE));
  138. }else{
  139. $paramsToBeSigned[] = $k.'='.$v;
  140. }
  141. }
  142. }
  143. unset ($k, $v);
  144. //签名字符串
  145. $stringToBeSigned = implode('&', $paramsToBeSigned);
  146. $stringToBeSigned .= $this->key;
  147. return $stringToBeSigned;
  148. }
  149. /**
  150. * 生成签名
  151. * @param $data
  152. * @param string $signType
  153. * @return string
  154. */
  155. protected function sign($data, $signType = "md5") {
  156. $sign = hash($signType, $data);
  157. return strtoupper($sign);
  158. }
  159. /**
  160. *设置密钥
  161. */
  162. public function setKey($key) {
  163. $this->key = $key;
  164. }
  165. /**
  166. * @param 获取密钥
  167. * @return mixed
  168. */
  169. public function getKey()
  170. {
  171. return $this->key;
  172. }
  173. /**
  174. *获取参数值
  175. */
  176. public function getParameter($parameter) {
  177. return isset($this->parameters[$parameter])?$this->parameters[$parameter] : '';
  178. }
  179. /**
  180. * 支付日志
  181. *
  182. * @param $post
  183. */
  184. public function log($data, $msg = '店帮支付')
  185. {
  186. //访问记录
  187. Pay::payAccessLog();
  188. //保存响应数据
  189. Pay::payResponseDataLog($this->getParameter('out_trade_no'), $msg, json_encode($data));
  190. }
  191. }