English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Some personal insights into developing WeChat payment on the APP side using PHP
Recently, due to the company's needs, we need to develop WeChat payment on the APP side, I have read the WeChat document, and it is quite good, without encountering too many pitfalls, and the points to pay attention to are not too many.
Write a note document as a memo.
APP payment process
From the above picture, it can be seen that the process that needs to be paid attention to is a total of3Part;
The first part: calling the order API, returning the pre-payment order, signing the information and then returning it (4,5,6,7)
The second part: asynchronous notification (15,16)
The third part: the final judgment of the payment result
The most important part to pay attention to is the first part: calling the order API, returning the pre-payment order, signing the information and then returning it
WeChat documentationThere are detailed descriptions, so there is no need to elaborate here.
Append my code below, copycat, just a little change to the code can be used.
//Entry function function weChatPay(){ $json = array(); //Required parameters for generating a pre-payment transaction order: $newPara = array(); //Application ID $newPara["appid"] = "wx2421b1c4370ec43b"; //Merchant number $newPara["mch_id"] = "10000100"; //Device number $newPara["device_info"] = "WEB"; //Random string, it is recommended to use the function to generate $newPara["nonce_str"] = "1add1a30ac87aa2db72f57a2375d8fec //Product description $newPara["body"] = "APP payment test"; //Merchant order number, this is the internal order number of the merchant $newPara["out_trade_no"] = "1415659990"; //Total amount $newPara["total_fee"] = 1; //IP terminal $newPara["spbill_create_ip"] = $_SERVER["REMOTE_ADDR"]; //Adresse de notification, notez que vous ne devez pas ajouter de paramètres dans l'url ici $newPara["notify_url"] = "http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php"; //Type de transaction $newPara["trade_type"] = "APP"; //La signature pour la première fois $newPara["sign"] = produceWeChatSign($newPara); //Transformer l'array en format xml $xmlData = getWeChatXML($newPara); //Utiliser le paquet CURL de PHP pour transmettre les données à l'interface de commande unifiée de WeChat, et retourner un prepay_id normal $get_data = sendPrePayCurl($xmlData); //Jugement sur le résultat retourné. if($get_data['return_code'] == "SUCCESS" && $get_data['result_code'] == "SUCCESS"){ //Faire une signature à deux niveaux en fonction du résultat retourné par WeChat Payment //Chaîne aléatoire nécessaire pour la signature à deux niveaux $newPara["nonce_str"] = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"; //Timestamp nécessaire pour la signature à deux niveaux $newPara['timeStamp'] = time().""; //Complément des paramètres restants de la signature à deux niveaux $secondSignArray = array( "appid"=>$newPara['appid'], "noncestr"=>$newPara['nonce_str'], "package"=>"Sign=WXPay", "prepayid"=>$get_data['prepay_id'], "partnerid"=>$newPara['mch_id'], "timestamp"=>$newPara['timeStamp'], ); $json['datas'] = $secondSignArray; $json['ordersn'] = $newPara["out_trade_no"]; $json['datas']['sign'] = weChatSecondSign($newPara,$get_data['prepay_id']); $json['message'] = "Prépaiement terminé"; //La prépaiement est terminé, procéder à la logique métier interne en dessous /*****************************/ return json_encode($json); } else{ $json['message'] = $get_data['return_msg']; } } return json_encode($json); } //La fonction de signature pour la première fois produceWeChatSign function produceWeChatSign($newPara){ $stringA = self::getSignContent($newPara); $stringSignTemp=$stringA."&key="192006250b4c09247ec02edce69f6a2d"; return strtoupper(MD5($stringSignTemp)); } //function to generate xml format public static function getWeChatXML($newPara){ $xmlData = "<xml>"; foreach ($newPara as $key => $value) { $xmlData = $xmlData."<".$key.">".$value."</".$key.">"; } $xmlData = $xmlData."</xml>"; return $xmlData; } //function to send data to WeChat interface via curl function sendPrePayCurl($xmlData) { $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $header[] = "Content-type: text/xml"; $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlData); $data = curl_exec($curl); if (curl_errno($curl)) { print curl_error($curl); } curl_close($curl); return self::XMLDataParse($data); } //xml format data parsing function public static function XMLDataParse($data){ $msg = array(); $msg = (array)simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); return $msg; } //Fonction de double signature function weChatSecondSign($newPara,$prepay_id){ $secondSignArray = array( "appid"=>$newPara['appid'], "noncestr"=>$newPara['nonce_str'], "package"=>"Sign=WXPay", "prepayid"=>$prepay_id, "partnerid"=>$newPara['mch_id'], "timestamp"=>$newPara['timeStamp'], ); $stringA = self::getSignContent($secondSignArray); $stringSignTemp=$stringA."&key="192006250b4c09247ec02edce69f6a2d"; return strtoupper(MD5($stringSignTemp)); }
Deux points à noter :
1.La double signature doit être effectuée en arrière-plan, et après cela, toutes les informations nécessaires à la double signature doivent être transmises à l'avant pour permettre l'appel du paiement WeChat. Cela évite les situations où le paiement WeChat ne peut pas être appelé.
2.Double signature, utilisant des chaînes de caractères aléatoires différentes.
Voici la totalité du contenu de cet article, j'espère qu'il vous sera utile dans vos études, et j'espère que vous soutenerez également le tutoriel criant.
Déclaration : le contenu de cet article est extrait du réseau, propriété de l'auteur original, partagé par les utilisateurs d'Internet et téléversé spontanément. Le site Web ne détient pas de propriété, n'a pas été édité par l'homme, et n'assume aucune responsabilité juridique. Si vous trouvez du contenu suspect de violation de droits d'auteur, veuillez envoyer un e-mail à : notice#oldtoolbag.com (veuillez remplacer # par @ lors de l'envoi d'un e-mail pour signaler des infractions, et fournir des preuves pertinentes. Une fois vérifié, le site supprimera immédiatement le contenu suspect d'infraction.