(*
JSAPI和H5支付,就相差一个openid和scene_info参数,JSAPI的openid需要从公众号里获取关注者的openid
H5文档地址:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1
JSAPI文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
*)
if not regexcheck(LowerCase(request.header),
'micromessenger|qq|alipay|aliapp|android|iphone|symbianos|windows phone|ipad|ipod')
then error('仅支持在手机上进行微信支付!');
// 公众账号ID
$appid:='请自填';
// AppSecret
$secret:='请自填';
// 商户号
$mch_id:='请自填';
// key
$key:='请自填';
// 随机字符串
$nonce_str:=md5(random(9999));
// 商品描述
$body:='微信支付测试。。。';
// 商户订单号
if regexcheck(query.state,'[0-9]{18,19}') then
$out_trade_no:=query.state
else
$out_trade_no:=Snowflake(1);
// 标价金额 单位分,不允许小数点
$total_fee:=0.01*100;
// 终端ip
// nginx代理时会传入forwardedfor
if trim(request.forwardedfor)<>'' then
$spbill_create_ip:=request.forwardedfor
else
$spbill_create_ip:=client.ip;
// 通知地址
$notify_url:='https://www.offeu.com/ask/wxpay/notify.api';
// 签名类型 MD5 / HMAC-SHA256
$sign_type:='HMAC-SHA256';
if regexcheck(LowerCase(request.header),'micromessenger') then
begin
// 交易类型 JSAPI=JSAPI支付 NATIVE=Native支付 APP=APP支付
$trade_type:='JSAPI';
// 如果没有获取到code,则去获取
if not regexcheck(query.code,'[0-9a-zA-Z]{32}') then
begin
$s:='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+$appid
+'&redirect_uri=https%3A%2F%2Fwww.offeu.com/ask/wxpay/testpay.api'
+'&response_type=code&scope=snsapi_base&state='+$out_trade_no+'#wechat_redirect';
exit('');
end;
$s:='appid='+$appid+'&secret='+$secret+'&code='+query.code
+'&grant_type=authorization_code';
// 申请 openid 数据
$s:=http('https://api.weixin.qq.com/sns/oauth2/access_token',$s,'','get',10000,65001,65001,false,false);
if not regexcheck($s,'(?<="openid":").*?(?=")') then error('获取openid失败!');
// openid
$openid:=regex($s,'(?<="openid":").*?(?=")').0;
// 待签名数据
$s:='appid='+$appid
+'&body='+$body
+'&mch_id='+$mch_id
+'&nonce_str='+$nonce_str
+'¬ify_url='+$notify_url
+'&openid='+$openid
+'&out_trade_no='+$out_trade_no
+'&sign_type='+$sign_type
+'&spbill_create_ip='+$spbill_create_ip
+'&total_fee='+$total_fee
+'&trade_type='+$trade_type
+'&key='+$key; // 最后加 key
// 签名
$sign:=UpperCase(Hmac($s,$key,'sha256'));
// 拼接完整数据
$s:=''
+''+$appid+''
+''+$body+''
+''+$mch_id+''
+''+$nonce_str+''
+''+$notify_url+''
+''+$openid+''
+''+$out_trade_no+''
+''+$sign_type+''
+''+$spbill_create_ip+''
+''+$total_fee+''
+''+$trade_type+''
+''+$sign+''
+'';
end else begin
// 交易类型 MWEB=H5支付
$trade_type:='MWEB';
// 场景信息
$scene_info:='{"h5_info": {"type": "Wap","wap_url": "https://www.offeu.com","wap_name": "星五博客"}}';
// 待签名数据
$s:='appid='+$appid
+'&body='+$body
+'&mch_id='+$mch_id
+'&nonce_str='+$nonce_str
+'¬ify_url='+$notify_url
+'&out_trade_no='+$out_trade_no
+'&scene_info='+$scene_info
+'&sign_type='+$sign_type
+'&spbill_create_ip='+$spbill_create_ip
+'&total_fee='+$total_fee
+'&trade_type='+$trade_type
+'&key='+$key; // 最后加 key
// 签名
$sign:=UpperCase(Hmac($s,$key,'sha256'));
// 拼接完整数据
$s:=''
+''+$appid+''
+''+$body+''
+''+$mch_id+''
+''+$nonce_str+''
+''+$notify_url+''
+''+$out_trade_no+''
+''+$scene_info+''
+''+$sign_type+''
+''+$spbill_create_ip+''
+''+$total_fee+''
+''+$trade_type+''
+''+$sign+''
+'';
end;
// 申请 统一支付 数据
// 注意:如果用不同的支付方式创建统一支付订单,商户订单号不允许重复,比如jsapi和h5方式发起同一个商户的订单,会出错。
$s:=http('https://api.mch.weixin.qq.com/pay/unifiedorder',$s,'','json',10000,65001,65001,false,false);
// 处理返回结果
//
//
//
$return_code:=regex($s,'(?<=)').0;
$return_msg:=regex($s,'(?<=)').0;
// 处理权限不足的情况,直接返回提示
if $return_code='FAIL' then error($return_msg);
$result_code:=regex($s,'(?<=)').0;
$err_code:=regex($s,'(?<=)').0;
$err_code_des:=regex($s,'(?<=)').0;
$prepay_id:=regex($s,'(?<=)').0;
// 覆盖
$trade_type:=regex($s,'(?<=)').0;
$nonce_str:=regex($s,'(?<=)').0;
$sign:=regex($s,'(?<=)').0;
if ($return_code='SUCCESS') and ($result_code='SUCCESS') then
begin
// 正常创建 统一支付
if regexcheck(LowerCase(request.header),'micromessenger') then
begin
// 校验签名
if ($sign<>UpperCase(Hmac('appid='+$appid+'&mch_id='+$mch_id+'&nonce_str='+$nonce_str+'&prepay_id='
+$prepay_id+'&result_code='+$result_code+'&return_code='+$return_code+'&return_msg='+$return_msg
+'&trade_type='+$trade_type+'&key='+$key,$key,'sha256')))
then error('交易信息存在风险,本次交易取消!');
$timestamp:=timestamp;
exit('');
end else begin
$mweb_url:=regex($s,'(?<=)').0;
// 校验签名
if ($sign<>UpperCase(Hmac('appid='+$appid+'&mch_id='+$mch_id+'&mweb_url='+$mweb_url+'&nonce_str='
+$nonce_str+'&prepay_id='+$prepay_id+'&result_code='+$result_code+'&return_code='+$return_code
+'&return_msg='+$return_msg+'&trade_type='+$trade_type+'&key='+$key,$key,'sha256')))
then error('交易信息存在风险,本次交易取消!');
// 跳转支付页面 &redirect_url是回调地址,支付成功、失败、取消等情况下,都会跳回到这个页面,否则就向后跳一页
exit('');
end;
end else begin
// 未能创建 统一支付
// 校验签名
if ($sign<>UpperCase(Hmac('appid='+$appid+'&err_code='+$err_code+'&err_code_des='+$err_code_des
+'&mch_id='+$mch_id+'&nonce_str='+$nonce_str+'&result_code='+$result_code+'&return_code='+$return_code
+'&return_msg='+$return_msg+'&key='+$key,$key,'sha256')))
then error('交易信息存在风险,本次交易取消!');
// 返回异常信息
error($err_code_des);
end;
?>