Java implementation of micro-credit payment function Development Example

Source: Internet
Author: User
Tags sha1

Management of micro-credit payment documents

Https://pay.weixin.qq.com/wiki/doc/api/index.html

Micro-credit to pay ordinary merchant access support four kinds of payment methods

Pay by credit card

The user opens the micro-letter wallet's swipe card interface, the merchant sweeps the code after submits completes pays

Public number Payment

The user enters the merchant H5 page in the micro-letter, the page calls Jssdk completes pays

Sweep Code Payment

User opens "micro-letter sweep", scan merchant's two-dimensional code to complete payment

App Payment

Merchant App integrated micro-mail SDK, users click to jump to the micro-letter to complete the payment

Here we look at the public number to pay

Public number Payment development steps

1, set the test directory

In the micro-credit public platform settings, the column is shown in Figure 7.7. Payment Test status, set the test directory, test the person's micro-signal added to the whitelist, launch the payment page directory must match the exact settings. and send the payment link to the corresponding Public Number session window in order to properly initiate the payment test. Note that the official directory must not be set to the same test directory, otherwise payment will be wrong.

Micro-Credit Payment source

Look directly at the code.

The code is as follows Copy Code

RequestHandler RequestHandler = new RequestHandler (Super.getrequest (), Super.getresponse ());

Get token//valid within two hours, regain after two hours

Token = Requesthandler.gettoken ();

Update token to Application

Requesthandler.gettokenreal ();

SYSTEM.OUT.PRINTLN ("micro-credit payment acquisition token=======================:" +token);

RequestHandler initialization

Requesthandler.init ();

Requesthandler.init (Appid,appsecret, Appkey,partnerkey, key);

Code: Local System Generation Order

Set up Package order parameters

sortedmap<string, string> packageparams = new treemap<string, string> ();

Packageparams.put ("Bank_type", "WX"); Payment type

Packageparams.put ("Body", "xxxx"); Product Description

Packageparams.put ("Fee_type", "1"); Currency of bank

Packageparams.put ("Input_charset", "UTF-8"); Character

Packageparams.put ("Notify_url", "Http://xxxx.com/xxxx/wxcallback"); The notification address here uses an extranet address test to see if Port 80 is open.

Packageparams.put ("Out_trade_no", no); Merchant Order Number

Packageparams.put ("partner", Partenerid); Set up merchant numbers

Packageparams.put ("Spbill_create_ip", Super.getrequest (). Getremotehost ()); Order-generated machine IP, which refers to the user's browser-side IP

Packageparams.put ("Total_fee", String.valueof (Rstotal)); Total amount of merchandise to be divided into units



Set up payment parameters

sortedmap<string, string> signparams = new treemap<string, string> ();

Signparams.put ("AppID", AppID);

Signparams.put ("Noncestr", noncestr);

Signparams.put ("Traceid", Propertiesutils.getorderno ());

Signparams.put ("timestamp", timestamp);

Signparams.put ("package", packagevalue);

Signparams.put ("Appkey", This.appkey);



Generate payment signatures, to use the original value of Urlencoder SHA1 algorithm!

String sign = "";

try {

Sign = sha1util.createsha1sign (signparams);

catch (Exception e) {

E.printstacktrace ();

}

Increase extra parameters for non-participating signatures

Signparams.put ("Sign_method", "SHA1");

Signparams.put ("App_signature", sign);

API Payment LCL End------------------------------------

Get Prepayid

String Prepayid = Requesthandler.sendprepay (signparams);

System.out.println ("Prepayid:" + Prepayid);
Code: Generate Prepaid Quick order complete


Generate prepaid Quick order completion and return the parameters required for Android,ios to drop the micro-letter.

sortedmap<string, string> payparams = new treemap<string, string> ();

Payparams.put ("AppID", AppID);

Payparams.put ("Noncestr", noncestr);

Payparams.put ("Package", "Sign=wxpay");

Payparams.put ("Partnerid", Partenerid);

Payparams.put ("Prepayid", Prepayid);

Payparams.put ("Appkey", This.appkey);

In addition to the 1000 is because of the parameter length limit.

int time = (int) (System.currenttimemillis ()/1000);

Payparams.put ("timestamp", string.valueof (time));



System.out.println ("timestamp:" + time);



Signature

String paysign = "";

try {

Paysign = Sha1util.createsha1sign (payparams);

catch (Exception e) {

E.printstacktrace ();

}

Payparams.put ("sign", paysign);



Spell-JSON data returned to the client

Basicdbobject backobject = new Basicdbobject ();

Backobject.put ("AppID", AppID);

Backobject.put ("Noncestr", Payparams.get ("Noncestr"));

Backobject.put ("Package", "Sign=wxpay");

Backobject.put ("Partnerid", Payparams.get ("Partnerid"));

Backobject.put ("Prepayid", Payparams.get ("Prepayid"));

Backobject.put ("Appkey", This.appkey);

Backobject.put ("timestamp", Payparams.get ("timestamp"));

Backobject.put ("Sign", Payparams.get ("sign"));

String backstr = dataobject.tostring ();

System.out.println ("Backstr:" + backstr);

return backstr;

So far, the prepayment order has been generated and returned to the client, waiting for the micro-server to notify the address of the notify_url that generated the prepayment order

ResponseHandler Reshandler = new ResponseHandler (request, response);

Reshandler.setkey (Partnerkey);

Create a Request object

RequestHandler queryreq = new RequestHandler (request, response);

Queryreq.init ();

if (reshandler.istenpaysign () = = True) {

Merchant Order Number

String out_trade_no = Reshandler.getparameter ("Out_trade_no");

System.out.println ("Out_trade_no:" + out_trade_no);

Tenpay Order Number

String transaction_id = Reshandler.getparameter ("transaction_id");

System.out.println ("transaction_id:" + transaction_id);

Amount to be divided into units

String Total_fee = Reshandler.getparameter ("Total_fee");

If there is a discount coupon used, the discount has a value, total_fee+discount= the original request Total_fee

String Discount = reshandler.getparameter ("discount");

Payment Results

String trade_state = Reshandler.getparameter ("Trade_state");



Judging signatures and results

if ("0". Equals (Trade_state)) {

//------------------------------

Instant to account processing business start

//------------------------------



SYSTEM.OUT.PRINTLN ("----------------Business logic Execution-----------------");



--Write a program based on your business logic (the above code is for reference only)--

SYSTEM.OUT.PRINTLN ("----------------business logic execution completed-----------------");

SYSTEM.OUT.PRINTLN ("Success"); Please do not modify or delete



SYSTEM.OUT.PRINTLN ("Instant to account payment success");

Send a success message to the Tenpay system and the Tenpay system will not follow up after receiving this result

RESHANDLER.SENDTOCFT ("Success");



Return success to the micro-mail server otherwise 30 minutes notice 8 times

Return "Success";

}else{

System.out.println ("Notify Signature verification failure");

RESHANDLER.SENDTOCFT ("fail");

Response.setcharacterencoding ("Utf-8");

}

}else {

System.out.println ("Fail-md5 failed");

}

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.