栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

准备工作

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

手机扫码下载手机端app

基础配置

所需jar包

AlipayConfig

package com.alipay.config;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ResourceBundle;


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:\";

  //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息
    
    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 mapp1 = new HashMap();
//   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页面。

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/141598.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号