PHP live source in the payment of recharge, the dependence on Alipay is quite large. With the use of Alipay payment, adding Alipay payment and callback in PHP live broadcast source becomes more and more common. Here we will analyze how to realize the use of PHP live source to pay callbacks.
1. Access and configuration of Alipay
1, login to Alipay open platform ( https://open.alipay.com/platform/home.htm ), enter the open platform, click the top right corner, key management - > Mapi gateway product key - > RSA (SHA1) secret key setting;
[Note: if it has been set before, please confirm whether there are APP items using the key]
2. Use the secret key generation tool to generate pairs of public and private keys according to their own needs;
Private key (download rsa signature verification tool)
Two, Alipay callback use
After receiving the user's recharge request, the PHP live broadcast source will automatically tune up the Alipay payment to make payment. The relevant configuration parameters of Alipay can be placed in the background to fill in, so that it is convenient to change and replace. The web side of PHP downloads Alipay's corresponding sdk to be used.
1, PHP live source in the background need to add Alipay configuration file content
//Partner id, a 16 bit pure number starting with 2088 $alipay_config['partner'] = 'Fill in according to the actual situation'; //The relative path of the merchant's private key (suffix is. pen) file $alipay_config['private_key_path'] = dirname(__FILE__).'/key/rsa_private_key.pem'; //The relative path of Alipay public key (suffix.pen) file $alipay_config['ali_public_key_path']= dirname(__FILE__).'/key/alipay_public_key.pem'; //Signature method $alipay_config['sign_type'] = strtoupper('RSA'); //The character encoding format currently supports gbk or utf-8 $alipay_config['input_charset']= strtolower('utf-8'); //ca certificate path address, used for ssl verification in curl //Please ensure cacert The PEM file is in the current folder directory $alipay_config['cacert'] = dirname(__FILE__).'/cacert.pem'; //The access mode depends on whether your server supports ssl access. If yes, please select https; If not, select http $alipay_config['transport'] = 'http';
2. Callback,
//Alipay callback public function notify_ali() { $configpri=getConfigPri(); require_once(CMF_ROOT."sdk/alipay_app/alipay.config.php"); //Partner id $alipay_config['partner'] = $configpri['aliapp_partner']; require_once(CMF_ROOT."sdk/alipay_app/lib/alipay_core.function.php"); require_once(CMF_ROOT."sdk/alipay_app/lib/alipay_rsa.function.php"); require_once(CMF_ROOT."sdk/alipay_app/lib/alipay_notify.class.php"); Introduce the required documents and allocation information, and fill in according to the actual situation //Calculated notification verification results $alipayNotify = new \AlipayNotify($alipay_config); $verify_result = $alipayNotify->verifyNotify(); $this->logali("ali_data:".json_encode($_POST)); if($verify_result) {//Verification successful //Get business order number, Alipay transaction number, transaction status, etc. according to their actual needs Get required parameters] $out_trade_no = $_POST['out_trade_no']; if($_POST['trade_status'] == 'TRADE_FINISHED') { //Judge whether the order has been processed in the merchant website //If any, write the program according to the actual business }else if ($_POST['trade_status'] == 'TRADE_SUCCESS') { //Judge whether the order has been processed in the merchant website //If not, write the program according to the actual business } }else { $this->logali("Validation failed"); //Validation failed echo "fail"; //Debug and try to write a text function to record whether the program is running normally //logResult("write the value of the code variable you want to debug or other running result records here"); } }
3. Business description for initializing AlipayNotify and obtaining verification results
(1) Verification results
function verifyNotify(){ if(empty($_POST)) {//Judge whether the array from POST is empty return false; }else { //Generate signature results $isSign = $this->getSignVeryfy($_POST, $_POST["sign"]); //Get the result of Alipay remote server ATN (verify whether it is Alipay). $responseTxt = 'false'; if(!empty($_POST["notify_id"])){ $responseTxt= $this->getResponse($_POST["notify_id"]);} //Note: the result of $responsetTxt is not true, which is related to the server settings, partner ID and notify_id is related to one minute failure. The result of isSign is not true. It is related to the security check code, the parameter format at the time of request (e.g. with user-defined parameters), and the coding format if (preg_match("/true$/i",$responseTxt) && $isSign) { return true; } else { return false; } }
(2) Get signature results
function getSignVeryfy($para_temp, $sign) { //Remove null values and signature parameters from the parameter array to be signed $para_filter = paraFilter($para_temp); //Sort the array of parameters to be signed $para_sort = argSort($para_filter); //All elements of the array are spliced into a string with "&" characters according to the mode of "parameter = parameter value" $prestr = createLinkstring($para_sort); $isSgin = false; switch (strtoupper(trim($this->alipay_config['sign_type']))) { case "RSA" : $isSgin = rsaVerify($prestr, trim($this->alipay_config['ali_public_key_path']), $sign); break; default : $isSgin = false; } return $isSgin;
(3) Server ATN results
function getResponse($notify_id) { $transport = strtolower(trim($this->alipay_config['transport'])); $partner = trim($this->alipay_config['partner']); $veryfy_url = ''; if($transport == 'https') { $veryfy_url = $this->https_verify_url; } else { $veryfy_url = $this->http_verify_url; } $veryfy_url = $veryfy_url."partner=" . $partner . "¬ify_id=" . $notify_id; $responseTxt = getHttpResponseGET($veryfy_url, $this->alipay_config['cacert']); return $responseTxt; }
This article briefly introduces the use of Alipay's configuration access and callback when using Alipay recharge when using PHP live source code. If there is a similar demand, it can be referred to simply. If there are other problems or want to develop PHP live source, you can contact the official website for customer service.
Statement: the above contents are original by the author of cloudleopard technology. Reprinting without the consent of the author is prohibited, otherwise relevant legal responsibilities will be investigated