WechatscanController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 WechatscanController extends PaymentController
  20. {
  21. private $attach = [];
  22. private $parameter = [];
  23. public function preAction()
  24. {
  25. parent::preAction();
  26. if (empty(\YunShop::app()->uniacid)) {
  27. $post = $this->getResponseResult();
  28. if (\YunShop::request()->attach) {
  29. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = \YunShop::request()->attach['uniacid'];
  30. } else {
  31. // $this->attach = explode(':', $post['attach']);
  32. $this->attach = $post['attach'];
  33. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->attach['uniacid'];
  34. }
  35. }
  36. }
  37. /**
  38. * @throws \app\common\exceptions\AppException
  39. * @throws \app\common\exceptions\ShopException
  40. * @throws \app\common\services\wechat\lib\WxPayException
  41. */
  42. public function notifyUrl()
  43. {
  44. $post = $this->getResponseResult();
  45. \Log::info('微信支付回调结果', $post);
  46. $this->log($post);
  47. $xml = file_get_contents('php://input');
  48. $config = new WxPayConfig();
  49. $result = WxPayResults::Init($config, $xml);
  50. if (!$result) {
  51. echo "fail";
  52. exit();
  53. }
  54. // $result = $this->createOrder($post);
  55. if ($result) {
  56. $data = [
  57. 'total_fee' => $post['total_fee'] ,
  58. 'out_trade_no' => $post['out_trade_no'],
  59. 'trade_no' => $post['transaction_id'],
  60. 'unit' => 'fen',
  61. 'pay_type' => '微信扫码支付',
  62. 'pay_type_id' => $post['trade_type'] == 'MICROPAY' ? 38 : 0
  63. ];
  64. $this->payResutl($data);
  65. echo "success";
  66. } else {
  67. echo "fail";
  68. }
  69. }
  70. /**
  71. * 支付日志
  72. *
  73. * @param $post
  74. */
  75. public function log($post)
  76. {
  77. //访问记录
  78. Pay::payAccessLog();
  79. //保存响应数据
  80. Pay::payResponseDataLog($post['out_trade_no'], '微信扫码支付', json_encode($post));
  81. }
  82. /**
  83. * @param $post
  84. * @return bool|PreOrder
  85. * @throws \app\common\exceptions\AppException
  86. * @throws \app\common\exceptions\ShopException
  87. */
  88. public function createOrder($post)
  89. {
  90. if ($post['openid']) {
  91. //注册会员
  92. $member = 0;
  93. }
  94. if (!$this->attach) {
  95. return false;
  96. }
  97. \app\frontend\models\Member::$current = $member;
  98. $orderGoods = new PreOrderGoods([
  99. 'total' => 1,
  100. 'option_id' => 1,
  101. 'goods_id' => $post['goods_id'],
  102. ]);
  103. $orderGoodsCollection[] = $orderGoods;
  104. $orderGoodsCollection = new OrderGoodsCollection($orderGoodsCollection);
  105. $preOrder = new PreOrder();
  106. $preOrder->init($member, $orderGoodsCollection, $post['attach']);
  107. $preOrder->generate();
  108. \app\frontend\models\Member::$current = null;
  109. return $preOrder;
  110. }
  111. /**
  112. * 签名验证
  113. *
  114. * @return bool
  115. */
  116. public function getSignResult($post)
  117. {
  118. switch ($post['trade_type']) {
  119. case 'JSAPI':
  120. $pay = \Setting::get('shop.pay');
  121. if (isset($this->attach[1]) && $this->attach[1] == 'wechat') {
  122. $min_set = \Setting::get('plugin.min_app');
  123. $pay = [
  124. 'weixin_appid' => $min_set['key'],
  125. 'weixin_secret' => $min_set['secret'],
  126. 'weixin_mchid' => $min_set['mchid'],
  127. 'weixin_apisecret' => $min_set['api_secret'],
  128. 'weixin_cert' => '',
  129. 'weixin_key' => ''
  130. ];
  131. }
  132. break;
  133. case 'APP' :
  134. $pay = \Setting::get('shop_app.pay');
  135. break;
  136. }
  137. $app = $this->getEasyWeChatApp($pay);
  138. $payment = $app->payment;
  139. $notify = $payment->getNotify();
  140. //老版本-无参数
  141. $valid = $notify->isValid();
  142. if (!$valid) {
  143. //新版本-有参数
  144. $valid = $notify->isValid($pay['weixin_apisecret']);
  145. }
  146. return $valid;
  147. }
  148. /**
  149. * 获取回调结果
  150. *
  151. * @return array|mixed|\stdClass
  152. */
  153. public function getResponseResult()
  154. {
  155. $input = file_get_contents('php://input');
  156. if (!empty($input) && empty($_POST['out_trade_no'])) {
  157. //禁止引用外部xml实体
  158. $disableEntities = libxml_disable_entity_loader(true);
  159. $data = json_decode(json_encode(simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  160. libxml_disable_entity_loader($disableEntities);
  161. if (empty($data)) {
  162. exit('fail');
  163. }
  164. if ($data['result_code'] != 'SUCCESS' || $data['return_code'] != 'SUCCESS') {
  165. exit('fail');
  166. }
  167. $post = $data;
  168. } else {
  169. $post = $_POST;
  170. }
  171. return $post;
  172. }
  173. }