PHP Micro-Credit Payment example detailed

Source: Internet
Author: User
Tags openid
This article is mainly for everyone to introduce the PHP payment example, including PHP payment source code, PHP refund source code, with a certain reference value, interested in small partners can refer to

Specific content is as follows

1.JSapi Payment Demo (click on the client)

<?php/** * Js_api Pay Demo * ==================================================== * in the browser Open H5 Web page to execute JS to pay. The interface input and output data format is JSON. * Three steps required to successfully adjust the payment: * Step 1: Web authorization to get user OpenID * Step 2: Use the unified payment interface, get prepay_id * Step 3: Use JSAPI to pay */include_once (".  /wxpaypubhelper/wxpaypubhelper.php "); Using the JsApi interface $jsApi = new Jsapi_pub (); ========= Step 1: Web authorization to get the user openid============//through code to obtain OpenID if (!isset ($_get[' Code ")) {//Trigger return code code $URL = $jsApi Createoauthurlforcode (Wxpayconf_pub::js_api_call_url);  Header ("Location: $url");  }else {//Get code to get OpenID $code = $_get[' code ']; $jsApi->setcode ($code); $openid = $jsApi->getopenid ();}  ========= Step 2: Use the unified payment interface to obtain prepay_id============//using the Unified Payment interface $unifiedOrder = new Unifiedorder_pub (); Set the unified Payment interface parameters//settings Required Parameters//appid is filled, the merchant does not need to fill in the//mch_id is filled, the merchant does not need to fill in//noncestr, the merchant does not need to fill in the//spbill_create_ip, the merchant does not need to fill in again/ Sign is filled in, the merchant does not need to fill in the $unifiedOrder->setparameter ("OpenID", "$openid");//Product Description $unifiedOrder->setparameter ("Body", "Contribute a penny");//Commodity Description//Custom order number, here only 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 parameters, the merchant can be selected according to the actual situation//$unifiedOrder->setparameter ("sub_mch_id", " XXXX ")//Sub-merchant number//$unifiedOrder->setparameter (" Device_info "," XXXX ");//device number//$unifiedOrder->setparameter (" Attach "," xxxx ");//Additional data//$unifiedOrder->setparameter (" Time_start "," xxxx ");//Trading start time//$unifiedOrder Setparameter ("Time_expire", "xxxx");//Trade end time//$unifiedOrder->setparameter ("Goods_tag", "xxxx");//commodity Mark//$ Unifiedorder->setparameter ("OpenID", "xxxx");//user ID//$unifiedOrder->setparameter ("product_id", "xxxx");// Product ID $prepay _id = $unifiedOrder->getprepayid (); ========= Step 3: Use JsApi to adjust the payment ============ $jsApi->setprepayid ($prepay _id); $jsApiParameters = $jsApi->getparameters (); Echo $jsApiparameters;? >

2.native Payment mode one demo (static link with sweep QR code)

<?php/** * Native (native) payment mode a demo * ==================================================== * Mode one: Merchants in a fixed format to generate the link QR code, the user sweep code after the tune * ProductID and User OpenID will be sent to the Merchant settings link, the merchant receives * request to generate orders, call the unified payment interface to submit the order, will return * to the merchant Prepayid. * The QR code corresponding to this routine is generated by native_call_qrcode.php; * The response service corresponding to this routine is native_call.php; * need to be used together. */Include_once (".. /wxpaypubhelper/wxpaypubhelper.php "); Set static link $nativeLink = new nativelink_pub ();   Set static link parameters//settings required parameters//appid filled, merchants do not need to fill//mch_id filled, merchants do not need to fill in the//noncestr, the merchant does not need to fill in the//time_stamp, the merchant does not need to fill in the//sign is filled, Merchants do not need to fill in the $product _id = wxpayconf_pub::appid. " Static ";//Custom Product ID $nativeLink->setparameter (" product_id "," $product _id ");//commodity ID//Get link $product _url = $ Nativelink->geturl (); Use the short link to convert the interface $SHORTURL = new Shorturl_pub (); Set Required parameters//appid has been filled, merchants do not need to fill//mch_id filled, the merchant does not need to fill in the//noncestr, the merchant does not need to fill in the//sign, the merchant does not need to fill in the duplicate $shortUrl->setparameter ( "Long_url", "$product _url");//url link//Get short link $CODEURL = $shortUrl->getshorturl ();?>

3.native Payment Mode two demo (dynamic link QR code with sweep)

 <?php/** * Native (native) payment-mode two-demo * ==================================================== * merchant Generate order, first call the unified payment interface get to code _url, * This URL directly generated two-dimensional code, the user sweep code after the payment. * * */include_once (". /wxpaypubhelper/wxpaypubhelper.php ");  Use the unified payment interface $unifiedOrder = new Unifiedorder_pub (); Set the unified Payment interface parameters//settings Required Parameters//appid is filled, the merchant does not need to fill in the//mch_id is filled, the merchant does not need to fill in//noncestr, the merchant does not need to fill in the//spbill_create_ip, the merchant does not need to fill in again/ Sign is filled, the merchant does not need to fill in the $unifiedOrder->setparameter ("Body", "Contribution a Penny");//Commodity Description//Custom order number, here only 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", "NATIVE");//transaction type//non-required parameters, the merchant can be selected according to the actual situation//$unifiedOrder->setparameter ("sub_mch_id", " XXXX ")//Sub-merchant number//$unifiedOrder->setparameter (" Device_info "," XXXX ");//device number//$unifiedOrder->setparameter ("  Attach "," XXXX ");//Additional data$unifiedOrder->setparameter ("Time_start", "XXXX");//Trading start time//$unifiedOrder->setparameter ("Time_expire", " XXXX ")///$unifiedOrder->setparameter (" Goods_tag "," xxxx ");//commodity mark//$unifiedOrder->setparameter (" OpenID "," xxxx ");//user ID//$unifiedOrder->setparameter (" product_id "," xxxx ");//commodity ID//Get Unified Payment Interface Results $  Unifiedorderresult = $unifiedOrder->getresult (); The merchant sets the appropriate processing process according to the actual situation if ($unifiedOrderResult ["return_code"] = = "FAIL") {//merchant self-increment processing process echo "Communication error:". $unifiedOrderResult [ ' Return_msg ']. " <br> "; } elseif ($unifiedOrderResult ["result_code"] = = "FAIL") {//merchant to increase the processing process by itself echo "error code:". $unifiedOrderResult [' Err_code ']. " <br> "; echo "Error code Description:". $unifiedOrderResult [' Err_code_des ']. " <br> "; } elseif ($unifiedOrderResult ["Code_url"]! = NULL) {//obtained from the unified payment interface to Code_url $code _url = $unifiedOrderResult ["Code_url"]; /merchants to increase their own processing flow//...}? >

4. Payment Query Interface Demo

<?php/** * Order Enquiry-demo * ==================================================== * This interface provides a query for all payment orders. * When payment notifications are handled abnormally or lost, the merchant can query the order payment status through this interface. * * */include_once (".  /wxpaypubhelper/wxpaypubhelper.php "); Refund Order number if (!isset ($_post["Out_trade_no")) {$out _trade_no = "";}  else{$out _trade_no = $_post["Out_trade_no"];//Use order query Interface $orderQuery = new Orderquery_pub ();//Set Required parameters//appid is filled, merchants do not need to re-fill MCH_ID has been filled, the merchant does not need to fill in//noncestr, the merchant does not need to fill in the//sign, the merchant does not need to fill in the $orderQuery->setparameter ("Out_trade_no", "$out _ Trade_no "),//Merchant order number//non-required parameters, the merchant can be selected according to the actual situation//$orderQuery->setparameter (" sub_mch_id "," XXXX ");//Sub-merchant number//$orderQuery-  >setparameter ("transaction_id", "XXXX");//order number//Get order query Result $orderQueryResult = $orderQuery->getresult (); The merchant sets the appropriate processing flow according to the actual situation, here only for example if ($orderQueryResult ["return_code"] = = "FAIL") {echo "Communication error:". $orderQueryResult [' Return_ Msg ']. " <br> "; } elseif ($orderQueryResult ["result_code"] = = "FAIL") {echo "Error code:". $orderQueryResult [' Err_code ']. " <br> "; echo "Error code Description:". $orderQueryResult ['Err_code_des ']. " <br> "; } else{echo "Trading Status:" $orderQueryResult [' trade_state ']. " <br> "; echo "Device number:". $orderQueryResult [' Device_info ']. " <br> "; echo "User id:". $orderQueryResult [' OpenID ']. " <br> "; Echo "is concerned about public accounts:". $orderQueryResult [' Is_subscribe ']. " <br> "; echo "Transaction type:". $orderQueryResult [' Trade_type ']. " <br> "; echo "Payment Bank:". $orderQueryResult [' Bank_type ']. " <br> "; echo "Total amount:" $orderQueryResult [' Total_fee ']. " <br> "; echo "Cash voucher Amount:" $orderQueryResult [' Coupon_fee ']. " <br> "; echo "Currency type:". $orderQueryResult [' Fee_type ']. " <br> "; echo "Pay Order Number:" $orderQueryResult [' transaction_id ']. " <br> "; echo "Merchant Order Number:" $orderQueryResult [' out_trade_no ']. " <br> "; echo "Merchant packet:". $orderQueryResult [' Attach ']. " <br> "; echo "Payment completion time:" $orderQueryResult [' time_end ']. " <br> "; }}//merchants to increase their own processing process//...? >

5. Billing Interface Demo

<?php/** * Billing Interface Demo * ==================================================== * merchant can download the history trade list via this interface. */Include_once (".. /wxpaypubhelper/wxpaypubhelper.php ");  The statement date if (!isset ($_post["Bill_date"])) {$bill _date = "20140814";} else{$bill _date = $_post["Bill_date"]; Use the statement interface $downloadBill = new Downloadbill_pub (); Set the statement interface parameters//settings Required Parameters//appid is filled, merchants do not need to fill in the//mch_id is filled, merchants do not need to fill in the//noncestr, merchants do not need to fill in the//sign, the merchant does not need to fill in the duplicate $downloadBill- >setparameter ("Bill_date", "$bill _date"),//Statement date $downloadBill->setparameter ("Bill_type", "all");//billing type// Non-mandatory parameters, the merchant can be selected according to the actual situation//$downloadBill->setparameter ("Device_info", "XXXX");//device number//Statement interface result $downloadBillResult = $ Downloadbill->getresult ();  echo $downloadBillResult [' Return_code ']; if ($downloadBillResult [' return_code '] = = "FAIL") {echo ' Communication error: '. $downloadBillResult [' return_msg '];} else{print_r (' <pre> '); echo "statement details". " </br> "; Print_r ($downloadBill->response); Print_r (' </pre> '); }}?>

6. Refund Interface Demo

<?php/** * Refund Request Interface-demo * ==================================================== * NOTE: Partial refunds for the same order need to be set with the same tick number and different * out_ Refund_no. A refund failed to resubmit, to use the original * out_refund_no. The total refund amount cannot exceed the actual amount paid by the user (the current * Gold voucher amount cannot be refunded). */Include_once (".. /wxpaypubhelper/wxpaypubhelper.php "); Enter the order number to be refunded if (!isset ($_post["Out_trade_no"]) | |!isset ($_post["Refund_fee")) {$out _trade_no = ""; $refund _fee = "1";  }else{$out _trade_no = $_post["Out_trade_no"]; $refund _fee = $_post["Refund_fee"]; Merchant refund number, merchant custom, here only for example $out _refund_no = "$out _trade_no". " $time _stamp ";  The total amount must correspond with the order number Out_trade_no, the total amount of all orders in the demo is 1 points $total _fee = "1"; Use the refund interface $refund = new Refund_pub (); Set Required parameters//appid has been filled, merchants do not need to fill//mch_id filled, merchants do not need to fill in the//noncestr, the merchant does not need to fill in the//sign, the merchant does not have to repeat the $refund->setparameter (" Out_trade_no "," $out _trade_no "),//Merchant order number $refund->setparameter (" Out_refund_no "," $out _refund_no ");//merchant refund No. Refund->setparameter ("Total_fee", "$total _fee");//Total Amount $refund->setparameter ("Refund_fee", "$refund _fee");// Refund Amount $refund->setparameter ("op_user_id", wxpayconf_pub::mchid);//operator//non-required parameters, the merchant can be selected according to the actual situation//$refund->setparameter (" sub_mch_id "," XXXX ");//Sub-merchant number//$ Refund->setparameter ("Device_info", "xxxx");//device number//$refund->setparameter ("transaction_id", "xxxx");//order number//  Call result $refundResult = $refund->getresult (); The merchant sets the appropriate processing flow according to the actual situation, here only for example if ($refundResult ["return_code"] = = "FAIL") {echo "Communication error:". $refundResult [' return_msg ']. " <br> "; } else{echo "Business results:". $refundResult [' Result_code ']. " <br> "; echo "Error code:". $refundResult [' Err_code ']. " <br> "; echo "Error code Description:". $refundResult [' Err_code_des ']. " <br> "; echo "Public account ID:" $refundResult [' AppID ']. " <br> "; echo "Merchant number:" $refundResult [' mch_id ']. " <br> "; echo "Sub-merchant Number:" $refundResult [' sub_mch_id ']. " <br> "; echo "Device number:". $refundResult [' Device_info ']. " <br> "; echo "Signature:" $refundResult [' sign ']. " <br> "; echo "Order number:" $refundResult [' transaction_id ']. " <br> "; echo "Merchant Order Number:" $refundResult [' out_trade_no ']. " <br> "; echo "Merchant Refund Number:". $refundResult [' Out_refund_no ']. " <Br> "; echo "Refund number:" $refundResult [' refund_idrefund_id ']. " <br> "; echo "Refund Channel:". $refundResult [' Refund_channel ']. " <br> "; echo "Refund Amount:" $refundResult [' Refund_fee ']. " <br> "; echo "Cash voucher refund Amount:" $refundResult [' Coupon_refund_fee ']. " <br> "; }}?>

7. Refund Enquiry Interface Demo

<?php/** * Refund Request Interface-demo * ==================================================== * * */include_once (". /wxpaypubhelper/wxpaypubhelper.php "); Order number to query if (!isset ($_post["Out_trade_no")) {$out _trade_no = "";}  else{$out _trade_no = $_post["Out_trade_no"]; Use the refund query interface $refundQuery = new Refundquery_pub (); Set Required parameters//appid has been filled, merchants do not need to fill//mch_id filled, the merchant does not need to fill in the//noncestr, the merchant does not need to fill in the//sign, the merchant does not need to repeat the fill $refundQuery Setparameter ("Out_trade_no", "$out _trade_no");//Merchant Order number//$refundQuery->setparameter ("Out_refund_no", "XXXX");// Merchant Refund Number//$refundQuery->setparameter ("refund_id", "XXXX"),//Refund number//$refundQuery->setparameter ("Transaction_ ID "," xxxx ");//refund number//non-required parameters, the merchant can be selected according to the actual situation//$refundQuery->setparameter (" sub_mch_id "," xxxx ");//Sub-merchant No.//$ Refundquery->setparameter ("Device_info", "XXXX");//device number//Refund query interface result $refundQueryResult = $refundQuery  GetResult (); The merchant sets the appropriate processing flow according to the actual situation, here only for example if ($refundQueryResult ["return_code"] = = "FAIL") {echo "Communication error:". $refundQueryResult [' Return _msg ']. " <br>"; } else{echo "Business results:". $refundQueryResult [' Result_code ']. " <br> "; echo "Error code:". $refundQueryResult [' Err_code ']. " <br> "; echo "Error code Description:". $refundQueryResult [' Err_code_des ']. " <br> "; echo "Public account ID:" $refundQueryResult [' AppID ']. " <br> "; echo "Merchant number:" $refundQueryResult [' mch_id ']. " <br> "; echo "Sub-merchant Number:" $refundQueryResult [' sub_mch_id ']. " <br> "; echo "Device number:". $refundQueryResult [' Device_info ']. " <br> "; echo "Signature:" $refundQueryResult [' sign ']. " <br> "; echo "Order number:" $refundQueryResult [' transaction_id ']. " <br> "; echo "Merchant Order Number:" $refundQueryResult [' out_trade_no ']. " <br> "; echo "Number of refund pens:" $refundQueryResult [' Refund_count ']. " <br> "; echo "Merchant Refund Number:". $refundQueryResult [' Out_refund_no ']. " <br> "; echo "Refund number:" $refundQueryResult [' refund_idrefund_id ']. " <br> "; echo "Refund Channel:". $refundQueryResult [' Refund_channel ']. " <br> "; echo "Refund Amount:" $refundQueryResult [' Refund_fee ']. " <br> "; echo "Cash voucher refund Amount:" $refundQueryResult [' Coupon_refund_fee ']. " <br> "; echo "RetreatStatus: ". $refundQueryResult [' Refund_status ']." <br> "; }}?>

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.