| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * Created by PhpStorm.
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2017/3/28
- * Time: 上午6:50
- */
- namespace app\payment\controllers;
- use app\common\exceptions\ShopException;
- use app\common\facades\Setting;
- use app\common\services\Pay;
- use app\common\services\PayFactory;
- use app\payment\PaymentController;
- use Yunshop\Freelogin\common\service\FreeLoginSign;
- class ThirdPartyWechatController extends PaymentController
- {
- private $appSecret;
- private $expires = 120;
- private $post;
- public function preAction()
- {
- parent::preAction();
- if (empty(\YunShop::app()->uniacid)) {
- if (request()->i) {
- Setting::$uniqueAccountId = \YunShop::app()->uniacid = request()->i;
- } else {
- \Log::debug('---------i--error------', [request()->all()]);
- die('i error');
- }
- $this->post = $_POST;
- $this->post['i'] = \YunShop::app()->uniacid;
- \Log::debug('---------i--------', [\YunShop::app()->uniacid,$this->post]);
- }
- }
- private function verifySign()
- {
- if (!$this->post['appid']) {
- throw new \Exception('appid error');
- }
- if (!$this->post['timestamp']) {
- throw new \Exception('timestamp error');
- }
- if (!$this->post['sign']) {
- throw new \Exception('sign error');
- }
- if (!$this->post['out_trade_no']) {
- throw new \Exception('out_trade_no error');
- }
- $this->getAppData();
- $hfSign = new FreeLoginSign();
- $hfSign->setKey($this->appSecret);
- if (!$hfSign->payNotifyVerify($this->post)) {
- throw new \Exception('sign verify error');
- }
- return true;
- }
- public function notifyUrl()
- {
- try {
- $this->log(request()->all());
- $this->verifySign();
- if (request()->status == 'SUCCESS') {
- if (!$this->post['transaction_id']) {
- throw new \Exception('transaction_id error');
- }
- if (!isset($this->post['total_fee'])) {
- throw new \Exception('total_fee error');
- }
- $pay_type_id = PayFactory::THIRD_PARTY_MINI_PAY;
- $data = [
- 'total_fee' => $this->post['total_fee'] ? : 0 ,
- 'out_trade_no' => $this->post['out_trade_no'],
- 'trade_no' => $this->post['transaction_id'],
- 'unit' => 'yuan',
- 'pay_type' => '第三方微信小程序',
- 'pay_type_id' => $pay_type_id,
- ];
- $this->payResutl($data);
- }
- die("SUCCESS");
- } catch (\Exception $e) {
- \Log::debug('-----第三方小程序支付-----'.$e->getMessage(),[request()->all()]);
- die($e->getMessage());
- }
- }
- private function getAppData()
- {
- $appData = Setting::get('plugin.freelogin_set');
- if (is_null($appData) || 0 == $appData['status']) {
- throw new \Exception('应用未启用');
- }
- if (empty($appData['app_id']) || empty($appData['app_secret'])) {
- throw new \Exception('应用参数错误');
- }
- if ($appData['app_id'] != request()->input('appid')) {
- throw new \Exception('访问身份异常');
- }
- if (time() - request()->input('timestamp') > $this->expires) {
- throw new ShopException('访问超时');
- }
- $this->appSecret = $appData['app_secret'];
- }
- /**
- * 支付日志
- *
- * @param $post
- */
- public function log($post)
- {
- //访问记录
- Pay::payAccessLog();
- //保存响应数据
- Pay::payResponseDataLog($post['out_trade_no'], '第三方微信小程序支付', json_encode($post));
- }
- }
|