WeChat applet payment

No nonsense directly to the code

Come to WeChat SDK first

class WeixinPay{
protected $appid = ""; // applet APPID
protected $mch_id = ""; // merchant ID
protected $Key = ""; // merchant key
protected $openid; // user openid
protected $out_trade_no; // Merchant ID
protected $body; // Product details
protected $total_fee; // Amount
protected $orderBuy; // Merchant ID
function __construct( $openid="",$body="",$total_fee="",$orderBuy ="") {

$this->openid =$openid;

$this->body = $body;
$this->total_fee =$total_fee;
$this->orderBuy = $orderBuy;
}
public function pay(){
$return = $this->weixinapp();
return $return;
}

//Unified order interface
private function unifiedorder() {

$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$parameters = array(
'appid' => $this->appid, // applet ID
'mch_id' => $ this->mch_id, //merchant number
'nonce_str' => $this->createNoncestr(), //random string
//'sign_type'=>"MD5",
//product description
'body' => $this- >body,
//Merchant order number
'out_trade_no'=> $this->mch_id.time(),
//Total amount in points
'total_fee' =>$this->total_fee,
'spbill_create_ip' => $_SERVER['REMOTE_ADDR '], //Terminal IP
'notify_url' => 'https://qds.aiyimu.cn/index.php/api/notify/index', //Notify address to ensure normal access to the external network
// 'spbill_create_ip' = > '192.168.0.161',//Terminal IP
//'notify_url' => 'http://www.weixin.qq.com/wxpay/pay.php',
'trade_type' => 'JSAPI',//trade type
'openid' => $this->openid, //用户id
);

//统一下单签名
$parameters['sign'] = $this->getSign($parameters);

$xmlData = $this->arrayToXml($parameters);


$return = $this->xmlToArray($this->postXmlCurl($xmlData, $url));
return $return;
}
private static function postXmlCurl($xml, $url)
{
$ch = curl_init();
$this_header = array(
"content-type: application/x-www-form-urlencoded;charset=UTF-8"
);
//设置超时

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
//设置header
curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
//The result is required to be a string and output to the screen
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//post submission method
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
set_time_limit(0);
//run curl
$data = curl_exec($ch);
//return result
if ($data) {
curl_close($ch );
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw new WxPayException("curl error, error code: $error");
} }
//The array is converted to xml
protected function arrayToXml($arr){
ksort($arr);
$xml = "<xml>";
foreach ($arr as $key=>$val) {
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
} }
$xml.="</xml>";
return $xml; }
//xml转换成数组
private function xmlToArray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;} if(isset($unifiedorder['prepay_id'])){$unifiedorder = $this->unifiedorder();//Unified order interfaceprivate function weixinapp() {
//Wechat applet interface




$parameters = array(
'appId' => $this->appid, // applet ID
'timeStamp' => '' . time() . '', // timestamp
'nonceStr' => $this->createNoncestr (), //random string
'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //data package
'signType' => 'MD5'//signature method
);
//signature
$parameters[' paySign'] = $this->getSign($parameters);
return $parameters;
}else{
return $unifiedorder;
} }
//function: generate a random string, no longer than 32 bits
private function createNoncestr($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length;$i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//Generate signature
public function getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
//Signature step 1: lexicographically Sort parameters
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//Signature step 2: add KEY after string
$String = $String . "&key=" . $this->Key ;
//Signature step 3: MD5 encryption
$String = MD5($String);
//Signature step 4: Convert all characters to uppercase
$result_ = strtoupper($String);
return $result_;
}
//Format parameters, signature The procedure needs to use the
private function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar ="";
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}}

callback address

 

$postXml = file_get_contents("php://input"); //
if($postXml){
$attr = $this->xmlToArray($postXml);

if($attr['return_code'] =="SUCCESS"){
$total_fee = $attr['total_fee'];
$open_id = $attr['openid'];
$out_trade_no = $attr['out_trade_no'];
$time = $attr['time_end'];
$transaction_id = $attr['transaction_id'];
对数据库得写入即可

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324790997&siteId=291194637