商户签署无密支付协议,然后通过代扣的方式发红包,参数详情点击参考文档 我这边分享一下java版的示例供大家参考,适用于jdk版本1.5及以上的开发环境 ,sdk可点击下载
2.代码示例:
/**
*
*/
/**
* @author wb-jm273093
*
*/
package alipay.com.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundCouponOrderAgreementPayModel;
import com.alipay.api.request.AlipayFundCouponOrderAgreementPayRequest;
import com.alipay.api.response.AlipayFundCouponOrderAgreementPayResponse;
import alipay.com.config.Config;
/**
* 红包协议支付接口(商户即请求的appid账号付款,无需输入密码)
* @author wb-wly251833
* 文档链接:https://docs.open.alipay.com/api_5/alipay.fund.coupon.order.agreement.pay
*/
public class AlipayFundCouponOrderAgreementPay {
public static void main(String[] args) throws AlipayApiException {
//在SDK调用前需要进行初始化
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", Config.appId, Config.privateKey, "json","utf-8", Config.alipayPulicKey, "RSA");
AlipayFundCouponOrderAgreementPayRequest request = new AlipayFundCouponOrderAgreementPayRequest();
AlipayFundCouponOrderAgreementPayModel model = new AlipayFundCouponOrderAgreementPayModel();
//商户的授权资金订单号 (自己填写,必须保证唯一性)
model.setOutOrderNo(getoutOrderNo());
//商户本次资金操作的请求流水号 (自己填写,必须保证唯一性)
model.setOutRequestNo(getoutOrderNo()+"test");
//订单描述
model.setOrderTitle("红包协议支付接口");
//金额,取值范围:[0.01,100000000.00]
model.setAmount("0.01");
//付款方的支付宝唯一用户号,以2088开头的16位纯数字组成
model.setPayerUserId("2088411964648546");
//该笔订单允许的最晚付款时间
model.setPayTimeout("1h");
request.setBizModel(model);
AlipayFundCouponOrderAgreementPayResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("调用成功!");
}else {
System.out.println("调用失败!");
}
System.out.println(response.getBody());
}
/**
* 随机生成订单号
* @return
*/
private static String getoutOrderNo() {
// TODO Auto-generated method stub
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String str = simpleDateFormat.format(date);
Random random = new Random();
int rannum = (int) (random.nextDouble() * (999 - 100 + 1)) + 100;// 获取5位随机数
return str+rannum;// 当前时间
}
}
如果没有权限,则会返回
调用失败!
{"alipay_fund_coupon_order_agreement_pay_response":{"code":"40004","msg":"Business Failed","sub_code":"ERROR_BALANCE_PAYMENT_DISABLE","sub_msg":"余额支付功能关闭","amount":"0.01","auth_no":"2018010410002001540285562705","operation_id":"20180104462225275402","out_order_no":"20180104145007943","out_request_no":"20180104145007657test","status":"INIT"},"sign":"XXXX"}
由于这边权限过期了,所以没有成功返回的信息,但是代码运行是没问题的 该接口是直接使用的alipayClient.execute(request);,几个红包接口调用的方法都不一样,大家要区别开来,不要混用了。 如有其它问题,欢迎论坛评论留言,如有不足之处也欢迎多多吐槽!!!