在php中实现微信支付,我们需要准备设置以下东西:
1、appid(微信开放平台上的应用id)
2、mch_id(微信申请成功之后邮件中的商户id)
3、notify_url(支付成功后的回调地址)
4、api_key(在微信商户平台上自己设定的api密钥 32位)
5、apiclient_cert.pem 和 apiclient_key.pem 证书
接下来直接上代码(可复制粘贴,修改以上的参数及可使用):
<?php
namespace app\api\controller;
use think\Db;
class Wxpay{
/*
微信支付配置参数
*/
private $config = array(
'appid' => "你的微信开放平台上的应用id", /*微信开放平台上的应用id*/
'mch_id' => "你的微信申请成功之后邮件中的商户id", /*微信申请成功之后邮件中的商户id*/
'api_key' => "你的在微信商户平台上自己设定的api密钥", /*在微信商户平台上自己设定的api密钥 32位*/
'notify_url' => '回调地址',
);
//微信支付下单
public function wxpay($body,$orderid, $out_trade_no, $total_fee, $type){
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$data["appid"] = $this->config["appid"];
$data["body"] = '艺商城-'.$body;
$data["mch_id"] = $this->config['mch_id'];
$data["nonce_str"] = $this->createNoncestr();
$data["notify_url"] = $this->config["notify_url"];
$data['trade_type'] = 'APP';
$data["total_fee"] = "1";//"$total_fee"
$data["out_trade_no"] = $out_trade_no;
$data["spbill_create_ip"] = $this->get_client_ip();
$sign = $this->getSign($data);
$data["sign"] = $sign;
$xml = $this->arrayToXml($data);
$response = $this->postXmlCurl($xml, $url);
$response = $this->xmlToArray($response);
$response = $this->two_sign($response, $data["nonce_str"]);
//返回数据
echo json_encode(['status'=>1,'indo'=>'success','orderid'=>$orderid,'data'=>$response]);
}
//微信支付回调地址--商品支付
public funct