<返回更多

web端网站接入支付宝支付过程

2020-02-22    
加入收藏

1、在支付宝开放平台(https://open.alipay.com/platform/home.htm)申请对应的功能

web端网站接入支付宝支付过程

 

找到页或者移动应用列表

web端网站接入支付宝支付过程

 

你可以看到你已申请的应用

点击进入应用详情增加功能:

web端网站接入支付宝支付过程

 

2、在自己的JAVAweb应用里引入对应的jar包

      <!--阿里支付SDK-->
      <dependency>
          <groupId>com.alipay.sdk</groupId>
          <artifactId>alipay-sdk-java</artifactId>
          <version>4.3.0.ALL</version>
      </dependency>

3、支付宝相关配置信息

可根据自己的情况配置相关的内容,下面只作为相关示意

package com.fast.tpp.dto;

public class AlipayConfig {

        /**
         * 签约的支付宝账号对应的支付宝唯一用户号,以2088开头的16位纯数字组成
         */
        public static final String PID = "xx";

        /**
         * 正式环境请求地址
         */
        public static String ALIPAY_URL = "https://openapi.alipay.com/gateway.do";

        /**
         * 支付宝分配给开发者的应用ID
         *
         */
        public static String App_ID = "xx";

        /**
         * 仅支持JSON
         */
        public static String FORMAT = "JSON";

        /**
         * 请求使用的编码格式
         */
        public static String CHARSET = "utf-8";

        /**
         * 商户生成签名字符串所使用的签名算法类型
         */
        public static String SIGN_TYPE = "RSA2";

        /**
         * 支付宝公钥
         */
        public static String ALIPAY_PUBLIC_KEY =xx";
        /**
         * 商户私钥
         */
        public static String PRIVATE_KEY =xx";

        /**
         * 回调地址
         */
        public static String CALLBACK_URL = "xx";

        /**
         * web支付跳转地址
         */
        public static String WEB_RETURN_URL = "xx";

}

4、初始化阿里支付相关配置,只贴部分代码作参考

@Service
@Log4j2
public class AlipaySaoImpl implements AlipaySao {

    @Autowired
    private redisService redisService;


    // 实例化客户端
    AlipayClient alipayClient;

    public AlipaySaoImpl(){
        // 实例化客户端
        alipayClient = new DefaultAlipayClient(
                AlipayConfig.ALIPAY_URL,
                AlipayConfig.APP_ID,
                AlipayConfig.PRIVATE_KEY,
                AlipayConfig.FORMAT,
                AlipayConfig.CHARSET,
                AlipayConfig.ALIPAY_PUBLIC_KEY,
                AlipayConfig.SIGN_TYPE);
    }

5、web支付相关方法代码

package com.fast.tpp.dto;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class AlipayWebPayDTO
{
    /**
     * 商户订单号,必填
     *
     */
    private String out_trade_no;
    /**
     * 订单名称,必填
     */
    private String subject;
    /**
     * 付款金额,必填
     * 根据支付宝接口协议,必须使用下划线
     */
    private String total_amount;
    /**
     * 商品描述,可空
     */
    private String body;
    /**
     * 超时时间参数
     */
    private String timeout_express= "5m";
    /**
     * 产品编号
     */
    private String product_code= "FAST_INSTANT_TRADE_PAY";

}


 /**
     * web支付接口
     * @param alipayBean
     * @return
     * @throws AlipayApiException
     */
    @Override
    public String webPagePay(AlipayWebPayDTO alipayBean) throws AlipayApiException {
        // 2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(AlipayConfig.WEB_RETURN_URL);
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(AlipayConfig.CALLBACK_URL);
        // 封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 3、请求支付宝进行付款,并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // 返回付款信息
        return result;
    }

6、最终返回给客户端的result值为,里面是一个form,页面拿到后会自动跳转到支付宝的订单支付页面

{
	"code": "00",
	"msg": "请求成功!",
	"data": {
		"orderString": "<form name="punchout_form" method="post" action="https://openapi.alipay.com/gateway.do?charset=utf-8&method=alipay.trade.page.pay&sign=G0jsaTORXtfF1VkQEK77qu%2BnYRU1YhCNQMFiWNNBUbYgQz8pxGRGVcnzbIXWkgWawV3AXco%2BJkFEemhV7qK2ALIn6uVziNklV6Folqb6OyVnsdKdCfHbID%2Bz79M1ngpyWsWvxXoAUN6Kuib5qkurDPQfMo4HWF4fxmLF70cFkQNeHxg577ByHS0%2Borv%2BzJ%2FwqehZZqOB1JQ0jzserODf2PGieoE5PCDnIYuXMZFqJqlCRUDc1IN3h9Usez8QJhLWepcFDsDR8tXiHrMmge9onHAl9bpLUtyOuVe4TCMU%2BLgMH21NUpeKJdXgQpnQxA8DoUuU0lRTmvPEp6EyXAmE%2FQ%3D%3D&return_url=http%3A%2F%2Fwww.moyou520.com%2Fdist%2Fhtml%2Fpay%2Fresult.html¬ify_url=http%3A%2F%2Fadmin.moyou520.com%2Fmoyou%2Fapp%2Ftrans%2FalipayNotice.do&version=1.0&app_id=2019081066129967&sign_type=RSA2×tamp=2020-02-17+15%3A58%3A24&alipay_sdk=alipay-sdk-java-4.3.0.ALL&format=JSON">n<input type="hidden" name="biz_content" value="{"body":"购买[年度VIP]","out_trade_no":"20200217155823617499935","product_code":"FAST_INSTANT_TRADE_PAY","subject":"默友APP","timeout_express":"5m","total_amount":"0.0"}">n<input type="submit" value="立即支付" style="display:none" >n</form>n<script>document.forms[0].submit();</script>",
		"orderNo": "20200217155823617499935"
	}
}

7、回调方法相关代码

/**
	 * 支付宝支付通知
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/alipayNotice.do",method = RequestMethod.POST)
	@ResponseBody
	@NoneAuth
	@SysLog(actionName = "支付宝支付通知")
	public String alipayNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> params = new HashMap<String, String>();
		Map requestParams = request.getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
			}
			
			params.put(name, valueStr);
		}
		// 切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
		try {
			boolean flag = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET,
					AlipayConfig.SIGN_TYPE);
			if (flag) {
				String tradeStatus = params.get("trade_status");
				String outTradeNo = params.get("out_trade_no");
				String tradeNo = params.get("trade_no");
				//购买者用户ID
				String openId = params.get("buyer_logon_id");
				this.orderService.alipayNotice(tradeStatus,outTradeNo,tradeNo,openId);
				if ("TRADE_SUCCESS".equals(tradeStatus)){
					return "success";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "fail";
	}

大致如上,欢迎大家批评指正

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>