AirwallexServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. package com.szwl.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.szwl.common.AccessTokenCommon;
  4. import com.szwl.constant.AirwallexConstant;
  5. import com.szwl.controller.TAreaController;
  6. import com.szwl.model.bo.R;
  7. import com.szwl.model.dto.BeneficiaryDTO;
  8. import com.szwl.model.dto.CaPaymentRequestDTO;
  9. import com.szwl.model.utils.AccessTokenThreadUtil;
  10. import com.szwl.model.utils.HttpClientSslUtils;
  11. import com.szwl.service.AirwallexService;
  12. import com.szwl.service.TCoinOrderService;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.http.entity.ContentType;
  15. import org.apache.http.message.BasicHeader;
  16. import org.springframework.context.annotation.ImportResource;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.math.BigDecimal;
  20. import java.util.*;
  21. /**
  22. * <p>
  23. * 服务实现类
  24. * </p>
  25. *
  26. * @author wuhs
  27. * @since 2022-07-12
  28. */
  29. @Service
  30. @Slf4j
  31. public class AirwallexServiceImpl implements AirwallexService {
  32. // @Override
  33. // public String getAccessToken() {
  34. //// HttpResponse<String> response = Unirest.post("https://api-demo.airwallex.com/api/v1/authentication/login")
  35. //// .header("Content-Type", "application/json")
  36. //// .header("x-client-id", "W_ORsgAFTiuA9k2KuqZt8A")
  37. //// .header("x-api-key", "8ac97c856c6d6cae7eb8fd05511f7a165be798d032381cb8026de7b4aa9aaee2e6312a8888a3474d783a40913ab6b55d")
  38. //// .body("{
  39. //// }").asString();
  40. // String url = AirwallexConstant.url+"/api/v1/authentication/login";
  41. // List<BasicHeader> headers = new ArrayList<>();
  42. // BasicHeader header1 = new BasicHeader("x-client-id",AirwallexConstant.clientid);
  43. // BasicHeader header2 = new BasicHeader("x-api-key",AirwallexConstant.apiKey);
  44. // headers.add(header1);
  45. // headers.add(header2);
  46. // Map<String,String> map = new HashMap<>();
  47. // String data = JSON.toJSONString(map);
  48. // String response=null;
  49. // try {
  50. // response = HttpClientSslUtils.doPost(url, data,ContentType.APPLICATION_JSON,headers);
  51. // JSONObject resultJson = JSONObject.parseObject(response);
  52. // String token = resultJson.getString("token");
  53. // if(StringUtils.isNotEmpty(token)){
  54. // System.out.println("token="+token);
  55. // return token;
  56. // }
  57. // } catch (Exception e) {
  58. // e.printStackTrace();
  59. // }
  60. // return response;
  61. // }
  62. /**
  63. * 创建支付意向
  64. * @param amount
  65. * @param currency
  66. * @param requestId
  67. * @param merchantOrderId
  68. * @return
  69. */
  70. @Override
  71. public Map<String, Object> caPaymentIntent(BigDecimal amount, String currency, String requestId, String merchantOrderId) {
  72. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  73. if(accessToken == null || "".equals(accessToken)) {
  74. accessToken = AccessTokenThreadUtil.getAccessToken();
  75. }
  76. // 调用付款意向 api,返回payment intent id和client secret
  77. // 请求头
  78. String url = AirwallexConstant.url+"/api/v1/pa/payment_intents/create";
  79. List<BasicHeader> headers = new ArrayList<>();
  80. // new BasicHeader("Authorization", AirwallexConstant.BEARER + this.getAccessToken());
  81. BasicHeader header1 = new BasicHeader("Authorization",AirwallexConstant.BEARER + accessToken);
  82. headers.add(header1);
  83. // body参数
  84. Map<String, Object> bodyMap = new HashMap<>();
  85. bodyMap.put("amount", amount);
  86. bodyMap.put("currency", currency);
  87. bodyMap.put("request_id", requestId);
  88. bodyMap.put("merchant_order_id", merchantOrderId);
  89. String data = JSON.toJSONString(bodyMap);
  90. log.info("请求body参数:" +data);
  91. Map<String, Object> responseMap = new HashMap<>();
  92. String resp = null;
  93. try {
  94. resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
  95. responseMap = JSON.parseObject(resp);
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. return responseMap;
  100. }
  101. /**
  102. * 获取某笔支付订单的信息
  103. * @param id
  104. * @return
  105. */
  106. @Override
  107. public String retrieveAPaymentIntent(String id) {
  108. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  109. if(accessToken == null || "".equals(accessToken)) {
  110. accessToken = AccessTokenThreadUtil.getAccessToken();
  111. }
  112. String url = AirwallexConstant.url + "/api/v1/pa/payment_intents/" + id;
  113. // 请求头
  114. List<BasicHeader> headers = new ArrayList<>();
  115. BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
  116. headers.add(header);
  117. // 请求体
  118. // Map<String, Object> bodyMap = new HashMap<>();
  119. // String data = JSON.toJSONString(bodyMap);
  120. String resp = null;
  121. try {
  122. resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
  123. } catch (Exception e) {
  124. throw new RuntimeException(e);
  125. }
  126. return resp;
  127. }
  128. /**
  129. * 创建一笔退款
  130. * @param requestId
  131. * @param paymentIntentId
  132. * @return
  133. */
  134. @Override
  135. public String createARefund(String requestId, String paymentIntentId) {
  136. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  137. if(accessToken == null || "".equals(accessToken)) {
  138. accessToken = AccessTokenThreadUtil.getAccessToken();
  139. }
  140. //
  141. // 请求头
  142. String url = AirwallexConstant.url + "/api/v1/pa/refunds/create";
  143. List<BasicHeader> headers = new ArrayList<>();
  144. BasicHeader header1 = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
  145. headers.add(header1);
  146. // body参数
  147. Map<String, Object> bodyMap = new HashMap<>();
  148. bodyMap.put("request_id", requestId);
  149. bodyMap.put("payment_intent_id", paymentIntentId);
  150. // bodyMap.put("amount", amount); // 退款金额
  151. // reason = "退款原因-测试";
  152. // bodyMap.put("reason", reason);
  153. String data = JSON.toJSONString(bodyMap);
  154. String resp = null;
  155. try {
  156. resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
  157. } catch (Exception e) {
  158. e.printStackTrace();
  159. }
  160. return resp;
  161. }
  162. /**
  163. * 获取某笔退款的详情
  164. *
  165. * @param refundId
  166. * @return
  167. */
  168. @Override
  169. public String retrieveARefund(String refundId) {
  170. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  171. if(accessToken == null || "".equals(accessToken)) {
  172. accessToken = AccessTokenThreadUtil.getAccessToken();
  173. }
  174. String url = AirwallexConstant.url + "/api/v1/pa/refunds/" + refundId;
  175. // 请求头
  176. List<BasicHeader> headers = new ArrayList<>();
  177. BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
  178. headers.add(header);
  179. String resp = null;
  180. try {
  181. resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
  182. } catch (Exception e) {
  183. throw new RuntimeException(e);
  184. }
  185. return resp;
  186. }
  187. /**
  188. * 获取 authorization_code
  189. * @param codeChallenge
  190. * @param scope
  191. * @return
  192. */
  193. @Override
  194. public String getAuthCode(String codeChallenge, String[] scope) {
  195. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  196. if(accessToken == null || "".equals(accessToken)) {
  197. accessToken = AccessTokenThreadUtil.getAccessToken();
  198. }
  199. String url = AirwallexConstant.url + "/api/v1/authentication/authorize";
  200. // 请求头
  201. List<BasicHeader> headers = new ArrayList<>();
  202. BasicHeader header1 = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
  203. // BasicHeader header2 = new BasicHeader("x-on-behalf-of", "string");
  204. headers.add(header1);
  205. // headers.add(header2);
  206. // 请求体
  207. Map<String, Object> bodyMap = new HashMap<>();
  208. bodyMap.put("code_challenge", codeChallenge);
  209. bodyMap.put("scope", scope);
  210. String data = JSON.toJSONString(bodyMap);
  211. String resp = null;
  212. try {
  213. resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. }
  217. return resp;
  218. }
  219. /**
  220. * 创建收款人
  221. * @param beneficiary
  222. * @param nickname
  223. * @param paymentMethods
  224. * @return
  225. */
  226. @Override
  227. public String caBeneficiary(BeneficiaryDTO beneficiary, String nickname, List<String> paymentMethods) {
  228. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  229. if(accessToken == null || "".equals(accessToken)) {
  230. accessToken = AccessTokenThreadUtil.getAccessToken();
  231. }
  232. String url = AirwallexConstant.url + "/api/v1/beneficiaries/create/";
  233. // 请求头
  234. List<BasicHeader> headers = new ArrayList<>();
  235. BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
  236. headers.add(header);
  237. // 请求体
  238. Map<String, Object> bodyMap = new HashMap<>();
  239. bodyMap.put("beneficiary", beneficiary);
  240. bodyMap.put("nickname", nickname);
  241. bodyMap.put("payment_methods", paymentMethods);
  242. String data = JSON.toJSONString(bodyMap);
  243. String resp = null;
  244. try {
  245. resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
  246. } catch (Exception e) {
  247. e.printStackTrace();
  248. }
  249. return resp;
  250. }
  251. /**
  252. * 创建一笔付款
  253. * @param caPaymentRequestDTO
  254. * @return
  255. */
  256. @Override
  257. public String caPayment(CaPaymentRequestDTO caPaymentRequestDTO) {
  258. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  259. if(accessToken == null || "".equals(accessToken)) {
  260. accessToken = AccessTokenThreadUtil.getAccessToken();
  261. }
  262. String url = AirwallexConstant.url + "/api/v1/payments/create/";
  263. // 请求头
  264. List<BasicHeader> headers = new ArrayList<>();
  265. BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
  266. headers.add(header);
  267. // 请求体
  268. Map<String, Object> bodyMap = new HashMap<>();
  269. String beneficiaryId = caPaymentRequestDTO.getBeneficiaryId();
  270. double paymentAmount = caPaymentRequestDTO.getPaymentAmount();
  271. String paymentCurrency = caPaymentRequestDTO.getPaymentCurrency();
  272. String paymentMethod = caPaymentRequestDTO.getPaymentMethod();
  273. String reason = caPaymentRequestDTO.getReason();
  274. String reference = caPaymentRequestDTO.getReference();
  275. String requestId;
  276. requestId = UUID.randomUUID().toString().trim();
  277. // String sourceAmount = caPaymentRequestDTO.getSourceAmount();
  278. String sourceCurrency = caPaymentRequestDTO.getSourceCurrency();
  279. String swiftChargeOption = caPaymentRequestDTO.getSwiftChargeOption();
  280. bodyMap.put("beneficiary_id", beneficiaryId);
  281. bodyMap.put("payment_amount", paymentAmount);
  282. bodyMap.put("payment_currency", paymentCurrency);
  283. bodyMap.put("payment_method", paymentMethod);
  284. bodyMap.put("reason", reason);
  285. bodyMap.put("reference", reference);
  286. bodyMap.put("request_id", requestId);
  287. // bodyMap.put("source_amount", sourceAmount);
  288. bodyMap.put("source_currency", sourceCurrency);
  289. bodyMap.put("swift_charge_option", swiftChargeOption);
  290. String data = JSON.toJSONString(bodyMap);
  291. String resp = null;
  292. try {
  293. resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
  294. } catch (Exception e) {
  295. e.printStackTrace();
  296. }
  297. return resp;
  298. }
  299. /**
  300. * 查询申泽钱包余额
  301. * @return
  302. */
  303. @Override
  304. public String getCurrentBalances() {
  305. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  306. if(accessToken == null || "".equals(accessToken)) {
  307. accessToken = AccessTokenThreadUtil.getAccessToken();
  308. }
  309. String url = AirwallexConstant.url + "/api/v1/balances/current";
  310. // 请求头
  311. List<BasicHeader> headers = new ArrayList<>();
  312. BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
  313. headers.add(header);
  314. String resp = null;
  315. try {
  316. resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
  317. } catch (Exception e) {
  318. throw new RuntimeException(e);
  319. }
  320. return resp;
  321. }
  322. // @Override
  323. // public String refundWebhooks(HttpServletRequest request, HttpServletResponse response) {
  324. // return null;
  325. // }
  326. /**
  327. * 根据国家/付款方式查询表单字段
  328. *
  329. * @return
  330. */
  331. @Override
  332. public R getFormSchema() {
  333. // POST /api/v1/beneficiary_form_schemas/generate 根据国家/付款方式查询表单字段,返回前端所需表单信息(对接付款国较多)
  334. // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiary_form_schemas_generate/post
  335. // HttpResponse<String> response = Unirest.post("https://api-demo.airwallex.com/api/v1/beneficiary_form_schemas/generate")
  336. // .header("Content-Type", "application/json")
  337. // .header("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0b20iLCJyb2xlcyI6WyJ1c2VyIl0sImlhdCI6MTQ4ODQxNTI1NywiZXhwIjoxNDg4NDE1MjY3fQ.UHqau03y5kEk5lFbTp7J4a-U6LXsfxIVNEsux85hj-Q")
  338. // .header("headers", "[object Object]")
  339. // .body("{
  340. // \"account_currency\": \"USD\",
  341. // \"bank_country_code\": \"US\",
  342. // \"entity_type\": \"PERSONAL\",
  343. // \"local_clearing_system\": \"ACH\",
  344. // \"payment_method\": \"LOCAL\"
  345. // }")
  346. // .asString();
  347. return null;
  348. }
  349. @Override
  350. public R getAPISchema() {
  351. // POST /api/v1/beneficiary_api_schemas/generate 动态获取指定国家、支付方式,返回创建收款人请求所需字段
  352. // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiary_api_schemas_generate/post
  353. return null;
  354. }
  355. /**
  356. * 创建收款人,返回收款人ID
  357. * @param requestBody
  358. * @return
  359. */
  360. @Override
  361. public String createBeneficiary (Map<String, Object> requestBody) {
  362. // POST /api/v1/beneficiaries/create 创建收款人,返回收款人ID
  363. // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiaries_create/post
  364. Map beneficiary = new HashMap();
  365. Object address = null;
  366. ArrayList paymentMethods = new ArrayList<>();
  367. String beneficiaryId = "fsdfsd";
  368. return beneficiaryId;
  369. }
  370. @Override
  371. public String createPayout() {
  372. // POST /api/v1/payments/create 创建付款交易,返回创建结果paymentID
  373. // https://www.airwallex.com/docs/api#/Payouts/Payments/_api_v1_payments_create/post
  374. String paymentId = "111";
  375. return paymentId;
  376. }
  377. @Override
  378. public String getPaymentById(String paymentId) {
  379. // GET /api/v1/payments/{payment_id} 查询支付结果,返回支付结果状态
  380. // https://www.airwallex.com/docs/api#/Payouts/Payments/_api_v1_payments__payment_id_/get
  381. String accessToken = AccessTokenCommon.ACCESS_TOKEN;
  382. if(accessToken == null || "".equals(accessToken)) {
  383. accessToken = AccessTokenThreadUtil.getAccessToken();
  384. }
  385. String url = AirwallexConstant.url + "/api/v1/payments/" + paymentId;
  386. // 请求头
  387. List<BasicHeader> headers = new ArrayList<>();
  388. BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
  389. headers.add(header);
  390. // 请求体
  391. // Map<String, Object> bodyMap = new HashMap<>();
  392. // String data = JSON.toJSONString(bodyMap);
  393. String resp = null;
  394. try {
  395. resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
  396. } catch (Exception e) {
  397. throw new RuntimeException(e);
  398. }
  399. return resp;
  400. }
  401. // Cancel、Retry
  402. }