05_编写用于准备请求数据的Servlet
a.payment/src/com/credream/servlet/PaymentRequest.java
package com.credream.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.utils.ConfigInfo;
import cn.itcast.utils.PaymentUtil;
/**
* 发起支付请求
*
*/
public class PaymentRequest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* p1_MerId=10000326625// 商家的id
keyValue=0acqgug6x57m0wrsiod6clpn1ezh47r2ot5h1zkq5dztiic8y5xkm5g0p0ek //密钥
merchantCallbackURL=http\://localhost\:8080/payment/servlet/yeepay/response //
这个地址是用来接收易宝支付返回结果的路径.这个路径必须外网可以访问.
*/
request.setCharacterEncoding("GBK");
String merchantID=ConfigInfo.getValue("merchantID");
String keyValue=ConfigInfo.getValue("keyValue");
String merchantCallbackURL=ConfigInfo.getValue("merchantCallbackURL");
String orderid=request.getParameter("orderid");//订单号
String amount=request.getParameter("amount");//支付金额
String pd_FrpId=request.getParameter("pd_FrpId");//银行支付方式
String messageType="Buy";//请求命令,在线支付固定为buy
String currency="CNY";//货币单位
String productDesc="";//商品描述
String productCat="";//商品种类
String productId="";//商品ID
String addressFlag="0";//需要填写送货信息0:不需要,1:需要
String sMctProperties="";//商品扩展信息
String pr_NeedResponse="0";//应答机制
//下面这个方法用来进行加密,通过调用加密类
String md5hmac=PaymentUtil.buildHmac(messageType, merchantID, orderid, amount, currency, productId,
productCat, productDesc, merchantCallbackURL, addressFlag, sMctProperties, pd_FrpId,
"0", keyValue);
request.setAttribute("messageType", messageType);
request.setAttribute("merchantID", merchantID);
request.setAttribute("orderid", orderid);
request.setAttribute("amount", amount);
request.setAttribute("currency", currency);
request.setAttribute("productId", productId);
request.setAttribute("productCat", productCat);
request.setAttribute("productDesc", productDesc);
request.setAttribute("merchantCallbackURL", merchantCallbackURL);
request.setAttribute("addressFlag", addressFlag);
request.setAttribute("sMctProperties", sMctProperties);
request.setAttribute("pd_FrpId", pd_FrpId);
request.setAttribute("pr_NeedResponse", pr_NeedResponse);
request.setAttribute("hmac", md5hmac);
request.getRequestDispatcher("/WEB-INF/page/connection.jsp").forward(request, response);
}
}
------------------------------------------------------------------------------------------------------------------------
b. payment/src/merchantInfo.properties
p1_MerId=10000326625
keyValue=0acqgug6x57m0wrsiod6clpn1ezh47r2ot5h1zkq5dztiic8y5xkm5g0p0ek
merchantCallbackURL=http\://localhost\:8080/payment/servlet/yeepay/response
第一个是商户的id,第二个是易宝给商户的密钥,第三个是接收易宝支付返回的支付结果的地址.
-------------------------------------------------------------------------------------------------------------------------
c. payment/src/com/credream/utils/ConfigInfo.java
package com.credream.utils;
import java.util.ResourceBundle;
//读取配置文件
public class ConfigInfo {
private static ResourceBundle cache=null;
static{
cache=ResourceBundle.getBundle("merchantInfo");
}
//获取指定key的值
public static String getValue(String key){
return cache.getString(key);
}
}