Java Online Book Mall (9) Payment Module _java

Source: Internet
Author: User
Tags hmac string back

This example for you to share the Java Online Mall payment module specific code for your reference, the specific content as follows

1. Payment Preparation

Public String Paymentpre (httpservletrequest req, HttpServletResponse resp)
  throws Servletexception, IOException { C3/>req.setattribute ("Order", Orderservice.load (Req.getparameter ("OID"));
 return "f:/jsps/order/pay.jsp";
}

2. Method of payment

Public String Payment (httpservletrequest req, HttpServletResponse resp) throws Servletexception, IOException {propert
 IES props = new Properties ();
 Props.load (This.getclass (). getClassLoader (). getResourceAsStream ("payment.properties")); * * 1.  Prepare 13 Parameters */String p0_cmd = "buy";//business type, fixed value buy string P1_merid = Props.getproperty ("P1_merid");/firm encoded, unique identification String in Yeepay P2_order = Req.getparameter ("oid");//order code String P3_amt = "0.01";//Payment amount String p4_cur = "CNY";//transaction currency, fixed value CNY string p5_ Pid = "";//commodity name string p6_pcat = "";//commodity type string p7_pdesc = "";//Commodity Description String P8_url = Props.getproperty ("P8_url");
 After a successful payment, Yi Bao will visit this address. String p9_saf = "";//shipping address string pa_mp = "";//Extended information string pd_frpid = Req.getparameter ("YH");/payment channel string Pr_needrespon SE = "1";//answer mechanism, fixed value 1/* 2.
 The calculation of HMAC * requires 13 parameters * requires KeyValue * requires encryption algorithm/String keyvalue = Props.getproperty ("KeyValue"); String HMAC = Paymentutil.buildhmac (P0_cmd, P1_merid, P2_order, P3_amt, P4_cur, P5_pid, P6_pcat, P7_pdesC, P8_url, P9_saf, Pa_mp, Pd_frpid, Pr_needresponse, keyvalue); * * 3.
 Redirect to easy-treasure payment gateway/StringBuilder SB = new StringBuilder ("Https://www.yeepay.com/app-merchant-proxy/node"); Sb.append ("?").
 Append ("p0_cmd="). Append (P0_cmd);
 Sb.append ("&"). Append ("p1_merid="). Append (P1_merid);
 Sb.append ("&"). Append ("p2_order="). Append (P2_order);
 Sb.append ("&"). Append ("p3_amt="). Append (P3_amt);
 Sb.append ("&"). Append ("p4_cur="). Append (P4_cur);
 Sb.append ("&"). Append ("p5_pid="). Append (P5_pid);
 Sb.append ("&"). Append ("p6_pcat="). Append (P6_pcat);
 Sb.append ("&"). Append ("p7_pdesc="). Append (P7_pdesc);
 Sb.append ("&"). Append ("P8_url="). Append (P8_url);
 Sb.append ("&"). Append ("p9_saf="). Append (P9_SAF);
 Sb.append ("&"). Append ("pa_mp="). Append (PA_MP);
 Sb.append ("&"). Append ("pd_frpid="). Append (Pd_frpid);
 Sb.append ("&"). Append ("pr_needresponse="). Append (Pr_needresponse);
  
 Sb.append ("&"). Append ("hmac="). Append (HMAC); Resp.sendredirect (Sb.tostriNg ());
return null;
 

 }

3. Feedback method

When the payment is successful, Yi Bao will visit here
Accessed in two ways:
1. Guide the user's browser redirection (if the user closes the browser, they will not be able to access it)
2. The server uses point-to-point communication to access the method. (Must give back success, otherwise the Easy Treasure Server will call this method)

Public String Back (httpservletrequest req, HttpServletResponse resp) throws Servletexception, IOException {/* * 1.
 Get 12 Parameters */String P1_merid = Req.getparameter ("P1_merid");
 String r0_cmd = Req.getparameter ("R0_cmd");
 String R1_code = Req.getparameter ("R1_code");
 String R2_trxid = Req.getparameter ("R2_trxid");
 String R3_amt = Req.getparameter ("R3_amt");
 String r4_cur = Req.getparameter ("R4_cur");
 String r5_pid = Req.getparameter ("R5_pid");
 String R6_order = Req.getparameter ("R6_order");
 String R7_uid = Req.getparameter ("R7_uid");
 String R8_MP = Req.getparameter ("R8_mp");
 String R9_btype = Req.getparameter ("R9_btype");
 String HMAC = Req.getparameter ("HMAC"); * * 2.
 Get KeyValue */Properties Props = new properties ();
 Props.load (This.getclass (). getClassLoader (). getResourceAsStream ("payment.properties"));
 String keyvalue = Props.getproperty ("KeyValue"); * * 3. Call the Paymentutil validation method to validate the caller's identity * > If the checksum fails: Save the error message, forward to msg.jsp * > If the checksum passes: * * To determine whether the access method is redirected or dotOn point, if redirected * Modify order status, save success information, forward to msg.jsp * * If is point-to-point: Modify order status, return success/Boolean bool = Paymentutil.verifycallback (HM
 AC, P1_merid, R0_cmd, R1_code, R2_trxid, R3_amt, R4_cur, R5_pid, R6_order, R7_uid);
  if (!bool) {Req.setattribute ("code", "error"); Req.setattribute ("msg", "Invalid signature, payment failed!")
  (You're not a good person) ");
 return "f:/jsps/msg.jsp";
  } if (R1_code.equals ("1")) {Orderservice.updatestatus (R6_order, 2);
   if (R9_btype.equals ("1")) {Req.setattribute ("code", "Success"); Req.setattribute ("msg", "Congratulations, pay success!")
   ");    
  return "f:/jsps/msg.jsp";
  else if (R9_btype.equals ("2")) {Resp.getwriter (). Print ("Success");
} return null;
 }

4.pay.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%> <%@ taglib prefix= "C" uri= "http://" Java.sun.com/jsp/jstl/core "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

Payment.properties

p1_merid=10001126856
KEYVALUE=69CL522AV6Q613II4W6U8K6XUW8VM1N6BFGYV769220IUYE9U37N4Y7RI4PL
p8_Url=http \://localhost\:8080/goods/orderservlet?method\=back

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.