How to invoke the Alipay interface with struts _java

Source: Internet
Author: User
Tags base64 json

First, Action Class key code the useless bag can kill

Copy Code code as follows:

Package com.hzdracom.alipay.action;

Import it.sauronsoftware.base64.Base64;

Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.net.HttpURLConnection;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.ResourceBundle;

Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.struts2.ServletActionContext;
Import org.dom4j.Document;
Import Org.springframework.stereotype.Controller;

Import Com.alibaba.fastjson.JSON;
Import com.hzdracom.action.BaseAction;
Import Com.hzdracom.config.AlipayConfig;
Import Com.hzdracom.util.AlipaySubmit;
Import Com.hzdracom.util.HTTPService;
Import Com.hzdracom.util.UtilDate;
Import Com.hzlq.source.security.DESUtil;
/*
* Alipay Payment Interface
* @author Zhuangzi
* @Directions
* @version $Id: alipayaction.java,v 0.1 2013-3-27 pm 04:49:57 Zhuangzi Exp $
*/
@Controller
public class Alipayaction extends Baseaction {
Private String Phonecode;
Private String Totalfee;

/**
* Call Alipay interface
* @author Zhuangzi
* @class com.hzdracom.alipay.action.AliPayAction
* @method AliPay
* @Directions Call Alipay interface
* @date 2013-3-27 pm 04:53:35 void
*/
public void Pay () {
try {
/*1**** generate Alipay transaction number, according to the time to the millisecond level * *
String out_trade_no = Utildate.getordernum ();
/*2*****↓↓↓↓↓↓↓↓↓↓ the request parameters into a group of ↓↓↓↓↓↓↓↓↓↓*/
map<string, string> sparatemp = new hashmap<string, string> ();
Sparatemp = Getpayprama (Totalfee, out_trade_no);

/*3*****↓↓↓↓↓↓↓↓↓↓ Request Alipay Interface ↓↓↓↓↓↓↓↓↓↓*/
String json = alipaysubmit.buildrequest (sparatemp, "get", "confirm");
HttpServletResponse response = Servletactioncontext.getresponse ();
Response.setcontenttype ("text/html");
Response.setcharacterencoding ("Utf-8");
PrintWriter out;
out = Response.getwriter ();
JSON = json.tojsonstring (JSON);
Out.write (JSON);
Out.flush ();
Out.close ();

catch (IOException e) {
}
}

/**
*
* @author Zhuangzi
* @class com.hzdracom.alipay.action.AliPayAction
* @method Getpayprama
* @Directions Package Call Alipay parameters
* @date 2013-4-1 05:26:11
* @param totalfee Amount
* @param out_trade_no Order number
* @return map<string,string>
*/
Public map<string, String> Getpayprama (String totalfee, String out_trade_no) {
map<string, string> sparatemp = new hashmap<string, string> ();
Sparatemp.put ("service", "Create_direct_pay_by_user");
Sparatemp.put ("partner", Alipayconfig.partner);
Sparatemp.put ("_input_charset", Alipayconfig.input_charset);
Sparatemp.put ("Payment_type", Alipayconfig.payment_type);
* Server Asynchronous notification page path//need http://format full path, can not add? id=123 such custom parameters
Sparatemp.put ("Notify_url", Alipayconfig.notify_url);
/* page Jump Sync notification page path//need http://format full path, can not add? id=123 Such custom parameters cannot be written as http://localhost/*/
Sparatemp.put ("Return_url", Alipayconfig.return_url);
* * Seller Alipay Account * *
Sparatemp.put ("Seller_email", Alipayconfig.seller_email);
/* Merchant Order Number * *
Sparatemp.put ("Out_trade_no", out_trade_no);
/* Order name * *
Sparatemp.put ("Subject", alipayconfig.subject);
* * Payment Amount * *
Sparatemp.put ("Total_fee", Totalfee);
/* Order Description * *
Sparatemp.put ("Body", alipayconfig.body);
/* The full path to start with http://, for example: http://www.xxx.com/myorder.html*/
Sparatemp.put ("Show_url", Alipayconfig.show_url);
Anti-phishing time stamp
Sparatemp.put ("Anti_phishing_key", Alipayconfig.anti_phishing_key);
/* Client IP address non-LAN IP address, such as: 221.0.0.1*/
Sparatemp.put ("Exter_invoke_ip", alipayconfig.exter_invoke_ip);
return sparatemp;

}

Public String Getphonecode () {
return phonecode;
}

public void Setphonecode (String phonecode) {
This.phonecode = Phonecode;
}

Public String Gettotalfee () {
return totalfee;
}

public void Settotalfee (String totalfee) {
This.totalfee = Totalfee;
}

}

Second, the Alipayconfig class is the package payment treasure interface Parameters

Copy Code code as follows:

Package com.hzdracom.config;

Import Com.hzdracom.util.UtilDate;

/* *
* Class Name: Alipayconfig
* Function: Basic Configuration Class
* Details: Set account information and return path
* Version: 3.3
* Date: 2012-08-10
Description
* The following code is only for the convenience of merchant test sample code, the merchant can according to their own website needs, according to technical documentation, not necessarily to use the code.
* This code is only for learning and research Alipay interface use, just provide a reference.

* Hint: How to obtain the security checksum code and partner ID
*1. Use your signed Payment Treasure account login to pay Treasure website (www.alipay.com)
*2. Click on "Merchant Service" (https://b.alipay.com/order/myOrder.htm)
*3. Click "Query partner Identity (PID)", "Query security check Code (KEY)"

* Security check code, when you enter the payment password, the page appears gray phenomenon, how to do?
* Solution:
* 1, check the browser configuration, do not allow the browser to do the frame screen settings
* 2, change the browser or computer, login query again.
*/

public class Alipayconfig {

↓↓↓↓↓↓↓↓↓↓ Please configure your basic information here ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Partner ID, a string of 16-digit pure digits beginning with 2088
public static String partner = "2088001466494907";
Merchant's private key
public static String key = "FB30213VJC4K4OABT7Z4Q9E07SFMLMZR";

↑↑↑↑↑↑↑↑↑↑ Please configure your basic information here ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

Debug with, create TXT log folder path
public static String Log_path = "d:\\";

The character encoding format currently supports GBK or Utf-8
public static String Input_charset = "Utf-8";

Signature mode does not need to be modified
public static String Sign_type = "MD5";

public static String Payment_type = "1";
Must fill, cannot modify
Server Asynchronous Notification page path
public static String Notify_url = "notify_url.jsp";
The full path to the http://format should not be added. id=123 Such custom parameters

Page Jump Sync notification page path
public static String Return_url = "return_url.jsp";
Need to http://the full path of the format, can not add id=123 such custom parameters, can not be written as http://localhost/

Seller pays treasure Account
public static String Seller_email = "jjj@hzdracom.com";
Must fill

Merchant Order Number
public static String Out_trade_no =utildate.getordernum ();
The unique order number in the order system of the merchant website must be filled in

Order name
public static String subject = "Hidden number recharge";
Must fill

Order description
public static String BODY = "Hidden number recharge";
Product Display Address

public static String Show_url = "";
Anti-phishing time stamp
public static String anti_phishing_key= "";
/* Client IP address non-LAN IP address, such as: 221.0.0.1*/
public static String exter_invoke_ip= "";
}

Third, strut configuration file

<action name= "pay" method= "pay" class= "Com.hzdracom.alipay.action.AliPayAction" >
</action>

These are the key parts of the code that need to be taken into consideration:

1. Several classes of the interface provided by Alipay need to be introduced into your project, including the rack package

2.notify_url.jsp and return_url.jsp address to do some of the full path to the external network address

3. Your own business process write notify_url.jsp this inside, you can also directly write an interface to the Alipay interface call


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.