微信官方demo基本可用,但是还需要自己慢慢修改,现将填写过程记录如下:
1、准备材料
下载微信官方demo
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
微信公众号
AppID wx23333333333333333
AppSecret 44444444444444444444444444445
设置白名单域名(前台js的和后台的)
微信支付
(使用的民转非的组织机构代码)
商户号 1333333333
证书:
cert
证书存放路径,证书可以登录商户平台
https://pay.weixin.qq.com/index.php/account/api_cert
下载
申请开通
https://jingyan.baidu.com/article/bad08e1e3c110909c8512117.html
生成api证书
https://zhidao.baidu.com/question/2208664595986792588.html?qbl=relate_question_0&word=%BD%D3%C8%EB%CE%A2%D0%C5%D6%A7%B8%B6%20%D0%E8%D2%AA%B4%D3%C4%C4%C0%EF%CF%C2%D4%D8%D6%A4%CA%E9
修改32位支付密码
https://jingyan.baidu.com/article/c1465413f093870bfcfc4c82.html
设置支付文件所在目录(这个等最后再设置也行)
https://jingyan.baidu.com/article/36d6ed1f89c1551bcf488387.html
2、开始编辑:
相对参考
https://blog.csdn.net/sinat_35861727/article/details/72765676
其中有图文,可以借鉴,不过这里直接介绍2019年8月版本的微信公众号。文字性的东西居多。
WxPay.Api.php
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
改为
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
WxPay.Config.php
填写基本信息:
public function GetAppId()
{
return 'wxxxxxxxxxxxxx';
}
public function GetMerchantId()
{
return '2362623601';
}
注意,这里要填写notify.php的具体地址
public function GetNotifyUrl()
{
return "http://www.xxxxxxxxxxxx.com/xxxxxxxxxxxx/weixin/example/notify.php";
}
public function GetSignType()
{
return "HMAC-SHA256";
}
这里是key(之前设置的32位密码)
和appsecret
获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
public function GetKey()
{
return '12411241411313513413414190d';
}
public function GetAppSecret()
{
return 'f1351341341313132323432423468555';
}
另外,注意一下:
public function GetSSLCertPath(&$sslCertPath, &$sslKeyPath)
{
$sslCertPath = '../cert/apiclient_cert.pem';
$sslKeyPath = '../cert/apiclient_key.pem';
}
这个代表着证书需要新建一个同级目录cert,将2个证书放进去。
为了安全性,记得改名。
WxPay.Notify.php
这个2019版的已经改过了,不用再改。
if($needSign == true &&
$this->GetReturn_code($return_code) == "SUCCESS")
{
$this->SetSign();
}
改成:
if($needSign == true &&
$this->GetReturn_code() == "SUCCESS")
{
$this->SetSign();
}
3、配置公众号
配置网页授权域名
配置支付授权目录(2019版移动此项到微信支付平台当中)https://jingyan.baidu.com/article/36d6ed1f89c1551bcf488387.html
4、修改主文件
jsapi.php
//①、获取用户openid
try{
$tools = new JsApiPay();
$openId = $tools->GetOpenid();
ini_set('date.timezone','Asia/Shanghai');//这句是后来加上的,否则会报Time_expire过短。
//②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody("商品名称");//商品名称
$input->SetAttach("test");//附加参数
$input->SetOut_trade_no("sdkphp".date("YmdHis"));//$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));//订单号
$input->SetTotal_fee("1");//支付金额,分
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("http://www.xxxxxxx.com/xxxxxxxxxx/weixin/example/notify.php");//回调函数//与GetNotifyUrl()内容一样。
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$config = new WxPayConfig();
$order = WxPayApi::unifiedOrder($config, $input);
echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);
//获取共享收货地址js函数参数
$editAddress = $tools->GetEditAddressParameters();
} catch(Exception $e) {
Log::ERROR(json_encode($e));
}
需要改的后面我都注释了。
重新描述一遍:
按顺序:
加一行
ini_set('date.timezone','Asia/Shanghai');
改时区。
然后
$input->SetBody("商品名称");//商品名称
$input->SetAttach("test");//附加参数
$input->SetOut_trade_no("sdkphp".date("YmdHis"));//$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));//订单号
$input->SetTotal_fee("1");//支付金额,分
然后
$input->SetNotify_url("http://www.xxxxxxx.com/xxxxxxxxxx/weixin/example/notify.php");//回调函数//与GetNotifyUrl()内容一样。
改完记得将回调地址(即是/notify.php所在的目录:http://www.xxxxxxx.com/xxxxxxxxxx/weixin/example/
添加到微信支付平台)
然后将http://www.xxxxxxx.com/xxxxxxxxxx/weixin/example/jsapi.php
上传并改成二维码(直接访问网址也行)即可生成一个微信支付页面。
这个仅在微信浏览器中有效,(微信电脑版的也有效,网页版没测试)
5、注意问题
测试后会弹出NaN:undefined
这个框,找到/jsapi.php
alert(value1 + value2 + value3 + value4 + ":" + tel);
注释掉就可以了,不影响支付。
参考
http://www.xiefei.cc/1119.html
测试后会生成商户订单号,记得改订单号形式,有用日期的,有用时间戳的,允许字母和数字组合,也允许单纯数字。
订单货物名称不要出现空格。
记得将cert目录建好并将两个文件复制到此目录下。
至于如何调用回调,先研究好在编辑。