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

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

支付宝预授权接入流程以及注意事项以及常见报错请参考:https://openclub.alipay.com/read.php?tid=12100&fid=56

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

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.调用接口成功返回请求字符串,需要把这个字符串发送给客户端在客户端唤起支付,详细的请求字符串示例:

alipay_sdk=alipay-sdk-java-3.3.49.ALL&app_id=201410090001****&biz_content={"amount":"0.03","extra_param":"{\"category\":\"CHARGE_PILE_CAR\"}","order_title":"大家的预授权","out_order_no":"202807173313122x1z112318123","out_request_no":"2018111212S2sz21221282314","pay_timeout":"2d","payee_user_id":"2088501624***","product_code":"PRE_AUTH_ONLINE"}&charset=utf-8&format=json&method=alipay.fund.auth.order.app.freeze&notify_url=http://notify.dengw.online***&sign=MR9EA5ap1hbtxkj9U3TKcCP6REdCfQeauhx4Xmz7veVlL3ldbeTet1OI3tmbfAXx/Q27uaUc/89aVZtf/kYMNgqLCVOihP1pQ/exMU3yHbPz2Fw8plgBHB30aQWCyG1COkVIgne/DbAGdEcGEK32PZilOSe0vTt95v/o3duod2IEm0D+ltZ75SQACWf8jpRNHadA0dHrqlL/rl2zXASJl4XXlj6o+F9gpGZSOm6L23uuQrPncOVOZzZH4/HdXACZCW0xTJbjpNtbYylQS7UsorGsTVHHMlL/XQhBo7n4ND+5VusEzXjfH+8Xa/q2FratfWuQn8lbKxjwknZwKoRQ0g==&sign_type=RSA2&timestamp=2018-12-29 14:32:15&version=1.0

 

4.客户端唤起支付冻结成功 异步信息示例

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.使用芝麻免押流程如下:

a.买家唤起授权页面

b.授权成功依据买家信用信息为买家减免预存

c.唤起支付页面

注:使用芝麻免押的方式默认会使用芝麻信用免押抵扣,无法选择其他支付方式,只有在买家芝麻信用不满足才会有其他的支付方式供选择

2.不使用芝麻信用免押会直接唤起支付页面,买家可自行选择支付方式

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

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(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
			AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key,
			AlipayConfig.sign_type);

	AlipayFundAuthOrderUnfreezeRequest alipayRequest = new AlipayFundAuthOrderUnfreezeRequest();

	// 支付宝资金授权订单号,在授权冻结成功时返回需要入库保存
	String auth_no = new String(request.getParameter("WIDOUauth_no").getBytes("ISO-8859-1"), "UTF-8");
	//同一商户每次不同的资金操作请求,商户请求流水号不能重复,且与冻结流水号不同
	String out_request_no = new String(request.getParameter("WIDOUout_request_no").getBytes("ISO-8859-1"),
			"UTF-8");
	// 本次操作解冻的金额,单位为:元(人民币),精确到小数点后两位
	String amount = new String(request.getParameter("WIDOUamount").getBytes("ISO-8859-1"), "UTF-8");
	// 商户对本次解冻操作的附言描述,长度不超过100个字母或50个汉字 
	String remark = new String(request.getParameter("WIDOUremark").getBytes("ISO-8859-1"), "UTF-8");

	AlipayFundAuthOrderUnfreezeModel model = new AlipayFundAuthOrderUnfreezeModel();
	model.setAuthNo(auth_no);
	model.setOutRequestNo(out_request_no);
	model.setAmount(amount);
	model.setRemark(remark);
	//选填字段,信用授权订单,针对0元订单,传入该值完结信用订单,形成芝麻履约记录
	//model.setExtraParam("{\"unfreezeBizInfo\":\"{\\\"bizComplete\\\":\\\"true\\\"}\"}");
	alipayRequest.setBizModel(model);
	alipayRequest.setNotifyUrl(AlipayConfig.notify_url);//异步通知地址,必填,该接口只通过该参数进行异步通知

	AlipayFundAuthOrderUnfreezeResponse alipayResponse = alipayClient.execute(alipayRequest);

	//请求
	String result = alipayClient.execute(alipayRequest).getBody();

	//输出
	out.println(result);

 

四、资金授权撤销接口调用示例

示例代码:

//获得初始化的AlipayClient
	AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
			AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key,
			AlipayConfig.sign_type);

	AlipayFundAuthOperationCancelRequest alipayRequest = new AlipayFundAuthOperationCancelRequest();
	// 支付宝资金授权订单号,在授权冻结成功时返回参数中获得
	String auth_no = new String(request.getParameter("WIDOCauth_no").getBytes("ISO-8859-1"), "UTF-8");
	//商户的授权资金订单号,与支付宝的授权资金订单号不能同时为空
	String out_order_no = new String(request.getParameter("WIDOCout_order_no").getBytes("ISO-8859-1"), "UTF-8");
	//支付宝的授权资金操作流水号
	String operation_id = new String(request.getParameter("WIDOCoperation_id").getBytes("ISO-8859-1"), "UTF-8");
	//与支付宝的授权资金操作流水号不能同时为空,与冻结流水号相同
	String out_request_no = new String(request.getParameter("WIDOCout_request_no").getBytes("ISO-8859-1"),
			"UTF-8");
	// 商户对本次撤销操作的附言描述,长度不超过100个字母或50个汉字 
	String remark = new String(request.getParameter("WIDOCremark").getBytes("ISO-8859-1"), "UTF-8");

	AlipayFundAuthOperationCancelModel model = new AlipayFundAuthOperationCancelModel();
	if (null != auth_no && !"".equals(auth_no)) {
		model.setAuthNo(auth_no);
	}
	if (null != out_order_no && !"".equals(out_order_no)) {
		model.setOutOrderNo(out_order_no);
	}
	if (null != operation_id && !"".equals(operation_id)) {
		model.setOperationId(operation_id);
	}
	if (null != out_request_no && !"".equals(out_request_no)) {
		model.setOutRequestNo(out_request_no);
	}
	if (null != remark && !"".equals(remark)) {
		model.setRemark(remark);
	}

	alipayRequest.setBizModel(model);
	alipayRequest.setNotifyUrl(AlipayConfig.notify_url);//异步通知地址,必填,该接口只通过该参数进行异步通知

	AlipayFundAuthOperationCancelResponse alipayResponse = alipayClient.execute(alipayRequest);

	//请求
	String result = alipayClient.execute(alipayRequest).getBody();

	//输出
	out.println(result);

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

示例代码:

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

 

 

我的回复

返回顶部