thinkphp Frame docking Alipay instant to account interface Callback operation Example _php instance

Source: Internet
Author: User
Tags explode md5 php template smarty template

The example of this article tells the thinkphp framework docking Alipay real-time to account interface callback operation. Share to everyone for your reference, specific as follows:

About Alipay Real-time payment interface docking process, very simple, there are a lot of people sent, I am not long-winded here, docking completed, online payment after the success of the callback, relatively speaking, is a difficult point, I focus on sharing my experience.

When I was developing the second generation tourism CMS (http://www.erdaicms.com), I spent a lot of time in the callback.

Whether the Alipay interface is good for the micro-credit payment interface, callbacks are divided into a hop-and-switch and asynchronous notification callback, jump callback is not insured, to join the guest payment is completed immediately after the payment page closed, did not jump to notice you this order has been paid, so we want to use the asynchronous notification callback:

Copy Code code as follows:
$alipay _config[' notify_url '] = "". $ss [' Web_url ']. " /v.php/index-alipay_notify_url.html ";

First set the address that describes the asynchronous callback

The specific processing function of the asynchronous callback, I also paste the processing here for reference:

/* Alipay asynchronous Notification/Public function Alipay_notify_url () {Vendor (' alipay.corefunction ');
  Vendor (' alipay.md5function ');
  Vendor (' alipay.notify ');
  Vendor (' Alipay.submit ');
  $info =m (' rewrite ')->where (Array (' name ' => ' Alipay '))->find ();
  $info =json_decode ($info [' content '],true);; ↓↓↓↓↓↓↓↓↓↓ Please configure your basic information here ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//ID, signing account, a string of 16-digit pure digits at the beginning of 2088, view address: https://b.alipay.com/order/
pidandkey.htm $alipay _config[' partner '] = $info [' Alipay_pid '];
Payment Alipay account number, with 2088 beginning by the 16-digit pure numbers of strings, in general, the billing account is the signing account $alipay _config[' seller_id '] = $info [' Alipay_pid ']; MD5 key, security check code, 32-bit string consisting of numbers and letters, view address: https://b.alipay.com/order/pidAndKey.htm $alipay _config[' key ' = $info [' Alipay
_key '];
$ss =s (' config '); The Server Asynchronous notification page path needs to http://the full path of the format, can not add? id=123 this kind of custom parameter, must extranet can normal access $alipay _config[' notify_url '] = "". $ss [' Web_url ']. "
/v.php/index-alipay_notify_url.html "; Page Jump sync notification page path needs to http://the full path of the format, can not add? id=123 this kind of custom parameter, must extranet can normal access $alipay _config[' return_url '] = "". $ss [' Web_url ']. "
/member.php "; Signature method $alipay _config[' sign_type '] = strtoupper (' MD5 ');
The character encoding format currently supports GBK or utf-8 $alipay _config[' Input_charset ']= strtolower (' utf-8 '); CA certificate path address, for SSL checksums in curl//Make sure CACERT.PEM files are $alipay _config[' cacert '] = GETCWD () in the current folder directory. '
\\cacert.pem ';
Access mode, depending on whether your server supports SSL access, if support, please select HTTPS; if not supported please select HTTP $alipay _config[' transport '] = ' http ';
Payment type, without modification $alipay _config[' payment_type '] = "1";
Product type, no need to modify $alipay _config[' service '] = "create_direct_pay_by_user"; ↑↑↑↑↑↑↑↑↑↑ Please configure your basic information here ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//↓↓↓↓↓↓↓↓↓↓ please configure the anti-phishing information here, if the fishing function is not opened, you can ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//fishing time stamp
To use, call the Query_timestamp function in the class file submit $alipay _config[' anti_phishing_key '] = "";
  The IP address of the client is not the extranet IP address of the LAN, such as: 221.0.0.1 $alipay _config[' exter_invoke_ip '] = "";
  $alipayNotify = new \alipaynotify ($alipay _config);
  $verify _result = $alipayNotify->verifynotify (); if ($verify _result) {//Verify success///////////////////////////////////////////////////////////////////////////////////// Please add the merchant's business logic program here.//--please write the program according to your business logic (the following code onlyFor reference)--//obtain Alipay notification return parameters, refer to the technical documentation of the server asynchronous notification parameter list//merchant order number $out _trade_no = $_post[' out_trade_no '];
  Alipay transaction number $trade _no = $_post[' trade_no '];
  Transaction status $trade _status = $_post[' trade_status '];
  $total _fee=$_post[' Total_fee ']; if ($info [' Alipay_pid ']!= $seller _id) return ";
  The receivable account is inconsistent $info =m (' Order ')->where (Array (' No ' => $out _trade_no))->find (); if (! $info) return "; The order number does not exist if ($_post[' trade_status '] = = ' trade_finished ') {//Determine if the order has been processed on the merchant website///If no processing has been done, according to the order number (Out_trade_no In the order system of the merchant website, find out the details of the order, and execute the business procedure of the merchant//Please make sure to judge the Total_fee, seller_id and Total_fee when the notice is requested, seller_id for the same//if there has been handled, do not execute the merchant's
      Business process $data = ';
      $data [' Status ']=2;
      $data [' Pay_price ']= $total _fee;
      $data [' Pay_type ']= ' Alipay ';
      $data [' Buyer ']=$_post[' buyer_email '];
      $data [' trade_no ']= $trade _no;
      $data [' Pay_time ']=time ();
      M (' Order ')->where (Array (' No ' => $out _trade_no))->save ($data);
 $c =m (' smtp_templates ')->where (array (' ID ' =>5))->find ();     if ($c [' status ']==1] {$content = Str_replace (' {title} ', $info [' Goods_name '], $c [' content ']);
        $content = Str_replace (' {ID} ', $info [' goods_id '], $content);
        $content = Str_replace (' {price} ', $total _fee, $content);
       $content = Str_replace (' {time} ', date (' y-m-d h:i:s ', Time ()), $content);
       $c =m (' Email_note ')->where (Array (' pay_id ' => $out _trade_no))->find ();
       if (! $c) {$e = ';
       $e [' Email ']= $info [' C_email '];
       $e [' content ']= $content;
       $e [' pay_id ']= $out _trade_no;
       M (' Email_note ')->add ($e);
       $m =explode (' | ', $c [' ather ']);
         foreach ($m as $mail) {if (Validate_email ($mail)) {$e [' email ']= $mail;
        M (' Email_note ')->add ($e); }}//NOTE://Refund date is beyond the refund period (such as three months refund), Alipay system send the transaction status notice//Tune trial, write text function to record the operation of the program is normal//logresu
  LT ("here writes the value of the code variable you want to debug, or other running result records"); else if ($_post[' trade_status '] = = ' trade_success ') {//Judge the penWhether the order has been processed in the Merchant website//If no processing has been done, the order number (OUT_TRADE_NO) is found in the Merchant website Order system in detail, and the business process of the merchant is executed//Please be sure to judge the request Total_fee, seller_id and
      Notice when the Total_fee, seller_id for the same//if have done processing, do not carry out business procedures $data = ';
      $data [' Status ']=2;
      $data [' Pay_price ']= $total _fee;
      $data [' Pay_type ']= ' Alipay ';
      $data [' Buyer ']=$_post[' buyer_email '];
      $data [' trade_no ']= $trade _no;
      $data [' Pay_time ']=time ();
       M (' Order ')->where (Array (' No ' => $out _trade_no))->save ($data);
      $c =m (' smtp_templates ')->where (array (' ID ' =>5))->find ();
        if ($c [' status ']==1] {$content = Str_replace (' {title} ', $info [' Goods_name '], $c [' content ']);
        $content = Str_replace (' {ID} ', $info [' goods_id '], $content);
        $content = Str_replace (' {price} ', $total _fee, $content);
       $content = Str_replace (' {time} ', date (' y-m-d h:i:s ', Time ()), $content);
       $c =m (' Email_note ')->where (Array (' pay_id ' => $out _trade_no))->find ();
       if (! $c) {$e = '; $e[' Email ']= $info [' C_email '];
       $e [' content ']= $content;
       $e [' pay_id ']= $out _trade_no;
       M (' Email_note ')->add ($e);
       $m =explode (' | ', $c [' ather ']);
         foreach ($m as $mail) {if (Validate_email ($mail)) {$e [' email ']= $mail;
        M (' Email_note ')->add ($e); }}//Note: When payment is completed, the Alipay system sends the transaction status notice//Tune trial, write text function to record the operation of the program is normal//logresult ("Here write the code you want to debug changes
  Measure, or other running results record ");    //--please write the program according to your business logic (the above code is for reference only)--echo "success"; Please do not modify or delete/////////////////////////////////////////////////////////////////////////////////////////////////////
  else {//validation failed echo "fail";
For debugging purposes, write the 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");

 }
}

Because it is asynchronous notification, so debugging is a nuisance, can not see that the results, that is, generally no way echo or print_r print output, no way to know where he executes or execution results, this time, we will use log file output, code:

$file = './log.txt '; the filename (can be any file name) to be written to the file, and if the file does not exist, a
$content = "Payment Success" will be created. $bdata [' Total_fee ']. " \ n "; The content to be written
file_put_contents ($file, $content, file_append);/write to File

The above code will automatically generate a Log.txt file in the root directory, so that you can easily know the implementation of the results, easy to debug

Alipay's asynchronous notification interface, will be repeatedly notified until you return to success, that is, you submit a test order, you can test many times, because you did not return to success, he will be at a certain time, notice

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.