利用java實現提現金額到支付寶賬戶的功能

xiaoyou202201發表於2020-12-29


實現步驟具體如下:

(學習視訊分享:java教學視訊)

1、匯入依賴

1

2

3

4

5



com.alipay.sdk

alipay-sdk-java

4.9.5.ALL



2、配置引數



3、實現方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

@ApiOperation(value = "企業轉賬到支付寶", httpMethod = "POST", produces = "application/json;charset=UTF-8")

@ApiImplicitParams(value = {@ApiImplicitParam(value = "*使用者token", name = "token",defaultValue ="", dataType = "String",paramType="header"),

@ApiImplicitParam(value = "支付寶會員id", name = "aliuserId",defaultValue ="", dataType = "int",paramType="query",example = "0"),

@ApiImplicitParam(value = "金額", name = "money",defaultValue ="", dataType = "String",paramType="query")

})

@PostMapping("/alipay/transfer")

public Result getMoney(HttpServletRequest servletRequest,BigDecimal money,@NotNull(message = "支付寶會員id不能為空")String aliuserId){

try {

String out_biz_no = "R-" + System.currentTimeMillis() + ((long) ((Math.random() * 9 + 1) * 100000000L) + "").substring(0, 8);

//構造client

CertAlipayRequest certAlipayRequest = new CertAlipayRequest();

//設定閘道器地址https://openapi.alipay.com/gateway.do

certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");

//設定應用AppId

certAlipayRequest.setAppId(appid);

//設定應用私鑰

certAlipayRequest.setPrivateKey(zzpri);

certAlipayRequest.setFormat("json");

//設定字符集

certAlipayRequest.setCharset("UTF-8");

//設定簽名型別

certAlipayRequest.setSignType("RSA2");

//設定應用公鑰證照路徑

certAlipayRequest.setCertPath(appcertpath);

//設定支付寶公鑰證照路徑

certAlipayRequest.setAlipayPublicCertPath(alicertpath);

//設定支付寶根證照路徑

certAlipayRequest.setRootCertPath(rootcertpath);

//構造Client

AlipayClient alipayClient = null;

try {

alipayClient = new DefaultAlipayClient(certAlipayRequest);

} catch (AlipayApiException e) {

e.printStackTrace();

}

//例項化介面

AlipayFundTransUniTransferRequest request=new AlipayFundTransUniTransferRequest();

request.setBizContent("{" +

"\"out_biz_no\":\""+out_biz_no+"\"," +

"\"trans_amount\":\""+money+"\"," +

"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," +

"\"biz_scene\":\"DIRECT_TRANSFER\"," +

"\"order_title\":\"轉賬\"," +

"\"payee_info\":{" +

"\"identity\":\""+aliuserId+"\"," +

"\"identity_type\":\"ALIPAY_USER_ID\"," +

" }," +

"\"remark\":\"單筆轉賬\"" +

" }");

AlipayFundTransUniTransferResponse response=null;

response = alipayClient.certificateExecute(request);

if (response.isSuccess()){

if("10000".equals(response.getCode())){

return Result.ok("轉賬成功");

} else {

return Result.fail(Integer.valueOf(response.getCode()),response.getSubMsg());

}

}else {

return Result.fail(Integer.valueOf(response.getCode()),response.getSubMsg());

}

}catch (Exception e){

e.printStackTrace();

return Result.error(901,"支付寶轉賬失敗!");

}

}

注意:該功能實現需要先實現支付寶授權功能獲取支付寶會員id,支付寶會員id是由2088開頭的,如果是用客戶手機號進行提現,就會還需要輸入客戶真實姓名。

用客戶手機號提現只是部分引數不同,程式碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

request.setBizContent("{" +

"\"out_biz_no\":\""+out_biz_no+"\"," +

"\"trans_amount\":\""+money+"\"," +

"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," +

"\"biz_scene\":\"DIRECT_TRANSFER\"," +

"\"order_title\":\"轉賬\"," +

"\"payee_info\":{" +

"\"identity\":\""+aliuserId+"\"," +

"\"identity_type\":\"ALIPAY_USER_ID\"," +

"\"name\":\"ALIPAY_USER_ID\"" +

" }," +

"\"remark\":\"姓名\"" +

" }");

相關文章