Website Integration Alipay

Source: Internet
Author: User
Tags md5 stringbuffer

(Reproduced in http://stephen830.javaeye.com/blog/254827)

The basic flow of the domestic e-commerce system is as follows:
Customer orders-> system within the system according to the order generated Alipay interface URL-> customer through the URL to use Alipay (Online banking) payment-> Alipay will customer payment completion information sent to the e-commerce system-> System received Alipay information to determine customer orders have been paid- > Follow up the shipping process.

Before you start the following, you have to have a Alipay account, if you want to integrate Alipay interface, you must also apply for open service (on how to open, you can directly to the Alipay website application). After the service is opened, Alipay will give you 2 string number: 1 Partnerid (partner ID), There are also 1 Securitycode (security codes). When you get these 2 yards, you can start with the following.

(1) How to invoke the Alipay interface. (The customer's order information in accordance with established rules to generate a URL to jump to the Alipay site)

Get the Alipay URL by invoking the following method [Makeorderalipayurl (HttpServletRequest httprequest,order Order)], and then jump (Response.sendredirect ( URL);).

Java code  /**          *  generate Alipay interface url.      & based on order nbsp;   *  @param  httpRequest          * @ param order  Order Example           *  @return           *  @throws  Exception          */          public   Static  string makeorderalipayurl ( Httpservletrequest httprequest,order order)   throws  Exception {               hashmap hm = new  HashMap ();               hm.put ("_input_charset", Httprequest.getcharacterencoding ()); Using the same coding method               &Nbsp;hm.put ("Body", "Your Order on www.xxx.com"); Fill in the payment content information shown on the Skip to Alipay page                hm.put (" Discount ","-5 "); Fill in the discount information  -5 the deduction of 5 Yuan                hm.put (" Logistics_fee "," 10 "); Logistics Cost                hm.put ("Logistics_payment" , "Buyer_pay"); Logistics cost payer  buyer_pay= Buyer pay logistics fee                 hm.put ("Logistics_type", "EXPRESS"); Logistics Mode                hm.put ("Notify_url", "http ://www.xxx.com/notifyurl.jsp ");//customer payment, Alipay calls page                hm.put ("Out_trade_no", Order.getid ()); External transaction number, preferably unique, using .               when obtaining payment information from Alipay  hm.put ("partner", Partnerid); Partnerid (Partner ID)                hm.put ("Agent ", Partnerid); Partnerid (Partner ID)                hm.put (" Payment_type "," 1 "); Payment type  1= commodity purchase, 2 = Service purchase,...                 Hm.put ("Price", "105.30"); Order Amount Information                hm.put ("Quantity", "1"); Order item quantity, generally write 1, it is according to the whole order package to calculate                 Hm.put ("Return_url", "http://www.xxx.com/ReturnUrl.jsp"), and/or after the customer has successfully paid, the page displayed to the customer                hm.put ("Seller_email", "alipay@xxx.com"); Your Alipay account email               hm.put ("service", " Create_direct_pay_by_user "); Create_direct_pay_by_usEr= Direct Payment,trade_create_by_buyer  guaranteed payment                  hm.put ("Subject", "www.xxx.com order"); Fill in the payment title information shown on the Skip to Alipay page                string  payGateway =  "https://www.alipay.com/cooperate/gateway.do";//Jump to Alipay's URL header                return  makeurl (Hm,securitycode, Httprequest.getcharacterencoding (), paygateway); Securitycode (Security Code)             }                              /**          *  generate Alipay payments based on incoming parameters url           *  @param  hm  parameter value            *  @param  securitycode  Security Code           *  @param  charset  Code           *  @param  payGateway  Alipay gateway          *  @return           */          public   Static  string makeurl (hashmap hm,string  securitycode,string charset,string paygateway)   throws  Exception{               list keys = new  ArrayList ( Hm.keyset ());              collections.sort ( keys);//Alipay request parameters must be sorted alphabetically                 Stringbuffer content = new  stringbuffer ();               for   (int  i =  0;  i < keys.size ();  i++)  {                   content.append ( String)  keys.get (i);                   content.append ("=");                   content.append ((String)  hm.get ((String)  keys.get (i));                   if   (i !=  keys.size ()  -  1)  {                       content.append ("&");                   }              }               content.append (securitycode);              &NBSP;STRING&NBSP;SIGN&NBSP;=&NBSP;MD5 (Content.tostring (), CharSet);               content.delete (0, Content.length ());               content.append (paygateway);               for   (int  i =  0;  i < keys.size ( ),  i++  {                   content.append (Keys.get (i));                   content.append ("=");             &nBsp;     content.append (Urlencoder.encode (String)  hm.get (Keys.get (i)),  CharSet);                   content.append ("&");              }               content.append ("sign=");               content.append (sign);               content.append ("&sign_type=md5");               keys.clear ();               keys = null;              return  content.tostring ();          }                     /**          *  generate MD5 encoded string .           *  @param  str  Source string           * @ param charset  Coding Method           *  @return           *          */         public   Static &NBSP;STRING&NBSP;MD5 (String str,string charset)  {               if   (str ==  null)                   return   NULL;              char  hexDigits[]  = {  ' 0 ',  ' 1 ',  ' 2 ',  ' 3 ',  ' 4 ',  ' 5 ',  ' 6 ',  ' 7 ',  ' 8 ', &nbs P ' 9 ',                        ' A ',  ' B ',  ' C ',  ' d ',  ' e ',  ' F '  };    & nbsp                        messagedigest md5messagedigest = null;               byte [] md5bytes =  NULL;              char  md5Chars[] =  NULL;               byte [] strbytes =  null;               try &NBSP;{&NBSP;&NBsp;                strbytes =  str.getbytes (CharSet);                   md5messagedigest = messagedigest.getinstance ("MD5");                   md5messagedigest.update (strBytes );                   Md5bytes = md5messagedigest.digest ();                   int  j = md5Bytes.length;                   md5chars = new   Char [j *  2];                   int k =  0;                   for   (int  i =  0;  i < j; i++)  {                        byte  md5Byte = md5Bytes[i];                       md5chars[k++] = hexdigits[md5byte  >>> 4  &  0xf];                       md5chars[k++] = hexdigits[md5byte &NBSP;&AMP;&NBSP;0XF];                   }                   rEturn   New  string (md5chars);               } catch   (nosuchalgorithmexception e)  {                   //log.output (E.tostring (),  Log.STD_ERR);                    return   NULL;              } catch   ( Unsupportedencodingexception e)  {                   //log.output (E.tostring (),  log.std_err);                    return   NULL;               } finally  {       &nbsP;           md5messagedigest = null;                   strBytes  = null;                   md5bytes = null;               }          }     [Java] View Plain copy print? /**         *  Payment PO based on order generation interface url.          *  @param  httpRequest         *  @param   order  Order Example          *  @return           *  @throws  Exception         */          public static string makeorderalipayurl (HttpServletRequest  httprequest,order order)  throws Exception {               hashmap hm = new hashmap ();               hm.put ("_input_charset", Httprequest.getcharacterencoding ())//using the same coding method                 Hm.put ("Body", "Your Order on www.xxx.com")//fill in the payment content information displayed on the Skip to Alipay page                 hm.put ("Discount", "-5")//Fill out the discount information  -5 means to deduct 5 yuan                hm.put ("Logistics_fee", "10")/Logistics cost                 hm.put ("Logistics_payment", "Buyer_pay");//logistics expense payer   buyer_pay= buyer pays logistics fee                hm.put (" Logistics_type "," EXPRESS ");/Logistics mode                 hm.put ("Notify_url", "http://www.xxx.com/notifyurl.jsp");//customer payment, Alipay calls page               hm.put ("Out_trade_no", Order.getid ());//external transaction number, preferably uniqueness, Use .               hm.put when obtaining payment information from Alipay ("partner ", Partnerid);//partnerid (partner ID)               hm.put ("Agent", Partnerid); Partnerid (Partner ID)                hm.put ("Payment _type "," 1 ");//Payment type  1= commodity purchase, 2 = Service purchase,...                 hm.put ("Price", "105.30");//Order Amount Information                 hm.put ("Quantity", "1");//Order merchandise quantity, is generally written 1, it is according to the entire order package to calculate                 hm.put ("Return_url", "http://www.xxx.com/ReturnUrl.jsp");//customer payment successful, Page displayed to the customer               hm.put ("Seller_email", " Alipay@xxx.com ")//Your Alipay account email                Hm.put ("service", "Create_direct_pay_by_user");//create_direct_pay_by_user= Direct Payment,trade_create_by_buyer  guaranteed payment                 hm.put ("Subject", "www.xxx.com order"); Fill in the payment title information shown on the Skip to Alipay page                String  paygateway =  "Https://www.alipay.com/cooperate/gateway.do"; /jump to Alipay's URL header               return makeurl ( Hm,securitycode,httprequest.getcharacterencoding (), paygateway);//securitycode (Safety Code)             }                               /** &n

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.