Spring Boot 分分钟搞定支付宝支付功能

1. 首先加入依赖

<dependency>
            <groupId>com.alipay.api</groupId>
            <artifactId>k12-alipay-sdk</artifactId>
            <version>0.0.1-beta</version>
</dependency>

2.

  1. application.properties中配置支付宝相关属性

2.发起支付请求调起支付页面

@RequestMapping("/alipay")
    public void alipay(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "orderNo", required = false, defaultValue = "12313131") String orderNo,
            @RequestParam(value = "clientType", required = false, defaultValue = "1") int clientType) {
        AlipayTradeWapPayModel atpm = new AlipayTradeWapPayModel();
        // 默认一种商品一个订单号
        atpm.setOutTradeNo(orderNo);
        atpm.setTotalAmount(Tools.toString(10));
        atpm.setSubject("测试");

        ClientType ct = ClientType.values()[clientType];
        String resultform = alipayService.payJump(atpm, ct);

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            response.getWriter().print(resultform);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 

package com.czw.alipay.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.czw.alipay.types.ClientType;
import com.czw.common.tools.Tools;
/**
 * 创建者:崔志伟
 * emai:[email protected]
 * 日期:2017年11月27日
 */

@Component
@PropertySource(value = {"classpath:/common.properties"},encoding="utf-8")  
public class AlipayService {
    
    @Value("${com.alipay.app.publickey}")  
    private String alipayAppPublicKey; //应用公钥
    
    @Value("${com.alipay.publickey}")  
    private String alipayPublicKey; //支付宝公钥
     
    @Value("${com.alipay.app.privatekey}")  
    private String alipayAppPrivateKey;  //应用私钥
    
    @Value("${com.alipay.pay.url}")  
    private String alipayUrl;  //阿里网关
    
    @Value("${com.alipay.pay.appid}")  
    private String alipayAppid; //appid
    
    @Value("${com.czw.pay.returnurl}")  
    private String alipayReturnUrl; //前台同步返回地址
    
    @Value("${com.czw.pay.notifyurl}")  
    private String alipayNotifyUrl; //后台异步通知地址
    
    private static String CHARSET="UTF-8";

    public String payJump(AlipayTradeWapPayModel alipayTradeWapPayModel,ClientType clientType){
        AlipayClient alipayClient = new DefaultAlipayClient(alipayUrl, alipayAppid, alipayAppPrivateKey, "json", CHARSET, alipayPublicKey, "RSA2"); //获得初始化的AlipayClient
        String form="";
        if(clientType == ClientType.WAP){
            AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
            alipayRequest.setReturnUrl(alipayReturnUrl);
            alipayRequest.setNotifyUrl(alipayNotifyUrl);//在公共参数中设置回跳和通知地址
            alipayRequest.setBizContent("{" +
            "\"out_trade_no\":\""+alipayTradeWapPayModel.getOutTradeNo()+"\"," +
            "\"total_amount\":\""+alipayTradeWapPayModel.getTotalAmount()+"\"," +
            "\"subject\":\""+alipayTradeWapPayModel.getSubject()+"\"," +
            "\"product_code\":\"QUICK_WAP_PAY\"," + //固定值
            "\"passback_params\":\""+Tools.encodePar("clientType="+ClientType.WAP.ordinal())+"\"" +
            "}");//填充业务参数
            try {
                form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
        }else if(clientType == ClientType.PC){
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
            alipayRequest.setReturnUrl(alipayReturnUrl);
            alipayRequest.setNotifyUrl(alipayNotifyUrl);//在公共参数中设置回跳和通知地址
            JSONObject content = new JSONObject();
            content.put("out_trade_no", "125464846161311465416541654165146161614643613");
            content.put("total_amount","0.01");
            content.put("product_code", "FAST_INSTANT_TRADE_PAY");
            content.put("subject", "Iphone6 16G");
            content.put("body", "Iphone6 16G body");
            
            alipayRequest.setBizContent(JSON.toJSONString(content));//填充业务参数
            
            try {
                form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
                
                System.out.println(form);
                
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
        }
        return form;
    }
}
 

3.跳转至支付页面

4.

OK 成功支付 soeasy!

猜你喜欢

转载自blog.csdn.net/m0_37598953/article/details/82287315