支付宝预授权接口调用示例分享

来自    APP支付与登录 | 
作者:天籁  |  更新于:2018-12-05 15:44:14     

一、线上资金授权冻结接口代码示例

1.model方法传参示例

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",app_id,your_private_key,format,CHARSET,alipay_public_key,sign_type);
		AlipayFundAuthOrderAppFreezeRequest request = new AlipayFundAuthOrderAppFreezeRequest();
	    AlipayFundAuthOrderAppFreezeModel model = new AlipayFundAuthOrderAppFreezeModel();
	    model.setOrderTitle("大家的预授权");
	    model.setOutOrderNo("20180772352451438125");
	    model.setOutRequestNo("20180277323149418239");
	    model.setPayeeUserId("对应请求APPID的PID");
	    model.setPayeeLogonId("对应APPID的登入账号");//PayeeUserId和PayeeLogonId  2选1,值空不要传入请求
	    model.setProductCode("PRE_AUTH_ONLINE");
	    model.setAmount("0.02");
	    model.setPayTimeout("2d");
	    model.setExtraParam("{\"category\":\"CHARGE_PILE_CAR\"}"); 
	    model.setEnablePayChannels("[{\"payChannelType\":\"PCREDIT_PAY\"},{\"payChannelType\":\"MONEY_FUND\"},"
	                + "{\"payChannelType\":\"CREDITZHIMA\"}]");
	    request.setBizModel(model); //这句代码要加,不然签名的时候biz里面的业务参数无法传递到签名中
	    request.setNotifyUrl("外网可以访问的异步地址");
	    AlipayFundAuthOrderAppFreezeResponse response = alipayClient.sdkExecute(request);
	    if(response.isSuccess()){
	        System.out.println("调用成功");
	       // logger.info("response: {}"+response.getBody());
	        
	        System.out.println(response.getBody());
	    } else {
	        System.out.println("调用失败");
	    }
	}

 

2.json传参示例

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
AlipayFundAuthOrderAppFreezeRequest request = new AlipayFundAuthOrderAppFreezeRequest();
request.setBizContent("{"+
			"\"order_title\": \"支付宝预授权\","+
			"\"out_order_no\": \"x111153XX773249\","+
			"\"out_request_no\": \"22li153XX73250\","+
			"\"payee_user_id\": \"卖家PID\","+
			"\"product_code\": \"PRE_AUTH_ONLINE\","+
			"\"amount\": 0.02,"+
			"\"extra_param\":\"{\\\"payChannelType\\\":\\\"PCREDIT_PAY\\\"},{\\\"payChannelType\\\":\\\"MONEY_FUND\\\"}]\"," +
			"\"pay_timeout\":\"2d\""+
		"}");
request.setNotifyUrl(config.notify_url);
AlipayFundAuthOrderAppFreezeResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}

3.调用接口冻结成功 异步信息示例

operation_id=20180912XXXX67702&auth_no=2018091210XXXXX0224440488&sign_type=RSA2&auth_app_id=201410XXXX13222&payee_user_id=208850XXXXX60335&out_order_no=20280XXXX52452438122&notify_type=fund_auth_freeze&payer_user_id=2088122XXXX15771&version=1.0&amount=0.01&rest_amount=0.01&notify_time=2018-09-12 17:30:32&status=SUCCESS&charset=utf-8&operation_type=FREEZE&sign=KTsWxjMmn4giDV9Fv7QJ8DNkyI52ikmOkKoFre8fUjMJhe4MLU3X7lyf2srgwOr0KRIkeyiZabEY0lTbkliSTSf3nYuiGGh6xG8FQFr2Ih3y4XPo9mbzZwCmB+OBHQUWTPRdX0qgym6GqveL3vse7+kS0foSg+eNYuNHq0J5GFmHHxYOPkAnLxHewUs3bO8tJPIs54o4wrrvTuN9+ggNvbCqD3hq19PCFvfLa9sxQiMg+SBBapSwlWCTddlQO9xnKzJmBHswzU6drPrYs4UFF93K9JdwmF0VkA1vIA5/coCo0d3rv5eBftDuYulAxZ/3F7FxEdLB1EGJHPoBXgrDmA==&total_unfreeze_amount=0.00&payee_logon_id=ali***@alipay.com&gmt_create=2018-09-12 17:30:27&total_freeze_amount=0.01&out_request_no=2018121242XXXX418233&payer_logon_id=180****0062&app_id=20141XXXXXX13222&notify_id=2018091XXXX173032051770206486066&total_pay_amount=0.00&gmt_trans=2018-09-12 17:30:32

 

二、授权转支付接口调用示例

1.model方法传参示例

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",app_id,your_private_key,format,CHARSET,alipay_public_key,sign_type);
	    
		AlipayTradePayRequest request = new AlipayTradePayRequest();
	    
	    AlipayTradePayModel model = new AlipayTradePayModel();
	    model.setOutTradeNo("服务商赋值保持唯一"); // 预授权转支付商户订单号,为新的商户交易流水号
	    model.setProductCode("PRE_AUTH_ONLINE"); // 固定值PRE_AUTH_ONLINE
	    model.setAuthNo("冻结接口异步返回的值"); // 填写预授权冻结交易号
	    model.setSubject("预授权转支付测试"); // 解冻转支付标题,用于展示在支付宝账单中
	    model.setTotalAmount("0.01"); // 结算支付金额
	    model.setSellerId("卖家PID"); // 填写卖家支付宝账户pid
	    model.setBuyerId("买家userid"); // 填写预授权用户uid,通过预授权冻结接口返回的payer_user_id字段获取
	  //  model.setStoreId("test_store_id"); // 填写实际交易发生的终端编号,与预授权的outStoreCode保持一致即可   
	    model.setBody("预授权解冻转支付测试"); // 可填写备注信息
	    model.setAuthConfirmMode("COMPLETE");//必须使用COMPLETE,传入该值用户剩余金额会自动解冻
	    request.setBizModel(model);
	   request.setNotifyUrl("外网可以访问的异步地址");//异步通知地址,必填,该接口只通过该参数进行异步通知
	    
	    AlipayTradePayResponse response = alipayClient.execute(request);
	 //   AlipayFundAuthOrderAppFreezeResponse response = alipayClient.execute(request);
	    if(response.isSuccess()){
	        System.out.println("调用成功");
	    //    logger.info("response: {}"+response.getBody());
	        System.out.println(response.getBody());
	    } else {
	        System.out.println("调用失败");
	    }

 

2.json传参示例

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",app_id,your_private_key,format,CHARSET,alipay_public_key,sign_type);
	AlipayTradePayRequest request = new AlipayTradePayRequest();
	
	request.setNotifyUrl("外网可以访问的异步地址");
	request.setBizContent("{" +
	"\"out_trade_no\":\"2018077735255938023\"," +
	"\"auth_no\":\"冻结异步通知返回值\"," + //切记这里不要使用成了alipay.trade.pay 里面的auth_code参数
	"\"subject\":\"预授权冻结\"," +
	"\"total_amount\":0.01," +
	"\"product_code\":\"PRE_AUTH_ONLINE\"," +
	"\"buyer_id\":\"买家userid\"," +

	"\"seller_id\":\"卖家PID\"," +
	"\"auth_confirm_mode\":\"COMPLETE\"" + //传入这个参数后调用支付接口会自动解冻剩余金额,如果需求手动解冻此参不传
	"}");

	  AlipayTradePayResponse response = alipayClient.execute(request);
	 System.out.println(response.getBody());
	if(response.isSuccess()){
	System.out.println("调用成功");
	
	} else {
	System.out.println("调用失败");
	}

auth_no这个参数的值是线上资金授权冻结接口异步通知返回的,且在冻结转支付接口参数名称为auth_no,切记不要传入为auth_code

 

3.调用接口成功同步返回示例

{"alipay_trade_pay_response":{"code":"10000","msg":"Success","buyer_logon_id":"180****xx62","buyer_pay_amount":"0.00","buyer_user_id":"2088xxxxx15771","invoice_amount":"0.00","out_trade_no":"2018xxxx3344466","point_amount":"0.00","receipt_amount":"0.00","total_amount":"0.01","trade_no":"201809122xxxxx71003784359"},"sign":"MJQY77D7s1ueE6dHbKndIV6gHOWUtBrob8ct2kXiPjKuxRrQ5PXhsIbtodB74Zb3+B9+SFRjMsd62b2oF0YARIWdjkRBntCOw7afThNnteof53eF3H9MGIIUfdNfdwPOHgwyTpowFK3xlWzt7Qw8r/cQKvi7Wiaq5jmxbdchQX0fpbImBStaopGtZFrpikHgwM9PZwWqv59o8jW7DUL8cDZDPFxouTYuZfU+n5J3tsnUtCOTMsHhZVAgpbtx66L76dlXeJbBPGdMOYIfZr1QfVZfeyMrPoH2betAX/2KJ3QDVXjYKkN1/c6p3ynuBc5TxuzQD7s58D0mv3fLORG1HA=="}

 

三、资金授权操作查询接口调用示例

示例代码:

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",app_id,your_private_key,format,CHARSET,alipay_public_key,sign_type);
		AlipayFundAuthOperationDetailQueryRequest request = new AlipayFundAuthOperationDetailQueryRequest();
		request.setBizContent("{" +
		//"\"auth_no\":\"冻结接口异步通知返回的\"," +
		"\"out_order_no\":\"冻结接口异步通知返回的\"," +
	  //  "\"operation_id\":\"冻结接口异步通知返回的\"," +
		"\"out_request_no\":\"冻结接口异步通知返回的\"" +
		"}");
     
     //PS:上面参数1配对3,2配对4,1配对4,2配对3进行调用,不能1配对2,3配对4,
		AlipayFundAuthOperationDetailQueryResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
		System.out.println("调用成功");
		  System.out.println(response.getBody());
		} else {
		System.out.println("调用失败");
		}

 

同步返回示例:

{"alipay_fund_auth_operation_detail_query_response":{"code":"10000","msg":"Success","amount":"0.0yi1","auth_no":"2018091xxxxx001770224440488","extra_param":"{}","gmt_create":"2018-09-12 17:30:27","gmt_trans":"2018-09-12 17:30:32","operation_id":"2018091xxxx67702","operation_type":"FREEZE","order_title":"大家的预授权","out_order_no":"2028077xxx452438122","out_request_no":"201812xxx129418233","payer_logon_id":"180****0xx62","payer_user_id":"2088122xxxx771","remark":"大家的预授权","rest_amount":"0.00","status":"SUCCESS","total_freeze_amount":"0.01","total_pay_amount":"0.01"},"sign":"MeJZ0ID2nxHF/T+03ynOeK4FEleL6wKJ4zpa7kBjHokmM3LlNKuPmX5otYv2/HXoNgucFaWxzOwjAuBfcgf4jTfAFV9u66RyuhrBS2SFY+Vm/OZntX8H0UQznRGZ+wYLqSaN8+LtRKWDENoZNsmvr1AyrjRvdM20vZo2sIrcWmIU9g9ukjwRqRMMaTxd73D5AAdx3n/ZxbTbADEyYtEhMbRcfpF77TZiDUWG67ttw4uqkvGAiygZBHAHUrC7ETl+5lujqEBWQ+G7mh8zW2MYDDk0eQ/s0XESeRe57E52ZFWwfOeEG6vRGHSnpy5B2owd46IkA+rgEuKUskFRQI9aEw=="}

 

四、示例demo下载

java版demo
download:alipay.fund.auth.order.app.freeze-JAVA-UTF-8.zip

PHP版本demo:
download:alipay.fund.auth.order.app.freeze.zip

 

五、常见问题和报错解析

  1.Q:线上资金授权冻结接口报错系统繁忙、系统异常

   A:这个报错一般是参数或是秘钥问题,详细的排查方案请参考:https://openclub.alipay.com/read.php?tid=11411&fid=60

 

  2、Q:为什么无法使用花呗进行付款冻结?

A:使用花呗需要商户签约有花呗支付的渠道且拥有花呗准入的(可联系商户客服确认是否满足这些条件:联系商户客服电话:0571-88158090,或在线商户客服:https://cschannel.alipay.com/newPortal.htm?scene=mysjbzzx

 

3、Q:为什么有的用户无法使用芝麻免押?

A:商家必须要有芝麻免押渠道,首先必须芝麻分不低于配置的限定分值才能免押,其次如果该用户之前有逾期违规行为导致列入芝麻风控,行业黑名单,芝麻分足够也有可能无法免押的,是否准入根据芝麻平台风控判断为准,(如果无法使用免押可以咨询芝麻客服进行确认:客服电话:0571-88158055 转 2   在线咨询:

  4、Q:如何使用银行卡进行付款冻结?

 A:如果不限制渠道支付的话不传入enable_pay_channels参数,这样就可以选择使用银行卡进行付款冻结

 

  5、Q:关于冻结接口生成订单的机制

   A: 创建支付宝订单的机制是唤起支付宝收银台,比如A用户的支付宝钱包唤起了收银台,这时候B用户在使用A用户的URL去唤起收银台会报系统异常,或者参数无效。在唤起的时候就会根据支付宝客户端进行绑定订单

 

 6、Q:关于冻结接口超时参数的问题pay_timeout

   A:这个参数是控制订单超时的时间,不传默认是15M,这个不传指的是整个参数不传,而不是传入空值,超过15M后会再次进行付款冻结会出现两个报错,一个是系统异常,一个是订单参数异常

 

 7、Q:关于调用冻结接口未安装支付宝钱包的问题导致的报错

 A:这个是基于在支付宝客户端进行的,如果未安装支付宝钱包H5支付冻结是走不通的(不像日常使用的支付接口在未检测到支付宝客户端会转入到H5页面进行登入),会提示用户去安装支付宝钱包

 

 8、Q:行业场景的免押金额场景码的免押金额上限
 A:每个行业场景的免押金额是不一样的如果需要了解金额可以实际通过入参进行测试
 
 
 9、Q:关于无法使用支付宝信用免押渠道的问题
A:在排除掉签约已经具备芝麻免押渠道情况下。1,签约账号绑定的邮箱里面查找下是否有收到一封芝麻的邮件,回复邮件申请开通渠道,2,需要登入芝麻平台创建好服务,配置好门槛然后绑定开放平台调用的APPID,关于芝麻平台的创建服务以及配置服务可以往下翻有截图流程参考
 
 10Q:关于开放平台下载的java版SDK1.5无法使用model.setExtraParam入参方法
 A:建议使用旁边的Maven项目依赖里面的最新SDK

11. Q:支付宝预授权冻结接口报暂无可用付款渠道

A:这个是付款渠道的问题,解决思路如果单独使用的是芝麻免押渠道,这个参数enablePayChannels可以先注释掉,看能否唤起冻结成功,如果可以的话,那就是两个原因,一个是商家不具备芝麻免押渠道,另一个就是买家不具备芝麻免押,这时候商家需要先检查是否有回复过邮件申请开通过芝麻信用渠道,如果没有则回复申请开通(主动申请开通【信用授权】,可主动发送至zmxyservice@zmxy.com.cn申请开通【信用授权】 邮件内容包含商户名称,PID,商户的行业  产品名),如果是买家账号问题,则看第三条QA里面的内容

 

我的回复

返回顶部