ThirdPartyWechatController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/28
  6. * Time: 上午6:50
  7. */
  8. namespace app\payment\controllers;
  9. use app\common\exceptions\ShopException;
  10. use app\common\facades\Setting;
  11. use app\common\services\Pay;
  12. use app\common\services\PayFactory;
  13. use app\payment\PaymentController;
  14. use Yunshop\Freelogin\common\service\FreeLoginSign;
  15. class ThirdPartyWechatController extends PaymentController
  16. {
  17. private $appSecret;
  18. private $expires = 120;
  19. private $post;
  20. public function preAction()
  21. {
  22. parent::preAction();
  23. if (empty(\YunShop::app()->uniacid)) {
  24. if (request()->i) {
  25. Setting::$uniqueAccountId = \YunShop::app()->uniacid = request()->i;
  26. } else {
  27. \Log::debug('---------i--error------', [request()->all()]);
  28. die('i error');
  29. }
  30. $this->post = $_POST;
  31. $this->post['i'] = \YunShop::app()->uniacid;
  32. \Log::debug('---------i--------', [\YunShop::app()->uniacid,$this->post]);
  33. }
  34. }
  35. private function verifySign()
  36. {
  37. if (!$this->post['appid']) {
  38. throw new \Exception('appid error');
  39. }
  40. if (!$this->post['timestamp']) {
  41. throw new \Exception('timestamp error');
  42. }
  43. if (!$this->post['sign']) {
  44. throw new \Exception('sign error');
  45. }
  46. if (!$this->post['out_trade_no']) {
  47. throw new \Exception('out_trade_no error');
  48. }
  49. $this->getAppData();
  50. $hfSign = new FreeLoginSign();
  51. $hfSign->setKey($this->appSecret);
  52. if (!$hfSign->payNotifyVerify($this->post)) {
  53. throw new \Exception('sign verify error');
  54. }
  55. return true;
  56. }
  57. public function notifyUrl()
  58. {
  59. try {
  60. $this->log(request()->all());
  61. $this->verifySign();
  62. if (request()->status == 'SUCCESS') {
  63. if (!$this->post['transaction_id']) {
  64. throw new \Exception('transaction_id error');
  65. }
  66. if (!isset($this->post['total_fee'])) {
  67. throw new \Exception('total_fee error');
  68. }
  69. $pay_type_id = PayFactory::THIRD_PARTY_MINI_PAY;
  70. $data = [
  71. 'total_fee' => $this->post['total_fee'] ? : 0 ,
  72. 'out_trade_no' => $this->post['out_trade_no'],
  73. 'trade_no' => $this->post['transaction_id'],
  74. 'unit' => 'yuan',
  75. 'pay_type' => '第三方微信小程序',
  76. 'pay_type_id' => $pay_type_id,
  77. ];
  78. $this->payResutl($data);
  79. }
  80. die("SUCCESS");
  81. } catch (\Exception $e) {
  82. \Log::debug('-----第三方小程序支付-----'.$e->getMessage(),[request()->all()]);
  83. die($e->getMessage());
  84. }
  85. }
  86. private function getAppData()
  87. {
  88. $appData = Setting::get('plugin.freelogin_set');
  89. if (is_null($appData) || 0 == $appData['status']) {
  90. throw new \Exception('应用未启用');
  91. }
  92. if (empty($appData['app_id']) || empty($appData['app_secret'])) {
  93. throw new \Exception('应用参数错误');
  94. }
  95. if ($appData['app_id'] != request()->input('appid')) {
  96. throw new \Exception('访问身份异常');
  97. }
  98. if (time() - request()->input('timestamp') > $this->expires) {
  99. throw new ShopException('访问超时');
  100. }
  101. $this->appSecret = $appData['app_secret'];
  102. }
  103. /**
  104. * 支付日志
  105. *
  106. * @param $post
  107. */
  108. public function log($post)
  109. {
  110. //访问记录
  111. Pay::payAccessLog();
  112. //保存响应数据
  113. Pay::payResponseDataLog($post['out_trade_no'], '第三方微信小程序支付', json_encode($post));
  114. }
  115. }