PHP access to the letter sweep Code Payment Summary (summary) _php example

Source: Internet
Author: User

There are two modes of micro-credit-sweep-code payment,

Model one is more complex and requires the public number configuration callback address.

Mode two is easier, just configure the callback address in your code.

I'm using mode two this time.

Requires configuration parameters,

Const APPID = ' xxx ';
Const MCHID = ' xxx ';
Const KEY = ' xxx ';
Const Appsecret = ' xxx ';

Configure the Appid,appsecret of the public number. As well as micro-letter payment of Mchid and key.

Generate two-dimensional code, this page needs to beautify itself, not like Alipay as a result of its own.

Require_once "./phpcms/plugin/weixinpay/lib/wxpay.api.php";
Require_once "./phpcms/plugin/weixinpay/example/wxpay.nativepay.php";
Require_once './phpcms/plugin/weixinpay/example/log.php ';
$input = new Wxpayunifiedorder (); $input->setbody (' reservation '. $product _info[' name '). '
Order "); $input->setattach (' reservation '. $product _info[' name '). '
Order ");
$input->setout_trade_no ($order _info[' OrderNo '));
$input->settotal_fee ($order _info[' payprice '] * 100);
$input->settime_start (Date ("Ymdhis"));
$input->settime_expire (Date ("Ymdhis", Time () + 600));
$input->setgoods_tag (""); $input->setnotify_url ("http://www.ayuanduanzu.com/wxpay/notify.php");
The address is accessible by the extranet and cannot contain parameters $input->settrade_type ("NATIVE");
$input->setproduct_id ($product _info[' id '));
$notify = new Nativepay ();
$result = $notify->getpayurl ($input);
$code _url = $result ["Code_url"]; 

The callback address here is very fastidious, after the success of the sweep code payment, the micro-letter will automatically call this address. This address cannot contain any arguments, otherwise the call fails. I can't see a thing!

When a micro-trust is invoked, a parameter of the XML type is passed.

Include_once ".
/phpcms/base.php ";
Processing callback Data error_reporting (E_ERROR); Require_once ".
/phpcms/plugin/weixinpay/lib/wxpay.api.php "; Require_once '..
/phpcms/plugin/weixinpay/lib/wxpay.notify.php '; Require_once '..
/phpcms/plugin/weixinpay/example/log.php '; Initialize log $logHandler = new Clogfilehandler (". /logs/". Date (' y-m-d ').
Log ");
$log = Log::init ($logHandler, 15); Class Paynotifycallback extends Wxpaynotify//query order Public Function Queryorder ($transaction _id) {$input = new Wxpayorderqu
Ery ();
$input->settransaction_id ($transaction _id);
$result = Wxpayapi::orderquery ($input); Log::D ebug ("Query:").
Json_encode ($result)); if (array_key_exists ("Return_code", $result) && array_key_exists ("Result_code", $result) && $result ["
Return_code "] = =" SUCCESS "&& $result [" result_code "] = =" SUCCESS ") {return true; //Overrides the callback handler function, Public function notifyprocess ($data, & $msg) {Log::D ebug (called back:).
Json_encode ($data));
$notfiyOutput = Array (); if (!array_key_exIsts ("transaction_id", $data)) {$msg = "Incorrect input parameters"; "return false;}//Query order, Judge order authenticity if (! $this->queryorder $data ["
transaction_id "]) {$msg =" order query failed "; return false;} return true;
Log::D ebug ("Begin Notify");
$notify = new Paynotifycallback ();
$ilog _db = Pc_base::load_model (' Ilog_model ');
$order _db = Pc_base::load_model (' Order_model ');
$POSTXML = $GLOBALS ["Http_raw_post_data"];
$POSTARR = Xmltoarray ($POSTXML);
Whether the query was paid successfully $r = $notify->queryorder ($postArr [' transaction_id ']); if ($r) {//Get order information $order _info = $order _db->get_one (Array (' OrderNo ' => $postArr [' out_trade_no ']); if ($order _info [' Pay_status ']!= ' 1 ') {$data [' pay_status '] = ' 1 '; $data [' pay_type '] = ' weixinpay '; $data [' pay_code '] = $postArr [' Transac
Tion_id '];
$data [' paytime '] = time (); $data [' Order_status ']= 3;
Paid $order _db->update ($data, Array (' OrderNo ' => $postArr [' out_trade_no '])); }?>

Pass

$GLOBALS ["Http_raw_post_data"];

Can get the parameters passed in by the micro-end

{"
AppID": "wxed7996e9ad58345d", "
Attach": " u9884u8ba2u5bbfu8fc1u00b7u592au53e4u91ccu7f8eu5f0fu5957u623fu8ba2u5355 ",
" Bank_type ":" CFT ",
" Cash_fee " : "1",
"Fee_type": "CNY",
"Is_subscribe": "Y",
"mch_id": "1283301801",
"Nonce_str": " 20xn5e0lbk2u1u6pes2uonape2sdyfs4 ",
" OpenID ":" Or8c7wsknwvelirrztlzx2eonwey ",
" Out_trade_no ":" 2016091455521024608 ",
" Result_code ":" SUCCESS ",
" Return_code ":" SUCCESS ",
" sign ":" 95c2c532d095e7bf7588522c579758c4 ",
" Time_end ":" 20160914135518 ",
" Total_fee ":" 1 ",
" Trade_type ":" NATIVE ",
" transaction_id ":" 4009602001201609143926590576 "
}

Check whether the payment has been paid, the payment completed, the order data processing.

Everything here is asynchronous, the two-dimensional code page can not see anything.

You can only obtain order status asynchronously to determine if the operation is successful.

Detect if payment succeeded
$ (document). Ready (function () {
setinterval ("Ajaxstatus ()", 3000); 

function Ajaxstatus () {
var OrderNo = $ ("#out_trade_no"). Val ();
if (OrderNo!= 0) {
$.ajax ({
URL: "? m=home&c=order&a=ajax",
type: "Get",
DataType: "JSON",
data: {
TODO: ' Ajaxcheckwxpay ',
Orderno:orderno,
},
success:function (JSON) {
if ( Json.status = = 1) {//Order status of 1 indicates successful payment
layer.msg (' pay success ', {icon:1,time:2000},function () {
window.location.href = "m=home&c=order&a=paydone&orderno=" +json.info[' OrderNo '];
Window.location.href = "Wxscansuccessurl.action"; Page Jump
}}}
);
}

Three seconds to execute once, if successful, jump processing.

Gift function

* Function: Array to XML
/function Arraytoxml ($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; 

* Function: Convert XML to array
/function Xmltoarray ($xml)
{ 
//convert XML to array 
$array _data = Json_decode ( Json_encode (simplexml_load_string ($xml, ' simplexmlelement ', Libxml_nocdata)), true); 
return $array _data;
}

Gift Tips

For an asynchronous call, if the effect is not visible. You can build a log table that records the operation's data in the table. Easy to test. The payment callback is asynchronous and can be judged by the data in the log table to determine whether the payment was successful, whether the callback was invoked, and how many times it was invoked.

Summary:

Micro-letter sweep code payment is not as easy as paying treasure sweep code payment. Need to do a lot of processing yourself.

The above is a small set to introduce the PHP micro-Scan Code Payment Access Summary (summary), the database shows the perfect solution blank (picture and text tutorial), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.