微信小程序支付微信小程序支付PHP代码代码
本文实例为大家分享了微信小程序支付PHP具体代码,供大家参考,具体内容如下
服务器端获取服务器端获取 openid
Getopenid.php
<?php
header('Content-type: application/json; charset=UTF-8');
$APPID="";//填写小程序appid
$SECRET="";//填写小程序secret
$JSCODE="";
if(isset($_GET['js_code'])){
$JSCODE=$_GET['js_code'];
$url="https://api.weixin.qq.com/sns/jscode2session?appid=".$APPID
."&secret=".$SECRET."&js_code=".$JSCODE."&grant_type=authorization_code";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
$data = curl_exec($curl);
$array=json_decode($data,true);
curl_close($curl);
$openid=isset($array['openid'])?$array['openid']:$array['errcode'];
if($openid=="40029"){
$response["result"] = 0;
$response["msg"] = "invalid code";
$response["openid"] = $openid;
echo json_encode($response);
}else{
$response["result"] = 1;
$response["msg"] = "user exist";
$response["openid"] = $openid;
echo json_encode($response);
}
}
小程序存储小程序存储openid
在app.js中
getUserInfo:function(cb){
var that = this
if(this.globalData.userInfo){
typeof cb == "function" && cb(this.globalData.userInfo)
}else{
wx.login({
success: function (res) {
if (res.code) {
var code = res.code;
wx.getUserInfo({
success: function (res2) {
console.log(res2);
that.globalData.userInfo = res2.userInfo;
typeof cb == "function" && cb(that.globalData.userInfo)
var encryptedData = encodeURIComponent(res2.encryptedData);//一定要把加密串转成URI编码
var iv = res2.iv;
//请求自己的服务器
//Login(code, encryptedData, iv);
wx.showToast({
title: '正在登录...',
icon: 'loading',
duration: 10000
});
//请求服务器
wx.request({
url: API_URL,//Getopenid.php
data: {
js_code: code,
},
method: 'GET',
header: {