java支付宝支付(电脑网页支付&手机网页支付)springboot maven项目

java支付宝支付(电脑网页支付&手机网页支付)springboot maven项目

pom.xml配置


		
			com.alipay.sdk
			alipay-sdk-java
			4.9.124.ALL
		

AlipayConfig配置类

package com.webber.cm.pay.config;

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

@Component
public class AlipayConfig {

	// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
	public static String APPID;

	// 支付宝网关
	public static String URL;

	// 商户私钥,您的PKCS8格式RSA2私钥
	public static String RSA_PRIVATE_KEY;

	// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
	public static String ALIPAY_PUBLIC_KEY;

	// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
	public static String NOTIFY_URL;

	// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
	public static String RETURN_URL;

	// 签名方式
	public final static String SIGNTYPE = "RSA2";

	// 字符编码格式
	public final static String CHARSET = "UTF-8";

	// 返回格式
	public final static String FORMAT = "json";

	public static String getAPPID() {
		return APPID;
	}

	@Value("${zhifubao.appid}")
	public void setAPPID(String aPPID) {
		APPID = aPPID;
	}

	public static String getURL() {
		return URL;
	}

	@Value("${zhifubao.url}")
	public void setURL(String uRL) {
		URL = uRL;
	}

	public static String getRSA_PRIVATE_KEY() {
		return RSA_PRIVATE_KEY;
	}

	@Value("${zhifubao.rsa_private_key}")
	public void setRSA_PRIVATE_KEY(String rSA_PRIVATE_KEY) {
		RSA_PRIVATE_KEY = rSA_PRIVATE_KEY;
	}

	public static String getALIPAY_PUBLIC_KEY() {
		return ALIPAY_PUBLIC_KEY;
	}

	@Value("${zhifubao.alipay_public_key}")
	public void setALIPAY_PUBLIC_KEY(String aLIPAY_PUBLIC_KEY) {
		ALIPAY_PUBLIC_KEY = aLIPAY_PUBLIC_KEY;
	}

	public static String getNOTIFY_URL() {
		return NOTIFY_URL;
	}

	@Value("${zhifubao.notify_url}")
	public void setNOTIFY_URL(String nOTIFY_URL) {
		NOTIFY_URL = nOTIFY_URL;
	}

	public static String getRETURN_URL() {
		return RETURN_URL;
	}

	@Value("${zhifubao.return_url}")
	public void setRETURN_URL(String rETURN_URL) {
		RETURN_URL = rETURN_URL;
	}
}

application.yml配置

########### 支付宝 ###################
zhifubao:
  appid: 2016102300741651
  url: https://openapi.alipaydev.com/gateway.do
  rsa_private_key: MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBK......
  alipay_public_key: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQE......
  notify_url: http://127.0.0.1:8080/alipay/notify_url.jsp
  return_url: https://127.0.0.1:8080/ad/html-handler/paySuccess.html

AliController.java

package com.webber.cm.pay.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.webber.cm.common.entity.AjaxResult;
import com.webber.cm.log.aop.Log;
import com.webber.cm.log.aop.LogOperType;
import com.webber.cm.log.aop.LogType;
import com.webber.cm.pay.config.AlipayConfig;
import com.webber.cm.pay.pojo.AliPayBean;

import io.swagger.annotations.Api;

/**
 * 阿里接口
 */
@Api(tags = "阿里接口")
@RestController
@RequestMapping("/ali")
public class AliController {

	// 获得初始化的AlipayClient
	private static final AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
			AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
			AlipayConfig.SIGNTYPE);

	/**
	 * 支付
	 */
	@PostMapping("/pay")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.INSERT, detail = "支付宝-支付")
	public String pay(AliPayBean aliPayBean) throws AlipayApiException {
		if (aliPayBean == null) {
			return "error";
		}
		if (StringUtils.isEmpty(aliPayBean.getOut_trade_no()) || StringUtils.isEmpty(aliPayBean.getTotal_amount())
				|| StringUtils.isEmpty(aliPayBean.getSubject()) || StringUtils.isEmpty(aliPayBean.getBody())) {
			return "error";
		}
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		alipayRequest.setReturnUrl(AlipayConfig.RETURN_URL);
		alipayRequest.setNotifyUrl(AlipayConfig.NOTIFY_URL);
		alipayRequest.setBizContent(AliPayBean.toBizContent(aliPayBean));
		String result = alipayClient.pageExecute(alipayRequest).getBody();
		return result;
	}

	/**
	 * 查询
	 */
	@PostMapping("/query")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.QUERY, detail = "支付宝-查询")
	public AjaxResult query(@RequestBody String out_trade_no) throws AlipayApiException {
		if (StringUtils.isEmpty(out_trade_no)) {
			return AjaxResult.err("非法请求");
		}
		AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
		alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"}");
		String result = alipayClient.execute(alipayRequest).getBody();
		return AjaxResult.succ(result);
	}

	/**
	 * 退款
	 */
	@PostMapping("/refund")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.UPDATE, detail = "支付宝-退款")
	public AjaxResult refund(@RequestBody String out_trade_no) throws AlipayApiException {
		if (StringUtils.isEmpty(out_trade_no)) {
			return AjaxResult.err("非法请求");
		}
		AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
		String refund_amount = "0.01";
		String refund_reason = "申请退款";
		alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"refund_amount\":\""
				+ refund_amount + "\"," + "\"refund_reason\":\"" + refund_reason + "\"}");
		String result = alipayClient.execute(alipayRequest).getBody();
		return AjaxResult.succ(result);
	}

	/**
	 * 退款查询
	 */
	@PostMapping("/refundQuery")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.QUERY, detail = "支付宝-退款查询")
	public AjaxResult refundQuery(@RequestBody String out_trade_no) throws AlipayApiException {
		if (StringUtils.isEmpty(out_trade_no)) {
			return AjaxResult.err("非法请求");
		}
		AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();
		String out_request_no = out_trade_no;
		alipayRequest.setBizContent(
				"{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"out_request_no\":\"" + out_request_no + "\"}");
		String result = alipayClient.execute(alipayRequest).getBody();
		return AjaxResult.succ(result);
	}

	/**
	 * 关闭
	 */
	@PostMapping("/close")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.OTHER, detail = "支付宝-关闭")
	public AjaxResult close(@RequestBody String out_trade_no) throws AlipayApiException {
		if (StringUtils.isEmpty(out_trade_no)) {
			return AjaxResult.err("非法请求");
		}
		AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
		alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"}");
		String result = alipayClient.execute(alipayRequest).getBody();
		return AjaxResult.succ(result);
	}
}

AliwapController.java

package com.webber.cm.pay.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.webber.cm.common.entity.AjaxResult;
import com.webber.cm.log.aop.Log;
import com.webber.cm.log.aop.LogOperType;
import com.webber.cm.log.aop.LogType;
import com.webber.cm.pay.config.AlipayConfig;
import com.webber.cm.pay.pojo.AliPayBean;

import io.swagger.annotations.Api;

/**
 * 阿里接口
 */
@Api(tags = "阿里手机接口")
@RestController
@RequestMapping("/aliwap")
public class AliwapController {

	// 获得初始化的AlipayClient
	private static final AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
			AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
			AlipayConfig.SIGNTYPE);

	/**
	 * 支付
	 */
	@PostMapping("/pay")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.INSERT, detail = "支付宝-支付")
	public String pay(HttpServletResponse response, AliPayBean aliPayBean) throws AlipayApiException {
		if (aliPayBean == null) {
			return "";
		}
		if (StringUtils.isEmpty(aliPayBean.getOut_trade_no()) || StringUtils.isEmpty(aliPayBean.getTotal_amount())
				|| StringUtils.isEmpty(aliPayBean.getSubject()) || StringUtils.isEmpty(aliPayBean.getBody())) {
			return "";
		}
		// 销售产品码 必填
		String product_code = "ZSLT-HD-001";
		AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest();
		// 封装请求支付信息
		AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
		model.setOutTradeNo(aliPayBean.getOut_trade_no());
		model.setSubject(aliPayBean.getSubject());
		model.setTotalAmount(aliPayBean.getTotal_amount());
		model.setBody(aliPayBean.getBody());
		model.setTimeoutExpress("2m");// 超时时间 可空
		model.setProductCode(product_code);
		alipay_request.setBizModel(model);
		// 设置异步通知地址
		alipay_request.setNotifyUrl(AlipayConfig.NOTIFY_URL);
		// 设置同步地址
		alipay_request.setReturnUrl(AlipayConfig.RETURN_URL);
		String result = client.pageExecute(alipay_request).getBody();
		return result;
	}

	/**
	 * 查询
	 */
	@PostMapping("/query")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.QUERY, detail = "支付宝-查询")
	public AjaxResult query(@RequestBody String out_trade_no) throws AlipayApiException {
		if (StringUtils.isEmpty(out_trade_no)) {
			return AjaxResult.err("非法请求");
		}
		AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
		AlipayTradeQueryModel model = new AlipayTradeQueryModel();
		model.setOutTradeNo(out_trade_no);
		alipayRequest.setBizModel(model);
		String result = client.execute(alipayRequest).getBody();
		return AjaxResult.succ(result);
	}

	/**
	 * 退款
	 */
	@PostMapping("/refund")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.UPDATE, detail = "支付宝-退款")
	public AjaxResult refund(@RequestBody String out_trade_no) throws AlipayApiException {
		if (StringUtils.isEmpty(out_trade_no)) {
			return AjaxResult.err("非法请求");
		}
		AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
		String refund_amount = "0.01";
		String refund_reason = "申请退款";
		AlipayTradeRefundModel model = new AlipayTradeRefundModel();
		model.setOutTradeNo(out_trade_no);
		model.setRefundAmount(refund_amount);
		model.setRefundReason(refund_reason);
		model.setOutRequestNo(out_trade_no);
		alipayRequest.setBizModel(model);
		String result = client.execute(alipayRequest).getBody();
		return AjaxResult.succ(result);
	}

	/**
	 * 退款查询
	 */
	@PostMapping("/refundQuery")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.QUERY, detail = "支付宝-退款查询")
	public AjaxResult refundQuery(@RequestBody String out_trade_no) throws AlipayApiException {
		if (StringUtils.isEmpty(out_trade_no)) {
			return AjaxResult.err("非法请求");
		}
		AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();
		String out_request_no = out_trade_no;
		AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
		model.setOutTradeNo(out_trade_no);
		model.setOutRequestNo(out_request_no);
		alipayRequest.setBizModel(model);
		String result = client.execute(alipayRequest).getBody();
		;
		return AjaxResult.succ(result);
	}

	/**
	 * 关闭
	 */
	@PostMapping("/close")
	@Log(logType = LogType.PC_LOG, logOperType = LogOperType.OTHER, detail = "支付宝-关闭")
	public AjaxResult close(@RequestBody String out_trade_no) throws AlipayApiException {
		if (StringUtils.isEmpty(out_trade_no)) {
			return AjaxResult.err("非法请求");
		}
		AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
		alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"}");
		AlipayTradeCloseModel model = new AlipayTradeCloseModel();
		model.setOutTradeNo(out_trade_no);
		alipayRequest.setBizModel(model);
		String result = client.execute(alipayRequest).getBody();
		return AjaxResult.succ(result);
	}
}

AjaxResult.java

package com.webber.cm.common.entity;

import lombok.ToString;

/**
 * ajax结果
 */
@ToString
public class AjaxResult {
	
	private static final String SUCC_CODE="200";// 成功编码
	
	private static final String ERR_CODE="0";// 失败编码

	private String code;
	private String msg;
	private Object data;

	
	public AjaxResult(String code,Object data) {
		this.code = code;
		this.data=data;
	}
	
	public AjaxResult(String code,String msg) {
		this.code = code;
		this.msg=msg;
	}
	
	public static AjaxResult succ(Object data) {
		return new AjaxResult(SUCC_CODE,data);
	}
	
	public static AjaxResult err(String msg) {
		return new AjaxResult(ERR_CODE,msg);
	}
	
	public static AjaxResult err(String code,String msg) {
		return new AjaxResult(code,msg);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

}

前端html





    
    
    
    
    支付



    
订单号:



订单号:



订单号:



订单号:



订单号:



前端js

// 支付
function fun_pay(){
    if (/(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent)) {
        $('#pay_form').attr('action', 'http://127.0.0.1:8081/aliwap/pay');
    } else {
        $('#pay_form').attr('action', 'http://127.0.0.1:8081/ali/pay');
    };
    $("#pay_form").submit();
}

// 查询
function fun_query(){
    var out_trade_no=$("#query_out_trade_no").val();
    $.ajax({
        type: "POST",
        url: "http://127.0.0.1:8081/ali/query",
        data: out_trade_no,
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code === "200") {
                $("#msg").html(data.data);
            } else {
                alert(data.msg);
            }
        },
        error: function (data) {
            alert("数据存在错误!");
        }
    })
}

其他方法类似......

你可能感兴趣的:(Java企业应用)