67,513
社区成员
发帖
与我相关
我的任务
分享
@RestController
@RequestMapping("/payment")
public class PaymentController {
// 支付宝重要参数
private static String APP_ID = "";
private static String APP_PRIVATE_KEY = "";
private static String CHARSET = "utf-8";
private static String ALIPAY_PUBLIC_KEY = "";
/**
* 对支付宝支付信息进行签名
*
* @param info
* 数据类
* @return
* @throws AlipayApiException
* @throws UnsupportedEncodingException
*/
@PostMapping("/sign")
public Object sign(@RequestBody SignInfo info) throws AlipayApiException, UnsupportedEncodingException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String appID = APP_ID;
String bizContent = toJson(info.Content);
String charset = CHARSET;
String method = "alipay.trade.app.pay";
String signType = "RSA";
String timestamp = sdf.format(new Date());
String version = "1.0";
String notify_url = "https://pay.ytbapp.com/payment/notify";// 增加支付异步通知回调,记住上下notify_url的位置,全在sign_type之前,很重要,同样放在最后都不行
String content = "app_id=" + appID + "&biz_content=" + bizContent + "&charset=" + charset + "&method=" + method
+ "¬ify_url=" + (notify_url) + "&sign_type=" + signType + "×tamp=" + timestamp + "&version="
+ version;
String sign = AlipaySignature.rsaSign(content, APP_PRIVATE_KEY, charset);
return "{\"Result\": \"app_id=" + encode(appID) + "&biz_content=" + encode(bizContent) + "&charset="
+ encode(charset) + "&method=" + encode(method) + "¬ify_url=" + encode(notify_url) + "&sign_type="
+ encode(signType) + "×tamp=" + encode(timestamp) + "&version=" + encode(version) + "&sign="
+ encode(sign) + "\"}";
}
private String encode(String sign) throws UnsupportedEncodingException {
return URLEncoder.encode(sign, "utf-8").replace("+", "%20");
}
private String toJson(BizContent content) {
String context = "";
context += "{" + "\"timeout_express\":\"" + content.timeout_express + "\"," + "\"seller_id\":\""
+ content.seller_id + "\"," + "\"product_code\":\"" + content.product_code + "\","
+ "\"total_amount\":\"" + content.total_amount + "\"," + "\"subject\":\"" + content.subject + "\","
+ "\"body\":\"" + content.body + "\"," + "\"out_trade_no\":\"" + content.out_trade_no + "\"}";
return context;
}