当前位置: 移动技术网 > IT编程>开发语言>Java > java实现在SSM下使用支付宝扫码支付功能

java实现在SSM下使用支付宝扫码支付功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java使用支付宝扫码支付的具体代码,供大家参考,具体内容如下

准备工作

首先开通支付宝沙箱的测试账号,里面会有消费者账户和收款方账户

手机扫码下载手机端app

基础配置

所需jar包

这里写图片描述

alipayconfig

package com.alipay.config;

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

/* *
 *类名:alipayconfig
 *功能:基础配置类
 *详细:设置帐户有关信息及返回路径
 *修改日期:2017-04-05
 *说明:
 *以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
 *该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
 */
public class alipayconfig {
  //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息
    // 应用id,您的appid,收款账号既是您的appid对应支付宝账号
    public static string app_id = "2016080403162340";

    // 商户私钥,您的pkcs8格式rsa2私钥
    public static string merchant_private_key = "miievaid2tulssmawg5+f4nzbexpnxi8nkqjpzeeaa==";

    // 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keymanage.htm 对应appid下的支付宝公钥。
    public static string alipay_public_key = "miibijt26tltkar8s1erdwi25vibcmz7plmxvvumhf5tdbwfbmhus3qidaqab";

    // 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static string notify_url = "http://localhost:8080/alipay.trade.page.pay-java-utf-8/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static string return_url = "http://localhost:8080/exam/index/goumai";
    // 签名方式
    public static string sign_type = "rsa2";

    // 字符编码格式
    public static string charset = "utf-8";

    // 支付宝网关
    public static string gatewayurl = "https://openapi.alipaydev.com/gateway.do";

    // 支付宝网关
    public static string log_path = "e:\\";

  //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息
    /** 
     * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
     * @param sword 要写入日志里的文本内容
     */
    public static void logresult(string sword ) {
      filewriter writer = null;
      try {
        writer = new filewriter(log_path + "alipay_log_" + system.currenttimemillis()+".txt");
        writer.write(sword);
      } catch (exception e) {
        e.printstacktrace();
      } finally { 
        if (writer != null) {
          try {
            writer.close();
          } catch (ioexception e) {
            e.printstacktrace();
          }
        }
      }
    }
}

controller

//生成有二维码,可供扫码支付的页面
  @requestmapping(value = "alipay")
  public string alipay(httpservletresponse response,modelmap map,string chapterid,httpservletrequest request,
      string widout_trade_no,string widtotal_amount,string widsubject,string widbody) throws ioexception, alipayapiexception{
//   string a,string urlname,string couname....+"&a="+a+"&urlname="+urlname+"&couname="+couname
    //获得初始化的alipayclient
    alipayclient alipayclient = new defaultalipayclient(alipayconfig.gatewayurl, alipayconfig.app_id, alipayconfig.merchant_private_key, "json", alipayconfig.charset, alipayconfig.alipay_public_key, alipayconfig.sign_type);

    //设置请求参数
    alipaytradepagepayrequest alipayrequest = new alipaytradepagepayrequest();
    alipayrequest.setreturnurl(alipayconfig.return_url+"?chapterid="+chapterid);
    alipayrequest.setnotifyurl(alipayconfig.notify_url);
    //付款id,必填
    string out_trade_no = widout_trade_no;
    //付款金额,必填
    string total_amount = widtotal_amount;
    total_amount=urldecoder.decode(total_amount,"utf-8");//转码
    //订单名称,必填
    string subject = widsubject;
    subject=urldecoder.decode(subject,"utf-8");
    //商品描述,可空
    string body = widbody;

    alipayrequest.setbizcontent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
        + "\"total_amount\":\""+ total_amount +"\"," 
        + "\"subject\":\""+ subject +"\"," 
        + "\"body\":\""+ body +"\"," 
        + "\"timeout_express\":\"1m\"," 
        + "\"product_code\":\"fast_instant_trade_pay\"}");
    //请求
    string result = alipayclient.pageexecute(alipayrequest).getbody();
     response.setcontenttype("text/html; charset=utf-8"); 
      printwriter out = response.getwriter();  
      out.println(result);  
      return null;
  }

支付成功的放回页面(return_url)

成功后的返回路径,走controller,详见alipayconfig中的配置

//点击购买,将课程存入购买表中
  @requestmapping(value="goumai")
  @responsebody
  public modelandview goumai(string chapterid,httpservletrequest req,string a,string urlname,string couname,modelmap map){
    modelandview mav = new modelandview();
    map<string,string> mapp1 = new hashmap<string,string>();
//   sysusertab login_user = sysuserservice.getsysuserbyid(userid);
    httpsession session = req.getsession();
    sysusertab login_user1 = (sysusertab) session.getattribute("login_user");
    string userid = login_user1.getuserid();
//   session.setattribute("login_user", login_user);

    mapp1.put("userid", userid);
    mapp1.put("chapterid", chapterid);
    int num = sysbuyservice.getbuycount(mapp1);
    if(num==0){
      mapp1.put("buyid", uuid.randomuuid().tostring().replace("-", ""));
      sysbuyservice.insertbuy(mapp1);
    }

    //查询课程内容
//   string fanhui = showfh(req,chapterid,urlname,couname,map, a);
    mav.setviewname("jsp/pay/paysuccess");
    return mav;
  }

支付成功后,页面跳转至paysuccess.jsp页面。

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网