PHP WeChat payment instance resolution _ php instance

Source: Internet
Author: User
Tags openid
This article mainly introduces PHP payment instances in detail, including PHP payment source code and PHP refund source code, which has some reference value, interested friends can refer to this article to share with you the PHP payment instance, including the PHP payment source code, PHP refund source code, and php payment interface for your reference. the specific content is as follows:

1. JSapi payment demo (click in the client)

<? Php/*** JS_API payment demo * ==================================== =================================* open the H5 web page in the browser and execute JS to start the payment. The format of input and output data is JSON. * Three steps are required for successful payment start: * Step 1: obtain user openid through webpage authorization * Step 2: use the unified payment interface to obtain prepay_id * Step 3: use jsapi to initiate payment */include_once (".. /WxPayPubHelper. php "); // use the jsapi interface $ jsApi = new JsApi_pub (); // ========== Step 1: obtain openid through code if (! Isset ($ _ GET ['code']) {// return code $ url = $ jsApi-> createOauthUrlForCode (WxPayConf_pub: JS_API_CALL_URL); Header ("Location: $ url ") ;}else {// GET the code to GET openid $ code =$ _ GET ['code']; $ jsApi-> setCode ($ code ); $ openid = $ jsApi-> getOpenId ();} // ========== Step 2: use the unified payment interface, get prepay_id ==============/// use the unified payment interface $ unifiedOrder = new UnifiedOrder_pub (); // Set the unified payment interface parameters // set the required parameters // appid has been filled in, merchants do not need to fill in the required parameters // mch_id has been filled in, merchants do not need to fill in the required parameters/ /Noncestr has been filled in. merchants do not need to fill in repeated items. // spbill_create_ip has been filled in; merchants do not need to fill in repeated items; and merchants do not need to fill in $ unifiedOrder-> setParameter ("openid ", "$ openid"); // product description $ unifiedOrder-> setParameter ("body", ""); // product description // custom order number, for example, $ timeStamp = time (); $ out_trade_no = WxPayConf_pub: APPID. "$ timeStamp"; $ unifiedOrder-> setParameter ("out_trade_no", "$ out_trade_no"); // merchant Order No. $ unifiedOrder-> setParameter ("total_fee", "1 "); // Total Amount $ unifiedOrder-> set Parameter ("yy_url", WxPayConf_pub: notify_url); // notification address $ unigiedorder-> setParameter ("trade_type", "JSAPI "); // transaction type // A non-required parameter. the merchant can select this parameter based on the actual situation. // $ unifiedOrder-> setParameter ("sub_mch_id", "XXXX "); // sub-merchant ID // $ unifiedOrder-> setParameter ("device_info", "XXXX"); // device ID // $ 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 "); // product tag // $ unifiedOrder-> setParameter ("openid", "XXXX"); // User ID // $ unifiedOrder-> setParameter ("product_id ", "XXXX"); // product ID $ prepay_id = $ unifiedOrder-> getPrepayId (); // ========= Step 3: use jsapi to initiate a payment =============$ jsApi-> setPrepayId ($ prepay_id); $ jsApiParameters = $ jsApi-> getParameters (); // echo $ jsApiParameters;?>

2. native payment mode 1 demo (scan the static link QR code)

<? Php/*** Native (Native) payment mode demo * =============================================== =========================== * Mode 1: the merchant generates a link QR code in a fixed format. after scanning the QR code, the user * will send the productid and user openid to the link set by the merchant. the merchant will receive * Request to generate the order, if you submit an order by calling the unified payment interface, * is returned to the merchant's 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. * The two must be used in combination. */Include_once (".. /WxPayPubHelper. php "); // Set the static link $ nativeLink = new NativeLink_pub (); // Set the static link parameter // set the required parameter // appid is filled in, the merchant does not need to fill in the/mch_id field repeatedly, and the merchant does not need to fill in the/noncestr field repeatedly. the merchant does not need to fill in the/time_stamp field repeatedly, and the merchant does not need to fill in the/sign field repeatedly, the merchant does not need to enter $ product_id = WxPayConf_pub: APPID. "static"; // custom item id $ nativeLink-> setParameter ("product_id", "$ product_id "); // product id // Get link $ product_url = $ nativeLink-> getUrl (); // use the short link conversion interface $ sho RtUrl = new ShortUrl_pub (); // you can specify the required parameter. // appid is set. the merchant does not need to enter the required parameter. // mch_id is set. the merchant does not need to enter the required parameter. // noncestr is set, the merchant does not need to fill in the required fields. the merchant does not need to fill in the $ parameter URL-> setParameter ("long_url", "$ product_url "); // URL link // get the short link $ codeUrl = $ response URL-> GetResponse URL ();?>

3. native payment mode 2 demo (scan the dynamic link QR code)

<? Php/*** Native (Native) payment-mode 2-demo * ==================================== ===============================* the merchant generates the order, call the unified payment interface to obtain the code_url. * The URL directly generates a QR code. after scanning the code, the user starts the payment. **/Include_once (".. /WxPayPubHelper. php "); // use the unified payment interface $ unifiedOrder = new UnifiedOrder_pub (); // set the parameters of the unified payment interface // set the required parameter // appid is filled in, the merchant does not need to fill in the/mch_id field repeatedly, the merchant does not need to fill in the/noncestr field repeatedly, the merchant does not need to fill in the/spbill_create_ip field repeatedly, and the merchant does not need to fill in the/sign field repeatedly, the merchant does not need to fill in $ unifiedOrder-> setParameter ("body", "contribute a penny"); // product description // custom order number, for example, $ timeStamp = time (); $ out_trade_no = WxPayConf_pub: APPID. "$ timeStamp"; $ unifiedOrder-> setParameter ("out_trade_no", "$ out_trade_no"); // merchant Order No. $ unifiedOrder-> setParameter ("total_fee", "1 "); // Total Amount $ unifiedOrder-> setParameter ("yy_url", WxPayConf_pub: notify_url); // notification address $ unifiedOrder-> setParameter ("trade_type", "NATIVE "); // transaction type // A non-required parameter. the merchant can select this parameter based on the actual situation. // $ unifiedOrder-> setParameter ("sub_mch_id", "XXXX "); // sub-merchant ID // $ unifiedOrder-> setParameter ("device_info", "XXXX"); // device ID // $ 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"); // product tag // $ unifiedOrder-> setParameter ("openid", "XXXX "); // User ID // $ unifiedOrder-> setParameter ("product_id", "XXXX "); // item ID // Obtain the unified payment interface result $ unigiedorderresult = $ unigiedorder-> getResult (); // The merchant sets the corresponding processing flow based on the actual situation if ($ unifiedOrderResult ["return_code"] = "FAIL") {// The Merchant adds the processing flow echo by himself "communication error: ". $ unifiedOrderResult ['return _ msg ']."
";} Elseif ($ unifiedOrderResult [" result_code "] =" FAIL ") {// The merchant automatically adds the processing process echo" error code :". $ unifiedOrderResult ['err _ Code']."
"; Echo" error code description: ". $ unifiedOrderResult ['err _ code_des ']."
";} Elseif ($ unifiedOrderResult [" code_url "]! = NULL) {// get code_url $ code_url from the Unified Payment Interface = $ unifiedOrderResult ["code_url"]; // The Merchant adds the processing process by himself. // ......}?>

4. payment query interface demo

<? Php/*** order query-demo * ================================ ===================================== this interface provides query for all payment orders. * When the payment notification processing is abnormal or lost, the merchant can query the order payment status through this interface. **/Include_once ("../WxPayPubHelper. php"); // refund order number if (! Isset ($ _ POST ["out_trade_no"]) {$ out_trade_no = "";} else {$ out_trade_no = $ _ POST ["out_trade_no"]; // use the order query interface $ orderQuery = new OrderQuery_pub (); // You do not need to enter the required parameter // appid, the merchant does not need to fill in the fields repeatedly // noncestr, and the merchant does not need to fill in the fields repeatedly // sign, and the merchant does not need to fill in $ orderQuery-> setParameter ("out_trade_no", "$ out_trade_no "); // The Merchant's Order Number // optional parameter. the merchant can choose to enter // $ orderQuery-> setParameter ("sub_mch_id", "XXXX") based on the actual situation "); // sub-merchant ID // $ orderQuery-> setParameter ("transaction_id", "XXXX "); // Order No. // Obtain the order query result $ orderQueryResult = $ orderQuery-> getResult (); // The merchant sets the corresponding processing process based on the actual situation, for example, if ($ orderQueryResult ["return_code"] = "FAIL") {echo "communication error :". $ orderQueryResult ['return _ msg ']."
";} Elseif ($ orderQueryResult [" result_code "] =" FAIL ") {echo" error code: ". $ orderQueryResult ['err _ Code']."
"; Echo" error code description: ". $ orderQueryResult ['err _ code_des ']."
";} Else {echo" transaction status: ". $ orderQueryResult ['trade _ state']."
"; Echo" device number: ". $ orderQueryResult ['device _ info']."
"; Echo" User ID: ". $ orderQueryResult ['openid']."
"; Echo" follow public account: ". $ orderQueryResult ['is _ subscribe ']."
"; Echo" transaction type: ". $ orderQueryResult ['trade _ type']."
"; Echo" payment bank: ". $ orderQueryResult ['bank _ type']."
"; Echo" total amount: ". $ orderQueryResult ['total _ region']."
"; Echo" coupon amount: ". $ orderQueryResult ['coupon _ Shanghai']."
"; Echo" currency type: ". $ orderQueryResult ['currency _ type']."
"; Echo" payment order number: ". $ orderQueryResult ['transaction _ id']."
"; Echo" merchant order number: ". $ orderQueryResult ['out _ trade_no ']."
"; Echo" merchant data packet: ". $ orderQueryResult ['Attach ']."
"; Echo" payment completion time: ". $ orderQueryResult ['time _ end']."
";}}// The merchant adds the processing process by himself. // ......?>

5. statement Interface demo

<? Php/*** statement interface demo * ==================================== =================================* the merchant can download the historical transaction list through this interface. */Include_once ("../WxPayPubHelper. php"); // 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 // set the required parameter // appid is filled in, the merchant does not need to fill in the // mch_id already filled in, the merchant does not need to fill in again // noncestr already filled in, and the merchant does not need to fill in again // sign already filled in, the merchant does not need to fill in $ downloadBill-> setParameter ("bill_date", "$ bill_date"); // statement date $ downloadBill-> setParameter ("bill_type", "ALL "); // billing type // optional parameters. merchants can select this parameter based on actual conditions. // $ 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 ('
'); Echo "[statement details ]"."
"; Print_r ($ downloadBill-> response); print_r ('
') ;}}?>

6. refund interface demo

<? Php/*** refund application interface-demo * ================================ =====================================* note: for partial refund of the same ticket, you must set the same order number and different * out_refund_no. If a refund fails, submit it again. use the original * out_refund_no. The total refund amount cannot exceed the user's actual payment amount (the current * voucher amount cannot be refunded ). */Include_once ("../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_refund =$ _ POST ["refund_refund"]; // merchant refund ticket no., customized by the merchant, for example, $ out_refund_no = "$ out_trade_no ". "$ time_stamp"; // The total amount must correspond to the order number out_trade_no. The total amount of all orders in the demo is 1 minute $ total_amount = "1 "; // use the refund interface $ refund = new Refund_pub (); // You do not need to enter the required parameter // appid, the merchant does not need to fill in the fields repeatedly // noncestr, and the merchant does not need to fill in the fields repeatedly // sign, and the merchant does not need to fill in $ refund-> setParameter ("out_trade_no", "$ out_trade_no "); // merchant Order No. $ refund-> setParameter ("out_refund_no", "$ out_refund_no"); // merchant refund ticket No. $ refund-> setParameter ("total_detail ", "$ total_eter"); // Total Amount $ refund-> setParameter ("refund_eter", "$ refund_fee"); // refund amount $ refund-> setParameter ("op_user_id ", wxPayConf_pub: MCHID); // operator // optional parameter. the merchant can select this parameter based on the actual situation. // $ refund-> setParameter ("sub_mch_id", "XXXX "); // sub-merchant ID // $ refund-> setParameter ("device_info", "XXXX"); // device ID // $ refund-> setParameter ("transaction_id ", "XXXX"); // Order No. // call result $ refundResult = $ refund-> getResult (); // The merchant sets the corresponding processing process based on the actual situation, for example, if ($ refundResult ["return_code"] = "FAIL") {echo "communication error :". $ refundResult ['return _ msg ']."
";} Else {echo" business result: ". $ refundResult ['result _ Code']."
"; Echo" error code: ". $ refundResult ['err _ Code']."
"; Echo" error code description: ". $ refundResult ['err _ code_des ']."
"; Echo" public account ID: ". $ refundResult ['appid ']."
"; Echo" merchant id: ". $ refundResult ['mch _ id']."
"; Echo" sub-merchant ID: ". $ refundResult ['sub _ mch_id ']."
"; Echo" device number: ". $ refundResult ['device _ info']."
"; Echo" signature: ". $ refundResult ['sign']."
"; Echo" order number: ". $ refundResult ['transaction _ id']."
"; Echo" merchant order number: ". $ refundResult ['out _ trade_no ']."
"; Echo" merchant refund ticket No.: ". $ refundResult ['out _ refund_no ']."
"; Echo" refund ticket No.: ". $ refundResult ['refund _ idrefund_id ']."
"; Echo" refund channel: ". $ refundResult ['refund _ channel']."
"; Echo" refund amount: ". $ refundResult ['refund _ region']."
"; Echo" coupon refund amount: ". $ refundResult ['coupon _ refund_fee ']."
";}}?>

7. refund query interface demo

<? Php/*** refund application interface-demo * ================================ ============================================****/include_once (".. /WxPayPubHelper. php "); // the order number to be queried 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 (); // You do not need to enter the required parameter // appid, the merchant does not need to fill in the fields repeatedly // noncestr, and the merchant does not need to fill in the fields repeatedly // sign, and the merchant does not need to fill in $ refundQuery-> setParameter ("out_trade_no", "$ out_trade_no "); // merchant Order No. // $ refundQuery-> setParameter ("out_refund_no", "XXXX"); // merchant refund ticket No. // $ refundQuery-> setParameter ("refund_id ", "XXXX"); // refund ticket No. // $ refundQuery-> setParameter ("transaction_id", "XXXX"); // refund ticket No. // optional parameter, merchants can select/$ refundQuery-> setParameter ("sub_mch_id", "XXXX"); // sub-merchant ID // $ refundQuery-> setParameter ("device_info ", "XXXX"); // device number // refund query interface result $ refundQueryResult = $ refundQuery-> getResult (); // The merchant sets the corresponding processing process based on the actual situation, for example, if ($ refundQueryResult ["return_code"] = "FAIL") {echo "communication error :". $ refundQueryResult ['return _ msg ']."
";} Else {echo" business result: ". $ refundQueryResult ['result _ Code']."
"; Echo" error code: ". $ refundQueryResult ['err _ Code']."
"; Echo" error code description: ". $ refundQueryResult ['err _ code_des ']."
"; Echo" public account ID: ". $ refundQueryResult ['appid ']."
"; Echo" merchant id: ". $ refundQueryResult ['mch _ id']."
"; Echo" sub-merchant ID: ". $ refundQueryResult ['sub _ mch_id ']."
"; Echo" device number: ". $ refundQueryResult ['device _ info']."
"; Echo" signature: ". $ refundQueryResult ['sign']."
"; Echo" order number: ". $ refundQueryResult ['transaction _ id']."
"; Echo" merchant order number: ". $ refundQueryResult ['out _ trade_no ']."
"; Echo" refund quantity: ". $ refundQueryResult ['refund _ count']."
"; Echo" merchant refund ticket No.: ". $ refundQueryResult ['out _ refund_no ']."
"; Echo" refund ticket No.: ". $ refundQueryResult ['refund _ idrefund_id ']."
"; Echo" refund channel: ". $ refundQueryResult ['refund _ channel']."
"; Echo" refund amount: ". $ refundQueryResult ['refund _ failed']."
"; Echo" coupon refund amount: ". $ refundQueryResult ['coupon _ refund_fee ']."
"; Echo" refund status: ". $ refundQueryResult ['refund _ status']."
";}}?>

Download payment source code

The above is all the content of this article. I hope it will be helpful to everyone's learning, and I hope you can support your own home.

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.