当前位置:   article > 正文

fastadmin微信支付宝支付 H5支付和微信公众号调起微信支付_微信支付fastadmin

微信支付fastadmin
if ($pay_name=='微信支付' || $pay_name=='支付宝支付'){
            // H5 支付流程
            $type = $pay_name == '微信支付' ? "wechat" : 'alipay';
            $order_no = date('YmdHis') . str_replace('.', '', microtime(true));
            
            $param = [
                'amount'=>$params['number'],
                'orderid'=>$order_no,
                'type'=>    $type,
                'title'=>"充值申请",
                'notifyurl'=>"http://域名.com/api/money_charge/recharge_notify",
                'returnurl'=>"http://域名/api/money_charge/recharge_return?type={$type}&out_trade_no=".$order_no,
                'method'=>"wap",
            ];
            $charge_data = [
                'user_id'   =>  $user['id'],
                'order_no'   =>  $order_no,
                'number'   =>  $params['number'],
                'charge_ways_id'   =>  $params['charge_id'],
                'status'=>4,
                'trade_no'   => '',
                'create_time' =>time(),
            ];
            Recharge::create($charge_data);
            /*if(is_weixin()) {
            	 $param['method'] = 'mp';
            	 $wechat = new Wechat('wx4bf09d3c532f7a0f', '2f2665b6301f1c2f680ae2b3c1671557');
				 $openid = $wechat->getOpenid();
            	 $param['openid'] = 'o51lq0rhR3fRfRzCknCdUkNkDN30';
            }*/
    		if(is_weixin()) {
            	 $wechat = new Wechat('wx4b*******a0f', '2f2665b63*******3c1671557');
				 $pre_url = $wechat->getOpenid($order_no, 0);
				 //dump($pre_url);die;
				 return json(['code'=>110,'msg'=>'成功','data'=>['url'=>$pre_url,'order_no'=>$order_no]]);
            }
            
            
            $return_url =  Service::submitOrder($param);
            if($pay_name=='微信支付')
                return json(['code'=>110,'msg'=>'成功','data'=>['url'=>$return_url,'order_no'=>$order_no]]);
            if($pay_name=='支付宝支付')
                return json(['code'=>120,'msg'=>'成功','data'=>$return_url,'order_no'=>$order_no]);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

Wechat.php中的代码

<?php

namespace addons\epay\library;

use fast\Http;
use think\Cache;
use think\Session;

/**
 * 微信授权
 *
 */
class Wechat
{
    private $app_id = '';
    private $app_secret = '';
    private $scope = 'snsapi_userinfo';

    public function __construct($app_id, $app_secret)
    {
        $this->app_id = $app_id;
        $this->app_secret = $app_secret;
    }

    /**
     * 获取微信授权链接
     *
     * @return string
     */
    public function getAuthorizeUrl($order_no, $money)
    {
        $redirect_uri = addon_url('epay/api/wechat', ['order_no'=>$order_no, 'money'=>$money], true, true);
        $redirect_uri = urlencode($redirect_uri);

        $state = \fast\Random::alnum();
        Session::set('state', $state);
        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope={$this->scope}&state={$state}#wechat_redirect";
    }
    
    

    /**
     * 获取微信openid
     *
     * @return mixed|string
     */
    public function getOpenid($order_no = '', $money=0)
    {
        $openid = Session::get('openid');
        if (!$openid) {
            if (!isset($_GET['code'])) {
                $url = $this->getAuthorizeUrl($order_no, $money);
                // Header("Location:{$url}");
                // exit();
                return $url;
            } else {
                $code = $_GET['code'];
                $token = $this->getAccessToken($code);
                $openid = isset($token['openid']) ? $token['openid'] : '';
                if ($openid) {
                    Session::set("openid", $openid);
                }
            }
        }
 
        return $openid;
    }

    /**
     * 获取授权token网页授权
     *
     * @param string $code
     * @return mixed|string
     */
    public function getAccessToken($code = '')
    {
        $params = [
            'appid'      => $this->app_id,
            'secret'     => $this->app_secret,
            'code'       => $code,
            'grant_type' => 'authorization_code'
        ];
        $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
        if ($ret['ret']) {
            $ar = json_decode($ret['msg'], true);
            return $ar;
        }
        return [];
    }

    public function getJsticket()
    {
        $jsticket = Session::get('jsticket');
        if (!$jsticket) {
            $token = $this->getAccessToken($code);
            $params = [
                'access_token' => 'token',
                'type'         => 'jsapi',
            ];
            $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET');
            if ($ret['ret']) {
                $ar = json_decode($ret['msg'], true);
                return $ar;
            }
        }
        return $jsticket;
    }
}
commin.php中的代码(公共方法)
```php
function is_weixin(){  
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {  
    	//strpos() 函数查找字符串在另一字符串中第一次出现的位置。
        return true;  
    }    
	return false;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/132039
推荐阅读
相关标签
  

闽ICP备14008679号