当前位置:   article > 正文

平台H5对接支付宝支付接口(含分布式源码)_抓h5支付宝转接口

抓h5支付宝转接口

简介
本文详细介绍通过支付宝开放平台开发助手生成密钥对、支付宝证书申请 CSR 文件步骤。
说明:生成密钥、CSR 文件后,开发者需根据 生成并配置密钥 指引,完成控制台及接口调用配置。
前提条件
• 已完成开发者入驻以及实名认证。详情可查看 支付宝个人账号注册及实名认证、支付宝企业账号注册及实名认证。
• 已下载并安装下载密钥生成工具(支付宝开放平台开发助手)。
获取工具
• WINDOWS(Windows 版本工具请不要安装在含有空格的目录路径下,否则会导致公私钥乱码的问题)。
• MAC_OSX
原理介绍
支付宝开放平台开发助手提供了一键生成密钥功能,便于开发者生成一对 RSA 密钥(应用公钥、应用私钥)以及公钥证书申请 CSR 文件(在线申请应用公钥证书需要)。
注意事项
• 应用公钥(public key)需提供给支付宝账号管理者上传到支付宝开放平台。
• 应用私钥(private key)由开发者自己保存,需填写到代码中供签名时使用。
• 生成的私钥需妥善保管,避免遗失,不要泄露。
• 密钥和应用(APPID)一一对应,即开发者需要为名下的每个应用分别设置密钥,且不同应用的密钥不能混用。
公钥方式
本步骤指引以 MAC_OSX 界面为例。

  1. 根据开发语言选择密钥格式和密钥长度。
    说明:
    • 新建应用请务必使用 RSA2 密钥长度 即 2048 位。详情可查看 开放平台证书升级指南。
    • 目前已使用 RSA 密钥长度即 1024 位密钥长度的应用仍然可以正常调用接口。
    在这里插入图片描述
    点击 生成密钥,工具会自动生成商家应用公钥(public_key)和应用私钥(private_key)。随后点击 打开密钥文件路径。
    在这里插入图片描述
    在弹出的 RSA密钥 文件夹下即可找到生成的公私钥文件。
    根据 生成并配置密钥 指引完成密钥配置。

公钥证书方式
本步骤指引以 MAC_OSX 界面为例。
2. 在开发助手工具的生成密钥工具,密钥长度选择 RSA2,密钥格式选择 PKCS8(Java适用),点击 获取CSR文件 后的 点击获取,生成应用公钥证书 CSR 申请文件。
在这里插入图片描述

  1. 在弹出的获取 CSR 对话框中根据提示填写相关信息,支付宝会根据提供的信息创建证书的标识名称,用来识别证书,点击 生成 CSR 文件。
    说明:
    • 组织/公司:名称一定要和开发者中心门户账号信息的公司名称保持一致,否则会导致后续步骤中上传 CSR 证书文件校验失败。
    • 沙箱环境下配置 CSR 公钥证书时,若沙箱账号为:
    • 2020 年 07 月 24 日前获取的沙箱账号,组织/公司 需固定填写 沙箱环境。
    • 2020 年 07 月 24 日后获取的沙箱账号,组织/公司 需填写沙箱商家账号,例如:沙箱商家账号为 xxx@sandbox.com,组织/公司就需要配置成 xxx@sandbox.com。
    • 域名 建议填写商家/服务商公司域名地址。
    在这里插入图片描述
    在生成 CSR 文件后,点击 打开文件位置,在对应的文件夹里可以看到三个文件:应用公钥 key 串、应用私钥 key 串,以及 .csr 格式的应用公钥证书申请文件。
    在这里插入图片描述
    根据 生成并配置密钥 指引完成密钥配置

alipay.trade.close(统一收单交易关闭接口)

请求示例

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("trade_no", "2013112611001004680073956707");
request.setBizContent(bizContent.toString());
AlipayTradeCloseResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

响应示例

{
    "alipay_trade_close_response": {
        "code": "10000",
        "msg": "Success",
        "trade_no": "2013112111001004500000675971",
        "out_trade_no": "YX_001"
    },
    "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

xml配置

<alipay_trade_close_response> 
  <code>10000</code>  
  <msg>Success</msg>  
  <trade_no>2013112111001004500000675971</trade_no>  
  <out_trade_no>YX_001</out_trade_no> 
</alipay_trade_close_response>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

跳转支付宝链接
https://www.merchant.com/receive_notify.htm?notify_type=trade_status_sync¬ify_id=91722adff935e8cfa58b3aabf4dead6ibe¬ify_time=2017-02-16 21:46:15&sign_type=RSA2&sign=WcO+t3D8Kg71dTlKwN7r9PzUOXeaBJwp8/FOuSxcuSkXsoVYxBpsAidprySCjHCjmaglNcjoKJQLJ28/Asl93joTW39FX6i07lXhnbPknezAlwmvPdnQuI01HZsZF9V1i6ggZjBiAd5lG8bZtTxZOJ87ub2i9GuJ3Nr/NUc9VeY=&trade_no=null&app_id=null&out_trade_no=null&out_biz_no=null&buyer_logon_id=null&seller_id=null&seller_email=null&trade_status=null&total_amount=null&refund_fee=null&subject=null&body=null&gmt_create=null&gmt_payment=null&gmt_refund=null&gmt_close=null&fund_bill_list=null&receipt_amount=null&invoice_amount=null&buyer_pay_amount=null&point_amount=null&buyer_id=null

统一收单线下交易查询

package com.java.sdk.demo;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayTradeQuery {

    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCSmzXtQZafZ2rA/jI2vP+ww+BWQeMsvIYWntiaCcxsOCS/fRI6BwfcKatwPTrr5Dz39w8rDJymglPi/9VxpXylBLyXEL9G4z2+Dq5J4EY89oDlbyW1ocggE/owFNvbPwnkpn1hCXGxvwYG4iz7sOn8TeR3n+1UJH1o4p7xutq2jAsF5HmfGJQpB51WKX4DNn0vIkSeFZj3DfcQ57UlieWfCexmwf5yR590LtMbE+y7E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LJVqb6eqZ0JelsjOxac0qipA4zXPIaUIcHlEfevXKT/KcB4LfKc6mBTSlE0EBsc2LwC5N1v5KiLMckq3OScHGIkCgYANfP5cGyKySkn2BzTlILnWu+uPHuqaAIV6Fc+Z6h/WbdXw2opdEP3A7GEUQUY6fjYj6AD8Jutn3sz1yd7l5ly+TX6I6YFJ5xHcFcxvl9m/kZIbps+wUjn5mJYZaGcnm2ZYvUD+I0VERLX2yBWg0SWXGuY/Z3mwe4D8nJxZNC4o4g==";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjedhtEgisXmKUQ9B+P+IldGYB6Qmo8eZi5cZdJzXPDlDMwofCwQqr8+XTjbhhAWMkMdWbR+pewVjAjcelF0apeRyAJlf9rLi0fADc9XdK32fjPXyUKRRyUX5obguTNaC7w3DoQm5MYnxxaWStFisXkyrIKM3E5ibN0ansdCC8+zF5XfdOffEl/5ohgTM79l1e4LRM/O3ei52fs2Ip03coUrnfeWCvzActeLby+z1rFW9BY3u/DkcSlwAOl3XPIg1r9yulXSDm8W49TDvLgnMIEXyVoOgmjckyOl7vKWti9fiDJ+RnARve01EjIpZx5NNM1MuDx/4OkIiNydgTXJZ1wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000121663960");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

统一交易退款接口

package com.java.sdk.demo;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayTradeRefund {

    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCSmzXtQZafZ2rA/jI2vP+ww+BWQeMsvIYWntiaCcxsOCS/fRI6BwfcKatwPTrr5Dz39w8rDJymglPi/9VxpXylBLyXEL9G4z2+Dq5J4EY89oDlbyW1ocggE/owFNvbPwnkpn1hCXGxvwYG4iz7sOn8TeR3n+1UJH1o4p7xutq2jAsF5HmfGJQpB51WKX4DNn0vIkSeFZj3DfcQ57UlieWfCexmwf5yR590LtMbE+y7EDCCTtB/ZnAUYs0/C5kXI0HlD5+st5x1bV+33RqJzd29DN5zp/Og+KDYSLvqS2tz3XytUew4cPaqkaWgjSko9zCyuaYcTJrjM0VeqysxygT3AgMBAAECggEAC9lxrWB8vRuNvFWPV29FN1NNoYsnwmjmaZOjLCgYlrKe/b8tJvWEhB32FldMmN5UszILzs3xJ2AnSZSl+s6GWYwddhRLcX5ScjcbjSsMYkcGOgUWxixyUW1r7lDxSDI7KXjvb1VlFBm9cfb77cfp0ZO2IJkThJaC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ayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjedhtEgisXmKUQ9B+P+IldGYB6Qmo8eZi5cZdJzXPDlDMwofCwQqr8+XTjbhhAWMkMdWbR+pewVjAjcelF0apeRyAJlf9rLi0fADc9XdK32fjPXyUKRRyUX5obguTNaC7w3DoQm5MYnxxaWStFisXkyrIKM3E5ibN0ansdCC8+zF5XfdOffEl/5ohgTM79l1e4LRM/O3ei52fs2Ip03coUrnfeWCvzActeLby+z1rFW9BY3u/DkcSlwAOl3XPIg1r9yulXSDm8W49TDvLgnMIEXyVoOgmjckyOl7vKWti9fiDJ+RnARve01EjIpZx5NNM1MuDx/4OkIiNydgTXJZ1wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000121663960");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setRefundAmount("200.12");
        request.setBizModel(model);
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

查询对账单下载地址

package com.java.sdk.demo;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.alipay.api.domain.AlipayDataDataserviceBillDownloadurlQueryModel;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayDataDataserviceBillDownloadurlQuery {

    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCSmzXtQZafZ2rA/jI2vP+ww+BWQeMsvIYWntiaCcxsOCS/fRI6BwfcKatwPTrr5Dz39w8rDJymglPi/9VxpXylBLyXEL9G4z2+Dq5J4EY89oDlbyW1ocggE/owFNvbPwnkpn1hCXGxvwYG4iz7sOn8TeR3n+1UJH1o4p7xutq2jAsF5HmfGJQpB51WKX4DNn0vIkSeFZj3DfcQ57UlieWfCexmwf5yR590LtMbE+y7EDCCTtB/ZnAUYs0/C5kXI0HlD5+st5x1bV+33RqJzd29DN5zp/Og+KDYSLvqS2tz3XytUew4cPaqkaWgjSko9zCyuaYcTJrjM0VeqysxygT3AgMBAAECggEAC9lxrWB8vRuNvFWPV29FN1NNoYsnwmjmaZOjLCgYlrKe/b8tJvWEhB32FldMmN5UszILzs3xJ2AnSZSl+s6GWYwddhRLcX5ScjcbjSsMYkcGOgUWxixyUW1r7lDxSDI7KXjvb1VlFBm9cfb77cfp0ZO2IJkThJaCcGTItf8FSHW/Lm6dmbo+HXnGyqhN9rPjAwzk4ieo1PsN1oyNJ1knOfWkIbQXFzYjQXZtBtiePMlbNuBAzAAx2cJ8lI6F+42wEvmYD2g4YEEFLwgoML+jMUCKHykO1uFVfK0WoxMOOLxU4OXxLRWbKyGtc+bzTONfKfJK6VRVJWYjnOCMzB2iaQKBgQDwT27J+WDZNb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IKM3E5ibN0ansdCC8+zF5XfdOffEl/5ohgTM79l1e4LRM/O3ei52fs2Ip03coUrnfeWCvzActeLby+z1rFW9BY3u/DkcSlwAOl3XPIg1r9yulXSDm8W49TDvLgnMIEXyVoOgmjckyOl7vKWti9fiDJ+RnARve01EjIpZx5NNM1MuDx/4OkIiNydgTXJZ1wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000121663960");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
        AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();
        model.setBillType("trade");
        model.setBillDate("2016-04-05");
        request.setBizModel(model);
        AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
package com.alipay.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.resource.AliPayResource;
import com.alipay.utils.DateUtil;
import com.alipay.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author: HuGoldWater
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("payment")
public class AlipayController {

    @Autowired
    private AliPayResource aliPayResource;

    /**
     * 前往支付宝进行支付
     */
    @GetMapping(value="/goAlipay")
    public JsonResult goAlipay(String merchantUserId, String merchantOrderId) throws Exception{
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayResource.getGatewayUrl(),
                aliPayResource.getAppId(),
                aliPayResource.getMerchantPrivateKey(),
                "json",
                aliPayResource.getCharset(),
                aliPayResource.getAlipayPublicKey(),
                aliPayResource.getSignType());

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(aliPayResource.getReturnUrl());
        alipayRequest.setNotifyUrl(aliPayResource.getNotifyUrl());

        // 商户订单号, 商户网站订单系统中唯一订单号, 必填
        String out_trade_no = merchantOrderId;
        // 付款金额, 必填 单位元
       String total_amount = "0.01";  // 测试用 1分钱
        // 订单名称, 必填
        String subject = "胡金水-付款用户[" + merchantUserId + "]";
        // 商品描述, 可空, 目前先用订单名称
        String body = subject;

        // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
        String timeout_express = "1h";
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String alipayForm = "";
        try {
            alipayForm = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        log.info("支付宝支付 - 前往支付页面, alipayForm: \n{}", alipayForm);
        return JsonResult.ok(alipayForm);
    }


    /**
     * 支付成功后的支付宝异步通知
     */
    @RequestMapping(value="/alipay")
    public String alipay(HttpServletRequest request, HttpServletResponse response) throws Exception {

        log.info("支付成功后的支付宝异步通知");

        //获取支付宝POST过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> 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] + ",";
            }
            //乱码解决,这段代码在出现乱码时使用
//       valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        boolean signVerified = AlipaySignature.rsaCheckV1(params,
                aliPayResource.getAlipayPublicKey(),
                aliPayResource.getCharset(),
                aliPayResource.getSignType()); //调用SDK验证签名

        if(signVerified) {//验证成功
            // 商户订单号
            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
            // 支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
            // 交易状态
            String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
            // 付款金额
            String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8");

            if (trade_status.equals("TRADE_SUCCESS")){
//                String merchantReturnUrl = paymentOrderService.updateOrderPaid(out_trade_no, CurrencyUtils.getYuan2Fen(total_amount));
//                notifyFoodieShop(out_trade_no,merchantReturnUrl);
            }

            log.info("************* 支付成功(支付宝异步通知) - 时间: {} *************", DateUtil.getCurrentDateString(DateUtil.DATETIME_PATTERN));
            log.info("* 订单号: {}", out_trade_no);
            log.info("* 支付宝交易号: {}", trade_no);
            log.info("* 实付金额: {}", total_amount);
            log.info("* 交易状态: {}", trade_status);
            log.info("*****************************************************************************");

            return "success";
        }else {
            //验证失败
            log.info("验签失败, 时间: {}", DateUtil.getCurrentDateString(DateUtil.DATETIME_PATTERN));
            return "fail";
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/700397
推荐阅读
相关标签
  

闽ICP备14008679号