现在的位置: 首页 > 综合 > 正文

java在线支付—05_在线支付_编写用于准备请求数据的Servlet

2019年09月18日 ⁄ 综合 ⁄ 共 3727字 ⁄ 字号 评论关闭

05_在线支付_编写用于准备请求数据的Servlet
创梦综合技术qq交流群:CreDream:251572072
--------------------------------------------------
c./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 com.credream.utils.ConfigInfo;
import com.credream.utils.PanymentUtil;
/**
* 发起支付请求
* @author 传智播客
*
*/
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 orderid = request.getParameter("orderid");//订单号
  String amount = request.getParameter("amount");//支付金额
  String pd_FrpId = request.getParameter("pd_FrpId");//选择的支付银行
  String p1_MerId = ConfigInfo.getValue("p1_MerId");
  String keyValue = ConfigInfo.getValue("keyValue");
  String merchantCallbackURL = ConfigInfo.getValue("merchantCallbackURL"); 
  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 = PanymentUtil.buildHmac(messageType, p1_MerId, orderid, amount, currency,
    productId, productCat, productDesc, merchantCallbackURL, addressFlag, sMctProperties,
    pd_FrpId, pr_NeedResponse, keyValue);
  // 得到加密后的字串.
  request.setAttribute("messageType", messageType);
  request.setAttribute("merchantID", p1_MerId);
  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("frpId", pd_FrpId);
  request.setAttribute("pr_NeedResponse", pr_NeedResponse);
  request.setAttribute("hmac", md5hmac);
 
  request.getRequestDispatcher("/WEB-INF/page/connection.jsp").forward(request, response);
}
}
--------------------------------------------------------------------------------------------
d./payment/src/merchantInfo.properties
  p1_MerId=10000326625
  keyValue=0acqgug6x57m0wrsiod6clpn1ezh47r2ot5h1zkq5dztiic8y5xkm5g0p0ek
  merchantCallbackURL=http\://localhost\:8080/payment/servlet/yeepay/response
  第一个是商户的id,第二个是易宝给商户的密钥,第三个是接收易宝支付返回的支付结果的地址.
----------------------------------------------------------------------------------
e./payment/src/com/credream/utils/ConfigInfo.java
package com.credream.utils;
import java.util.Properties;
import java.util.ResourceBundle;
/**
* 读取配置文件
*
*/
public class ConfigInfo {
// 读取配置文件.
// private static ResourceBundle cacheBundle =null;
private static Properties cache = new Properties();
static{
  try {
//   cacheBundle = ResourceBundle.getBundle("");
   cache.load(ConfigInfo.class.getClassLoader().getResourceAsStream("merchantInfo.properties"));
  } catch (Exception e) {
   e.printStackTrace();
  }
}
/**
  * 获取指定key的值
  * @param key
  * @return
  */
public static String getValue(String key){
  return cache.getProperty(key);
}
}
---------------------------------------------------------------------------------------------------

抱歉!评论已关闭.