HkscanController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Url;
  9. use app\common\models\AccountWechats;
  10. use app\common\modules\orderGoods\OrderGoodsCollection;
  11. use app\common\services\Pay;
  12. use app\common\services\wechat\lib\WxPayConfig;
  13. use app\common\services\wechat\lib\WxPayResults;
  14. use app\payment\PaymentController;
  15. use Yunshop\ConvergePay\services\NotifyService;
  16. use app\common\events\withdraw\WithdrawSuccessEvent;
  17. use Yunshop\StoreCashier\frontend\store\models\PreOrder;
  18. use Yunshop\StoreCashier\frontend\store\models\PreOrderGoods;
  19. class HkscanController extends PaymentController
  20. {
  21. private $attach = [];
  22. private $parameter = [];
  23. private $param = '';
  24. public function preAction()
  25. {
  26. parent::preAction();
  27. if (empty(\YunShop::app()->uniacid)) {
  28. $post = $this->getResponseResult();
  29. $this->attach = $post['attach'];
  30. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->attach;
  31. }
  32. }
  33. /**
  34. * @throws \app\common\exceptions\AppException
  35. * @throws \app\common\exceptions\ShopException
  36. * @throws \app\common\services\wechat\lib\WxPayException
  37. */
  38. public function notifyUrl()
  39. {
  40. $post = $this->getResponseResult();
  41. \Log::info('港版微信支付回调结果', $post);
  42. $this->param = '微信港版扫码支付';
  43. $this->log($post);
  44. //验签
  45. $result = $this->getSignResult($post);
  46. if ($result) {
  47. $data = [
  48. 'total_fee' => $post['total_fee'] ,
  49. 'out_trade_no' => $post['out_trade_no'],
  50. 'trade_no' => 'hk_pay',
  51. 'unit' => 'fen',
  52. 'pay_type' => '港版扫码支付',
  53. 'pay_type_id' => 56
  54. ];
  55. $this->payResutl($data);
  56. echo "success";
  57. } else {
  58. echo "fail";
  59. }
  60. }
  61. public function notifyAliUrl()
  62. {
  63. $post = $this->getResponseResult();
  64. \Log::info('支付宝香港H5支付回调结果', $post);
  65. $this->param = '支付宝香港H5支付';
  66. $this->log($post);
  67. //验签
  68. $result = $this->getSignResult($post);
  69. if ($result) {
  70. $data = [
  71. 'total_fee' => $post['total_fee'] ,
  72. 'out_trade_no' => $post['out_trade_no'],
  73. 'trade_no' => 'hk_pay',
  74. 'unit' => 'fen',
  75. 'pay_type' => '支付宝香港H5支付',
  76. 'pay_type_id' => 62
  77. ];
  78. $this->payResutl($data);
  79. echo "success";
  80. } else {
  81. echo "fail";
  82. }
  83. }
  84. /**
  85. * 支付日志
  86. *
  87. * @param $post
  88. */
  89. public function log($post)
  90. {
  91. //访问记录
  92. Pay::payAccessLog();
  93. //保存响应数据
  94. Pay::payResponseDataLog($post['out_trade_no'], $this->param, json_encode($post));
  95. }
  96. /**
  97. * 签名验证
  98. *
  99. * @return bool
  100. */
  101. public function getSignResult($data)
  102. {
  103. $sign = $data['sign'];
  104. $set = \Setting::get('plugin.hk_pay_set');
  105. //验签
  106. unset($data['sign']);
  107. $string1 = '';
  108. foreach($data as $k => $v) {
  109. $string1 .= "{$k}={$v}&";
  110. }
  111. $string1 .= "key={$set['key']}";
  112. if ($sign == strtoupper(md5($string1))) {
  113. \Log::debug('验签成功');
  114. return true;
  115. }
  116. return false;
  117. }
  118. /**
  119. * 获取回调结果
  120. *
  121. * @return array|mixed|\stdClass
  122. */
  123. public function getResponseResult()
  124. {
  125. $input = file_get_contents('php://input');
  126. if (!empty($input) && empty($_POST['out_trade_no'])) {
  127. //禁止引用外部xml实体
  128. $disableEntities = libxml_disable_entity_loader(true);
  129. $data = json_decode(json_encode(simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  130. libxml_disable_entity_loader($disableEntities);
  131. if (empty($data)) {
  132. exit('fail');
  133. }
  134. if ($data['status'] != 0 || $data['result_code'] != 0 || $data['pay_result'] != 0) {
  135. exit('fail');
  136. }
  137. $post = $data;
  138. } else {
  139. $post = $_POST;
  140. }
  141. return $post;
  142. }
  143. }