说明: 本帖是利用支付宝沙箱测试当面付支付接口,不是官方demo,是我自己写的java简版示例,一个接口一个java 文件,一个main方法,导入环境,修改参数即可运行,demo中会配置个人的沙箱账号信息,方便大家测试。
测试环境:Eclipse+JDK1.6及以上
测试流程
1、创建应用,生成密钥
2.下载sdk 3.公共参数配置(沙箱环境测试参数)
package com.alipay.config;
/**
* 基本信息配置
* @author wb-wly251833
*
*/
public class AlipayConfig {
//沙箱APPID
public static final String app_id = "2016101800718925";
//沙箱私钥
public static final String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDfDBUJhaw46ck8MHccIBXPt2USOvCRZ0dQNZZwC0EmZvcnL2+NUyoJMbgcV3U5o6wApajkpibf9UkrtYgI3/GZhOgzfzH0IJYAXmYCEniM+Cl5ipIWhY0W/s+uNRGNdXvNlketNNcVqAvkKZdcHz23N60BXsvjdR+BJ/np2CUmr9OCv0aqqwOO190rOuZvVaFCJD5LDmvDFdXPzrJHcOAyYjb2hhJIWt5n4o/b3LMYeuxfwWuQ4VlGx+s0wZJ1udUyAX8X8byqYcnevG8A1sQd/dRVNr8f1BxS8kjDIDljCzKPoYyoaoeJMhJIxrV8gL57oEIX7PbrlvFLplaC7g/XAgMBAAECggEAI4m/SFceC6tkPszSDY8nKoNj/TWa4u/7stH2+D8y0DWTp5CtS202w2RCm3Fr4hUr8KZ0dInso75nPCmeVRupWPaOMLZRdT5D6CciRa4/uuZHZXAouqHoywMqf+1AsLNJWIZbQUOsCW1PB7mAvlLT+H9Md21tOv6MWH3QGewwbytEcri0YkzroQDso3P08xdZDpXtrmg34tLnrNhw+gl8LOtpkbd0SOrGAsoTAE1vTbSLtTR2PAW7RQZkyKW1ansouzwduamFMhTc+DTc40ODJ5b5xlwTDrXn681E7UTX7JytYTPmjrKFHceX9hQ3vxSkm0fEQ7sXoF0pOBL+7AkOIQKBgQD00qCxCSVE8x2Xb/shayGBLCumqIzHtH5Eb42Wz8AOCH2/Ns9Hah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poTBXdqfnJNrTVAMqcQhrIWwgpSVgmUq+UhlZsDaiUvxWWFm9v9nY172jFfzup/DIET//OpIghL6AB2F+ywkCcHdZHiGklnpNoQKQ6XMVKaO/k2nlHU64Y4WGSgJnSG6B798ohZoi3pndrVPAH5+5BKEFQIDAQAB";
//沙箱网关地址
public static final String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
//正式环境网关地址
// public static final String gatewayUrl = "https://openapi.alipay.com/gateway.do";
// 签名方式
public static String sign_type = "RSA2";
// 字符编码格式
public static String charset = "utf-8";
public static String format = "json";
}
4.执行代码
package com.alipay.demo;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.config.AlipayConfig;
/**
*
* alipay.trade.refund (统一收单交易退款接口)
* @author wb-wly251833
*
*/
public class AlipayTradeFefundTest {
public static void main(String[] args) throws AlipayApiException {
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
//外部订单号
model.setOutTradeNo("21070921129550562525");
//退款金额
model.setRefundAmount("0.02");
//标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传。
model.setOutRequestNo("HZ01RF2221");
request.setBizModel(model);
AlipayTradeRefundResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
System.out.println(response.getBody());
}
}
5.demo示例:当面付demo.zip