java实现支付宝支付功能代码
时间: 2023-03-13 09:13:04 浏览: 172
java实现支付宝支付功能
可以参考以下示例代码,实现支付宝支付功能:import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;public class AlipayUtil {
/**
* 支付宝支付功能
* @param out_trade_no 商户订单号,商户网站订单系统中唯一订单号,必填
* @param total_amount 付款金额,必填
* @param subject 订单名称,必填
* @param body 商品描述,可空
*/
public static String pay(String out_trade_no,String total_amount,String subject,String body){
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求
String result;
try {
result = alipayClient.pageExecute(alipayRequest).getBody();
} catch (Exception e) {
return "支付宝支付失败!";
}
//输出
return result;
}
}
阅读全文