url :是你自己请求的https服务器;加上你的code一起传过去;
接下来看后台:我用的是tp5框架;
$code = $_GET['code']; //这是你前台传过来的code值
$appId = '###'; //你的appid
$secret = '###'; //你的AppSecret
$mch_id = '##'; //你的商户号
$url='https://api.weixin.qq.com/sns/jscode2sessionappid='.$appId.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
$userOpen = file_get_contents($url);
$openId = json_decode($userOpen,true);
$openId = $openId['openid']; //这是你所获取的openid
$data_sign = [
'appid'=>$appId,
'mch_id'=>$mch_id,
'nonce_str'=>$nonce_str,
'body'=>$body,
'out_trade_no'=>$out_trade_no,
'total_fee'=>$total_fee,
'spbill_create_ip'=>$spbill_create_ip,
'notify_url'=>$notify_url,
'trade_type'=>'JSAPI',
'openid'=>$openId
];
$key = ''; //商户密钥
$sign = self::sign($data_sign,$key);
$xml_data = '
'.$body.'
$ret_pay = self::http_post($xml_data);
$payld = self::getPreld($ret_pay);
$pre_pay_id = 'prepay_id='.$payld;
$time = "'".time()."'";
$data_pay_sign = [
'appId'=>$appId,
'timeStamp'=>$time,
'nonceStr'=>md5(rand(1000,20000)),
'signType'=>'MD5'
];
$pay_sign = self::sign($data_pay_sign,$key);
$data_pay_sign['paySign'] = $pay_sign;
$data_pay_sign['package'] = $pre_pay_id;
echo json_encode($data_pay_sign);
}
public function sign($data,$key){
ksort($data);
$str = '';
foreach($data as $k=>$v){
$str.=$k.'='.$v.'&';
}
$str.='key='.$key;
return strtoupper(md5($str));
}
public function http_post($data){
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不检查 ssl
$output = curl_exec($ch);
if (false === $output) {
echo 'CURL Error:' . curl_error($ch);die;
}
return $output;
}
public static function getPreld($xml){
$res = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
return $res->prepay_id;
}
后台其实就是这么多东西;然后把你需要的数据返回到前台;填写这些值就可以了.
现在就已经可以付款成功了.
当然,如果实际操作的话,还需判断,查询你的数据库中的付款状态是否已经改变;