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Oyxqjvkbm36K1YPQ1ApzPwf2EqpOOk+ukfDBxdHQPHSXh2J3Lyh8Ho8Ak7yQs0tSVVrtSct+JQCSTWgai7aYCVyJuoO8bBFYkz6XuiECgYEA5S06kNpv9oe32u6iIA5TV+mfXPVY4OskoQvMymz3JTC7zuFeq2zg8IRFb2S4u95Gaa+bVj6gpQDiLIVQKAzMLq3S6LdiegzsvGjDtWYdUYYfsY/eXGj4kcVb336hLeP3WbZ7oodsYPg+ddvmJ7Sv2DQcbJC2NEMrvVceKAeVUzcCgYEA0umx9QiBwtdhxXDWPZ75wbQBz1MnUCVEkqdUzdCRxQFCqaD+1JGNPlLexg1hLukfKHcMHmHW3e2v4viboWDIQPJIvJkRCeGJQy0I+aWBks4JKUwgsN/5EznLAk/gov6k4kjq/YSSi6NEhvprBr/8JW5ETVvs2X74qnvjUZiOI5MCgYEAtGfEQJ8ToBf6TSepDF76kKvhvYWNr5CrRvNJBNh+PRJ95T9YDM4qzGrzVdsN6SXye+O2/k9SmlF1ueI/1Sct3FkNPBCQPs/J1A8McNGvpqtUa6x7OHeRpP6dcfP24W8hGcgizzCXykXgtVqjdQIxoppy/at1IvL1jZj0rkI59EUCgYEAmNsnzyd4K8a/JEIP8x7pcbpM6X4hatNnrEW7j2JOkxHDx35X0aUfUYDr1WvS+BWaMFB/JvHPsI3XwE5fNIkY+QDfOFhS4e9j9JyJ+aw6iTPrrdNvieD4+jcTazyK2HZm91zTeiL/tlYAjzakJIM1ccdFly89K6/69kvNRtCiiyMCgYBFGO3jzVDwEejO4vKb2LI2HwQ+yrKbA7MJI2z/8+Sqpy1yMutVk7KCNeOjxSohSV9hwGKeAfCAd+1VBjGLAz2mGLpm75WfWj0AGySijFTC7q6WXu2zJzzLM+L8J/p92nnhGkfSFNkXqJRyTp91cJi7mHJbzzqoN5E0n63R7XGFaw==
2.填写沙箱环境
填写应用公钥后,就会出现右边的“查看支付宝公钥”,两者不一样,代码中使用后者。
支付宝公钥
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAytOPpZDw2DiAHC/vfGeDAGjxU/E6GCJ9Z3OSHkXSrGkob32WGfOQRcOfqJURRrDAfOMBETeazhZzcE/nrwrCb/e2L8s+jO3zGYaB2AhqpWyhmV9w8EkL4BFuwFxVUupaUNSvfltg7BgPjPI/1S7zTLdO+XT2F5KJKvn20ihYley6WLXTFhTo999myeB9EKSFwNkRLZx2JDHn0DWIZTQJRJmGRd79GDwSV/A9iMNnA9IzCFD9heaaQaKFiBZ8ZJkQs/u1PTnq7mszHMoaQdXbw2S853j74QRKansaf84k1Be8pHeym9Xxp2qY8H8PZYfVdmnmRS655kCxY4HcJWvLhQIDAQAB
3.编写代码
// 商户appid
public static String APPID = "沙箱的appId";
// 私钥 pkcs8格式的
public static String RSA_PRIVATE_KEY = "前面生成的私钥";
// 请求网关地址
public static String URL = "https://openapi.alipaydev.com/gateway.do";
// 支付宝公钥
public static String ALIPAY_PUBLIC_KEY = "填写沙箱环境-填写公钥后,查看支付宝公钥";
// RSA2
public static String SIGNTYPE = "RSA2";
参考代码:
com.alipay.sdk
alipay-sdk-java
3.4.49.ALL
加载数据
// 支付宝需要信息
var vNow = new Date();
var sNow = "";
sNow += String(vNow.getFullYear());
sNow += String(vNow.getMonth() + 1);
sNow += String(vNow.getDate());
sNow += String(vNow.getHours());
sNow += String(vNow.getMinutes());
sNow += String(vNow.getSeconds());
sNow += String(vNow.getMilliseconds());
$("#WIDout_trade_no").val(sNow); // 订单号,必填
$("#WIDsubject").val(product.productName); // 订单名称,必填
$("#WIDtotal_amount").val(product.promotionPrice); // 付款金额,必填
$("#WIDbody").val(product.productDesc); // 商品描述,选填
提交数据
支付
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.AlipayTradeWapPayRequest;
import org.baozi.config.AlipayConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@Controller
public class AlipayController {
@PostMapping("/alipay/pay")
public void doPost(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
String out_trade_no = request.getParameter("WIDout_trade_no");
String subject = request.getParameter("WIDsubject");
String total_amount = request.getParameter("WIDtotal_amount");
String body = request.getParameter("WIDbody");
String timeout_express="2m";
String product_code="QUICK_WAP_WAY";
// 还可以多获取些其他的信息(比如下订单的用户id),生成订单,存入数据库。
// 或者回调之后,获取相应消息生成订单
/**********************/
// SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签
// 调用RSA签名方式
AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();
// 封装请求支付信息
AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
model.setOutTradeNo(out_trade_no);
model.setSubject(subject);
model.setTotalAmount(total_amount);
model.setBody(body);
model.setTimeoutExpress(timeout_express);
model.setProductCode(product_code);
alipay_request.setBizModel(model);
// 设置异步通知地址
// alipay_request.setNotifyUrl(AlipayConfig.notify_url);
// 设置同步地址
alipay_request.setReturnUrl(AlipayConfig.return_url);
// form表单生产
String form = "";
try {
// 调用SDK生成表单
form = client.pageExecute(alipay_request).getBody();
response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
response.getWriter().write(form);//直接将完整的表单html输出到页面
response.getWriter().flush();
response.getWriter().close();
} catch (AlipayApiException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ApiConfig配置信息
public class AlipayConfig {
// 商户appid
public static String APPID = "2016092400584458";
// 私钥 pkcs8格式的
public static String R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k/gov6k4kjq/YSSi6NEhvprBr/8JW5ETVvs2X74qnvjUZiOI5MCgYEAtGfEQJ8ToBf6TSepDF76kKvhvYWNr5CrRvNJBNh+PRJ95T9YDM4qzGrzVdsN6SXye+O2/k9SmlF1ueI/1Sct3FkNPBCQPs/J1A8McNGvpqtUa6x7OHeRpP6dcfP24W8hGcgizzCXykXgtVqjdQIxoppy/at1IvL1jZj0rkI59EUCgYEAmNsnzyd4K8a/JEIP8x7pcbpM6X4hatNnrEW7j2JOkxHDx35X0aUfUYDr1WvS+BWaMFB/JvHPsI3XwE5fNIkY+QDfOFhS4e9j9JyJ+aw6iTPrrdNvieD4+jcTazyK2HZm91zTeiL/tlYAjzakJIM1ccdFly89K6/69kvNRtCiiyMCgYBFGO3jzVDwEejO4vKb2LI2HwQ+yrKbA7MJI2z/8+Sqpy1yMutVk7KCNeOjxSohSV9hwGKeAfCAd+1VBjGLAz2mGLpm75WfWj0AGySijFTC7q6WXu2zJzzLM+L8J/p92nnhGkfSFNkXqJRyTp91cJi7mHJbzzqoN5E0n63R7XGFaw==";
// 请求网关地址
public static String URL = "https://openapi.alipaydev.com/gateway.do";
// 支付宝公钥
public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAytOPpZDw2DiAHC/vfGeDAGjxU/E6GCJ9Z3OSHkXSrGkob32WGfOQRcOfqJURRrDAfOMBETeazhZzcE/nrwrCb/e2L8s+jO3zGYaB2AhqpWyhmV9w8EkL4BFuwFxVUupaUNSvfltg7BgPjPI/1S7zTLdO+XT2F5KJKvn20ihYley6WLXTFhTo999myeB9EKSFwNkRLZx2JDHn0DWIZTQJRJmGRd79GDwSV/A9iMNnA9IzCFD9heaaQaKFiBZ8ZJkQs/u1PTnq7mszHMoaQdXbw2S853j74QRKansaf84k1Be8pHeym9Xxp2qY8H8PZYfVdmnmRS655kCxY4HcJWvLhQIDAQAB";
// RSA2
public static String SIGNTYPE = "RSA2";
// 编码
public static String CHARSET = "UTF-8";
// 返回格式
public static String FORMAT = "json";
// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址
public static String return_url = "http://localhost:8081/frontend/index";
// 日志记录目录
// public static String log_path = "/log";
// 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
// public static String notify_url = "http://localhost:8081/frontend/index";
}