欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javaweb在线支付功能实现代码

程序员文章站 2024-03-01 13:42:58
本文实例为大家分享了javaweb在线支付功能的具体实现代码,供大家参考,具体内容如下 package com.learning.web.servlet;...

本文实例为大家分享了javaweb在线支付功能的具体实现代码,供大家参考,具体内容如下

package com.learning.web.servlet;

import java.io.ioexception;
import java.util.resourcebundle;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.learning.utils.paymentutil;

@webservlet("/payonline")
public class payonline extends httpservlet {
  private static final long serialversionuid = 1l;
    
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
      dopost(request, response);
  }


  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
  
    string orderid = request.getparameter("orderid");
    string money = request.getparameter("money");
    
        // 银行
        string pd_frpid = request.getparameter("pd_frpid");

        // 发给支付公司需要哪些数据
        string p0_cmd = "buy";
        string p1_merid = resourcebundle.getbundle("/web-inf/classes/merchantinfo").getstring(
            "p1_merid");
        string p2_order = orderid;
        string p3_amt = money;
        string p4_cur = "cny";
        string p5_pid = "";
        string p6_pcat = "";
        string p7_pdesc = "";
        // 支付成功回调地址 ---- 第三方支付公司会访问、用户访问
        // 第三方支付可以访问网址(本项目网址)
        string p8_url = "http://115.170.37.189/day19pay/callback";
        string p9_saf = "";
        string pa_mp = "";
        string pr_needresponse = "1";
        // 加密hmac 需要密钥
        string keyvalue = resourcebundle.getbundle("merchantinfo").getstring(
            "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);
        // 生成url --- url?
        request.setattribute("pd_frpid", pd_frpid);
        request.setattribute("p0_cmd", p0_cmd);
        request.setattribute("p1_merid", p1_merid);
        request.setattribute("p2_order", p2_order);
        request.setattribute("p3_amt", p3_amt);
        request.setattribute("p4_cur", p4_cur);
        request.setattribute("p5_pid", p5_pid);
        request.setattribute("p6_pcat", p6_pcat);
        request.setattribute("p7_pdesc", p7_pdesc);
        request.setattribute("p8_url", p8_url);
        request.setattribute("p9_saf", p9_saf);
        request.setattribute("pa_mp", pa_mp);
        request.setattribute("pr_needresponse", pr_needresponse);
        request.setattribute("hmac", hmac);

        request.getrequestdispatcher("/confirm.jsp").forward(request, response);
    
    
  }

}

2.用户确认提交的信息(confirm.jsp) 

<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>

<%@taglib prefix="p" uri="http://www.itcast.cn/tag"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
</head>
<body>
  <p:user />
  <!-- 确认支付form -->
  <form action="https://www.yeepay.com/app-merchant-proxy/node"
    method="get">
    <h3>订单号:${p2_order},付款金额 :${p3_amt }</h3>
    <input type="hidden" name="pd_frpid" value="${pd_frpid }" /> <input
      type="hidden" name="p0_cmd" value="${p0_cmd }" /> <input
      type="hidden" name="p1_merid" value="${p1_merid }" /> <input
      type="hidden" name="p2_order" value="${p2_order }" /> <input
      type="hidden" name="p3_amt" value="${p3_amt }" /> <input
      type="hidden" name="p4_cur" value="${p4_cur }" /> <input
      type="hidden" name="p5_pid" value="${p5_pid }" /> <input
      type="hidden" name="p6_pcat" value="${p6_pcat }" /> <input
      type="hidden" name="p7_pdesc" value="${p7_pdesc }" /> <input
      type="hidden" name="p8_url" value="${p8_url }" /> <input
      type="hidden" name="p9_saf" value="${p9_saf }" /> <input
      type="hidden" name="pa_mp" value="${pa_mp }" /> <input type="hidden"
      name="pr_needresponse" value="${pr_needresponse }" /> <input
      type="hidden" name="hmac" value="${hmac }" /> <input type="submit"
      value="确认支付" />
  </form>
</body>
</html>

3.网站获得第三方支付的信息

package com.learning.web.servlet;

import java.io.ioexception;
import java.util.resourcebundle;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.learning.service.orderservice;
import com.learning.utils.paymentutil;


/**
 * 该servlet会在支付成功后 进行调用----- 支付公司 、客户
 * 
 * @author seawind
 * 
 */
public class callbackservlet extends httpservlet {

  public void doget(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    // 获得回调所有数据
    string p1_merid = request.getparameter("p1_merid");
    string r0_cmd = request.getparameter("r0_cmd");
    string r1_code = request.getparameter("r1_code");
    string r2_trxid = request.getparameter("r2_trxid");
    string r3_amt = request.getparameter("r3_amt");
    string r4_cur = request.getparameter("r4_cur");
    string r5_pid = request.getparameter("r5_pid");
    string r6_order = request.getparameter("r6_order");
    string r7_uid = request.getparameter("r7_uid");
    string r8_mp = request.getparameter("r8_mp");
    string r9_btype = request.getparameter("r9_btype");
    string rb_bankid = request.getparameter("rb_bankid");
    string ro_bankorderid = request.getparameter("ro_bankorderid");
    string rp_paydate = request.getparameter("rp_paydate");
    string rq_cardno = request.getparameter("rq_cardno");
    string ru_trxtime = request.getparameter("ru_trxtime");
    // 身份校验 --- 判断是不是支付公司通知你
    string hmac = request.getparameter("hmac");
    string keyvalue = resourcebundle.getbundle("merchantinfo").getstring(
        "keyvalue");

    // 自己对上面数据进行加密 --- 比较支付公司发过来hamc
    boolean isvalid = paymentutil.verifycallback(hmac, p1_merid, r0_cmd,
        r1_code, r2_trxid, r3_amt, r4_cur, r5_pid, r6_order, r7_uid,
        r8_mp, r9_btype, keyvalue);
    if (isvalid) {
      // 响应数据有效
      if (r9_btype.equals("1")) {
        // 浏览器重定向
        response.setcontenttype("text/html;charset=utf-8");
        response.getwriter().println("<h1>付款成功!等待商城进一步操作!等待收货...</h1>");
      } else if (r9_btype.equals("2")) {
        // 服务器点对点 --- 支付公司通知你
        system.out.println("付款成功!");
        // 修改订单状态 为已付款
        orderservice orderservice=new orderservice();
        orderservice.modifyorderstate(p1_merid);
        
        // 回复支付公司
        response.getwriter().print("success");
      }
    } else {
      // 数据无效
      system.out.println("数据被篡改!");
    }
  }

  public void dopost(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    doget(request, response);
  }

}

文件:

merchantinfo.properties

p1_merid=10001126856
keyvalue=69cl522av6q613ii4w6u8k6xuw8vm1n6bfgyv769220iuye9u37n4y7ri4pl
responseurl=http://localhost:8080/onlinepay/servlet/paymentresponse

工具类:paymentutil

package com.learning.utils;

import java.io.unsupportedencodingexception;
import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
import java.util.arrays;

public class paymentutil {

  private static string encodingcharset = "utf-8";
  
  /**
   * 生成hmac方法
   * 
   * @param p0_cmd 业务类型
   * @param p1_merid 商户编号
   * @param p2_order 商户订单号
   * @param p3_amt 支付金额
   * @param p4_cur 交易币种
   * @param p5_pid 商品名称
   * @param p6_pcat 商品种类
   * @param p7_pdesc 商品描述
   * @param p8_url 商户接收支付成功数据的地址
   * @param p9_saf 送货地址
   * @param pa_mp 商户扩展信息
   * @param pd_frpid 银行编码
   * @param pr_needresponse 应答机制
   * @param keyvalue 商户密钥
   * @return
   */
  public static string buildhmac(string p0_cmd,string p1_merid,
      string p2_order, string p3_amt, string p4_cur,string p5_pid, string p6_pcat,
      string p7_pdesc,string p8_url, string p9_saf,string pa_mp,string pd_frpid,
      string pr_needresponse,string keyvalue) {
    stringbuilder svalue = new stringbuilder();
    // 业务类型
    svalue.append(p0_cmd);
    // 商户编号
    svalue.append(p1_merid);
    // 商户订单号
    svalue.append(p2_order);
    // 支付金额
    svalue.append(p3_amt);
    // 交易币种
    svalue.append(p4_cur);
    // 商品名称
    svalue.append(p5_pid);
    // 商品种类
    svalue.append(p6_pcat);
    // 商品描述
    svalue.append(p7_pdesc);
    // 商户接收支付成功数据的地址
    svalue.append(p8_url);
    // 送货地址
    svalue.append(p9_saf);
    // 商户扩展信息
    svalue.append(pa_mp);
    // 银行编码
    svalue.append(pd_frpid);
    // 应答机制
    svalue.append(pr_needresponse);
    
    return paymentutil.hmacsign(svalue.tostring(), keyvalue);
  }
  
  /**
   * 返回校验hmac方法
   * 
   * @param hmac 支付网关发来的加密验证码
   * @param p1_merid 商户编号
   * @param r0_cmd 业务类型
   * @param r1_code 支付结果
   * @param r2_trxid 易宝支付交易流水号
   * @param r3_amt 支付金额
   * @param r4_cur 交易币种
   * @param r5_pid 商品名称
   * @param r6_order 商户订单号
   * @param r7_uid 易宝支付会员id
   * @param r8_mp 商户扩展信息
   * @param r9_btype 交易结果返回类型
   * @param keyvalue 密钥
   * @return
   */
  public static boolean verifycallback(string hmac, string p1_merid,
      string r0_cmd, string r1_code, string r2_trxid, string r3_amt,
      string r4_cur, string r5_pid, string r6_order, string r7_uid,
      string r8_mp, string r9_btype, string keyvalue) {
    stringbuilder svalue = new stringbuilder();
    // 商户编号
    svalue.append(p1_merid);
    // 业务类型
    svalue.append(r0_cmd);
    // 支付结果
    svalue.append(r1_code);
    // 易宝支付交易流水号
    svalue.append(r2_trxid);
    // 支付金额
    svalue.append(r3_amt);
    // 交易币种
    svalue.append(r4_cur);
    // 商品名称
    svalue.append(r5_pid);
    // 商户订单号
    svalue.append(r6_order);
    // 易宝支付会员id
    svalue.append(r7_uid);
    // 商户扩展信息
    svalue.append(r8_mp);
    // 交易结果返回类型
    svalue.append(r9_btype);
    string snewstring = paymentutil.hmacsign(svalue.tostring(), keyvalue);
    return snewstring.equals(hmac);
  }
  
  /**
   * @param avalue
   * @param akey
   * @return
   */
  public static string hmacsign(string avalue, string akey) {
    byte k_ipad[] = new byte[64];
    byte k_opad[] = new byte[64];
    byte keyb[];
    byte value[];
    try {
      keyb = akey.getbytes(encodingcharset);
      value = avalue.getbytes(encodingcharset);
    } catch (unsupportedencodingexception e) {
      keyb = akey.getbytes();
      value = avalue.getbytes();
    }

    arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
    arrays.fill(k_opad, keyb.length, 64, (byte) 92);
    for (int i = 0; i < keyb.length; i++) {
      k_ipad[i] = (byte) (keyb[i] ^ 0x36);
      k_opad[i] = (byte) (keyb[i] ^ 0x5c);
    }

    messagedigest md = null;
    try {
      md = messagedigest.getinstance("md5");
    } catch (nosuchalgorithmexception e) {

      return null;
    }
    md.update(k_ipad);
    md.update(value);
    byte dg[] = md.digest();
    md.reset();
    md.update(k_opad);
    md.update(dg, 0, 16);
    dg = md.digest();
    return tohex(dg);
  }

  public static string tohex(byte input[]) {
    if (input == null)
      return null;
    stringbuffer output = new stringbuffer(input.length * 2);
    for (int i = 0; i < input.length; i++) {
      int current = input[i] & 0xff;
      if (current < 16)
        output.append("0");
      output.append(integer.tostring(current, 16));
    }

    return output.tostring();
  }

  /**
   * 
   * @param args
   * @param key
   * @return
   */
  public static string gethmac(string[] args, string key) {
    if (args == null || args.length == 0) {
      return (null);
    }
    stringbuffer str = new stringbuffer();
    for (int i = 0; i < args.length; i++) {
      str.append(args[i]);
    }
    return (hmacsign(str.tostring(), key));
  }

  /**
   * @param avalue
   * @return
   */
  public static string digest(string avalue) {
    avalue = avalue.trim();
    byte value[];
    try {
      value = avalue.getbytes(encodingcharset);
    } catch (unsupportedencodingexception e) {
      value = avalue.getbytes();
    }
    messagedigest md = null;
    try {
      md = messagedigest.getinstance("sha");
    } catch (nosuchalgorithmexception e) {
      e.printstacktrace();
      return null;
    }
    return tohex(md.digest(value));

  }
  
//  public static void main(string[] args) {
//    system.out.println(hmacsign("annulcard1000043252120080620160450.0http://localhost/szxpro/callback.asp杩?4564868265473632445648682654736324511","8upp0ke8sq73zvp370vko7c39403rtk1ywx40td6irh216036h27eb12792t"));
//  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。