PHP micro-Credit Payment interface development Program _php skills

Source: Internet
Author: User
Tags cdata curl error code flock openid

PHP micro-Credit Payment interface development program to explain :

Necessary:
AppID//Public Number Background Developer Center Access (same as in mail)

mchid//in Mail

key//Merchant backstage oneself set up

Appsecret//Public Number Developer Center Access
Two certificate files, received Apiclient_cert.pem apiclient_key.pem in Mail
Precautions :
Public number background micro-letter Payment-"Development configuration-" New test directory and test personal micro-signal.
Developer Center-"website authorization to get user profile-" modify it to your test domain. Otherwise a Redirect_uri parameter error will occur
—————————— follow-up to be completed ——————-
The micro-credit payment ready page backstage three times to operate:

1. Get OpenID

Use the Jsapi interface
 
 code to copy the code as follows
  $jsApi = new Jsapi_pub ();
 
  ========= Step 1: Web page authorization to obtain the user openid============//
  via code to obtain OpenID
  if (!isset ($_get[' Code '))
  {
    // Trigger the micro-letter return code code
    $url = $jsApi->createoauthurlforcode (wxpayconf_pub::js_api_call_url);
    echo $url;
    Header ("Location: $url");
  } else
  {
    //Get code code to get OpenID
    $code = $_get[' code '];
    $jsApi->setcode ($code);
    $openid = $jsApi->getopenid ();
  }

At the beginning of the first step also encountered problems, can not get OpenID this and some of the server-related, demo is used in the way curl get.
Strange that my server curl has been unable to get. Later changed into file_get_contents can be normal access.
But that's not the way to solve it. Because more curl operations are needed later.
See the development document there is a place to write a certificate operation requires Libcurl 7.20.1 version, and then I have been in the whole server want to improve the Linux PHP Curl version. The last thing I did was switch to another Windows server.
Let's do this for a while, and then debug it the next time you need it.

Step two: get and pay Order number ID
The code is as follows

$unifiedOrder = new Unifiedorder_pub ();
  Var_dump ($unifiedOrder); 
  Set up uniform Payment interface parameters//set Required parameters//appid has been filled out, the merchant does not need to fill in the//mch_id has been filled, the merchant does not need to fill in the duplicate//noncestr has filled in, the merchant does not need to repeat fills//spbill_create_ip has filled, the merchant does not need Sign has been filled in, the merchant does not need to fill in $unifiedOrder->setparameter ("OpenID", "$openid");//Product Description $unifiedOrder->setparameter ("body
  "," Contribution to a Penny ")//Product Description/Custom order number, here for example $timeStamp = time (); $out _trade_no = wxpayconf_pub::appid. "
  $timeStamp "; $unifiedOrder->setparameter ("Out_trade_no", "$out _trade_no");//Merchant Order number $unifiedOrder->setparameter ("Total_ Fee "," 1 ")//Total amount $unifiedOrder->setparameter (" Notify_url ", wxpayconf_pub::notify_url);//notification address $unifiedOrder-> Setparameter ("Trade_type", "JSAPI")//transaction type//non-required, the merchant may fill in the actual situation by filling//$unifiedOrder->setparameter ("sub_mch_id", " XXXX ");//Merchant number//$unifiedOrder->setparameter (" Device_info "," XXXX ");//device number//$unifiedOrder->setparameter (" Attach "," xxxx ")//Additional data//$unifiedOrder->setparameter (" Time_start "," xxxx "),//Transaction start time//$unifiedOrder->sEtparameter ("Time_expire", "xxxx");//transaction end time//$unifiedOrder->setparameter ("Goods_tag", "xxxx");//mark of goods//$ Unifiedorder->setparameter ("OpenID", "xxxx");//user identification//$unifiedOrder->setparameter ("product_id", "xxxx");
   
  Product ID $prepay _id = $unifiedOrder->getprepayid ();
  Echo ' prepay_id: ';

 Var_dump ($prepay _id);

This step also encountered a lot of problems.
First, the micro-credit payment test is more difficult, only in the micro-letter can be tested. I use the phone brush to brush.
Second, the use of Var_dump debugging is not a good amount. Printing some XML-formatted files displays only the character length and does not display the content. So log in the form of writing on the server debugging, log code:
The code is as follows

Print log
  function log_d ($word) 
  {
    $log _name= "./logd.log";//log file path
    $fp = fopen ($log _name, "a");
    Flock ($FP, lock_ex);
    Fwrite ($FP, "date of execution:". Strftime ("%y-%m-%d-%h:%m:%s", Time ()). " N ". $word." nn ");
    Flock ($FP, lock_un);
    Fclose ($FP);
  

In the demo inside the wxpaypubhelper.php inside with $this->log_d (XXX);
At the beginning, due to my mchid and AppID do not match has been an error. They gave me the wrong account. At first I didn't know how to try. This step is debugged in Getprepayid () var_dump ($this->result); You can see the error code.

Step three: generate the payment front-end JS code ready to the Web page:
The code is as follows

$jsApi->setprepayid ($prepay _id);
 
$jsApiParameters = $jsApi->getparameters ();

———————-Click to go to pay ————————-

This part has also encountered the problem:
Android returns to "System:access_denied" and iOS returns to "access_control:not_allowed"
searched a lot of Baidu. Actually already saw this thing to have not noticed!
The page on which the authorization request originated must be a page under the authorization directory, not a subdirectory. Otherwise, an error is returned
Payment file I put in the/domain/pay/demo/
At the beginning of the time I have been to/domain/pay/end thought it can be. Support subdirectories, the result is not!.
————————— finally look at the figure —————

Wxpay1
Wxpay3
Wxpay2

————— – The Xmljs —————— in the process –
Build and Pay Order ID to be submitted:
The code is as follows

<xml>
 <openid><![ Cdata[ou9dht0l8qfli1fop-kj5x1mdwsm]]></openid>
 <body><![ cdata[contribution]]></body>
 <out_trade_no><![ cdata[wx88888888888888881414411779]]></out_trade_no>
 <total_fee>1</total_fee>
 <notify_url><! [cdata[http://shanmao.me/wxpay/notify_url.php]]></notify_url>
 <trade_type><![ Cdata[jsapi]]></trade_type>
 <appid><![ cdata[wx8888888888888888]]></appid>
 <mch_id>10012345</mch_id>
 <spbill_create_ ip><! [cdata[61.50.221.43]]></spbill_create_ip>
 <nonce_str><![ Cdata[60uf9sh6nmppr9azveb2bn7arhy79izk]]></nonce_str>
 <sign><![ Cdata[2d8a96553672d56bb2908ce4b0a23d0f]]></sign>
</xml>

Returned correctly after submission, which contains the perpay_id:

<xml>
 <return_code><![ Cdata[success]]></return_code> 
 <return_msg><![ Cdata[ok]]></return_msg> 
 <appid><![ Cdata[wx8888888888888888]]></appid> 
 <mch_id><![ Cdata[10012345]]></mch_id> 
 <nonce_str><![ Cdata[be8yx7gjcdtct7cr]]></nonce_str> 
 <sign><![ Cdata[885b6d84635ae6c020ef753a00c8eedb]]></sign> 
 <result_code><![ Cdata[success]]></result_code> 
 <prepay_id><![ Cdata[wx201410272009395522657a690389285100]]></prepay_id> 
 <trade_type><![ Cdata[jsapi]]></trade_type> 
</xml>

Generate the payment with JS:

{"
  appId": "wx8888888888888888", "
  TimeStamp": "1414411784",
  "Noncestr": " Gbwr71b5no6q6ne18c8up1u7l7he2y75 ","
  Package ":" prepay_id=wx201410272009395522657a690389285100 ",
  " Signtype ":" MD5 ","
  paysign ":" 9c6747193720f851eb876299d59f6c7d "
}

Notification XML returned when payment is successful:

<xml><appid><! [cdata[wx8888888888]]></appid> <bank_type><! [cdata[ccb_debit]]></bank_type> <fee_type><! [cdata[cny]]></fee_type> <is_subscribe><! [cdata[y]]></is_subscribe> <mch_id><! [cdata[1011111]]></mch_id> <nonce_str><! [cdata[38gt0ffgsvfsdfsdfbt1981duv63p7]]></nonce_str> <openid><! [cdata[o4p3sjfdsfdsfdsdce5y2xhw4]]></openid> <out_trade_no><! [cdata[wx4b56d1fsdfdsf416643247]]></out_trade_no> <result_code><! [cdata[success]]></result_code> <return_code><! [cdata[success]]></return_code> <sign><! [cdata[356efsdfdsfsdsfe69509eda344]]></sign> <sub_mch_id><! [cdata[10018826]]></sub_mch_id> <time_end><! [cdata[20141122160122]]></time_end> <total_fee>1</total_fee> <trade_type><! [cdata[jsapi]]></trade_type> <transaction_id><!

 [cdata[100715001020fsdfsd1220006123174]]></transaction_id> </xml>

This one of the data I random, we refer to the format bar. Note In addition the case sensitivity.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.