赞
踩
简介
本文详细介绍通过支付宝开放平台开发助手生成密钥对、支付宝证书申请 CSR 文件步骤。
说明:生成密钥、CSR 文件后,开发者需根据 生成并配置密钥 指引,完成控制台及接口调用配置。
前提条件
• 已完成开发者入驻以及实名认证。详情可查看 支付宝个人账号注册及实名认证、支付宝企业账号注册及实名认证。
• 已下载并安装下载密钥生成工具(支付宝开放平台开发助手)。
获取工具
• WINDOWS(Windows 版本工具请不要安装在含有空格的目录路径下,否则会导致公私钥乱码的问题)。
• MAC_OSX
原理介绍
支付宝开放平台开发助手提供了一键生成密钥功能,便于开发者生成一对 RSA 密钥(应用公钥、应用私钥)以及公钥证书申请 CSR 文件(在线申请应用公钥证书需要)。
注意事项
• 应用公钥(public key)需提供给支付宝账号管理者上传到支付宝开放平台。
• 应用私钥(private key)由开发者自己保存,需填写到代码中供签名时使用。
• 生成的私钥需妥善保管,避免遗失,不要泄露。
• 密钥和应用(APPID)一一对应,即开发者需要为名下的每个应用分别设置密钥,且不同应用的密钥不能混用。
公钥方式
本步骤指引以 MAC_OSX 界面为例。
公钥证书方式
本步骤指引以 MAC_OSX 界面为例。
2. 在开发助手工具的生成密钥工具,密钥长度选择 RSA2,密钥格式选择 PKCS8(Java适用),点击 获取CSR文件 后的 点击获取,生成应用公钥证书 CSR 申请文件。
请求示例
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("调用失败");
}
响应示例
{
"alipay_trade_close_response": {
"code": "10000",
"msg": "Success",
"trade_no": "2013112111001004500000675971",
"out_trade_no": "YX_001"
},
"sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}
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>
跳转支付宝链接
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+1UJH1o4p7xutq2jAsF5HmfG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nnagJsrNkm843pH/1eWUSHlVWaGkrk8sRAw3EiFL74tPIym8vVOPkYIOLJVqb6eqZ0JelsjOxac0qipA4zXPIaUIcHlEfevXKT/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("调用失败");
}
}
}
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+s6GWYwddhRLcX5ScjcbjSsMYkcGOgUWxixyUW1r7lDxSDI7KXjvb1VlFBm9cfb77cfp0ZO2IJkThJaCcGTItf8FSHW/Lm6dmbo+HXnGyqhN9rPjAwzk4ieo1PsN1oyNJ1knOfWkIbQXFzYjQXZtBtiePMlbNuBAzAAx2cJ8lI6F+42wEvmYD2g4YEEFLwgoML+jMUCKHykO1uFVfK0WoxMOOLxU4OXxLRWbKyGtc+bzTONfKfJK6VRVJWYjnOCMzB2iaQKBgQDwT27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FisXkyrIKM3E5ibN0ansdCC8+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("调用失败");
}
}
}
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/9VxpXylBLy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LzHmq/b9wMzLBaCjyF8XVszRY9QGvf+OrhvCXUiKGQxobWvAbkcqQ88tgqu5pAoGASttAirD52FQ3AENgjrd7bfWaHFm9nnagJsrNkm843pH/1eWUSHlVWaGkrk8sRAw3EiFL74tPIym8vVOPkYIOLJVqb6eqZ0JelsjOxac0qipA4zXPIaUIcHlEfevXKT/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);
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("调用失败");
}
}
}
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";
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。