123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- package com.szwl.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.szwl.common.AccessTokenCommon;
- import com.szwl.constant.AirwallexConstant;
- import com.szwl.controller.TAreaController;
- import com.szwl.model.bo.R;
- import com.szwl.model.dto.BeneficiaryDTO;
- import com.szwl.model.dto.CaPaymentRequestDTO;
- import com.szwl.model.utils.AccessTokenThreadUtil;
- import com.szwl.model.utils.HttpClientSslUtils;
- import com.szwl.service.AirwallexService;
- import com.szwl.service.TCoinOrderService;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.http.entity.ContentType;
- import org.apache.http.message.BasicHeader;
- import org.springframework.context.annotation.ImportResource;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.util.*;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author wuhs
- * @since 2022-07-12
- */
- @Service
- @Slf4j
- public class AirwallexServiceImpl implements AirwallexService {
- // @Override
- // public String getAccessToken() {
- //// HttpResponse<String> response = Unirest.post("https://api-demo.airwallex.com/api/v1/authentication/login")
- //// .header("Content-Type", "application/json")
- //// .header("x-client-id", "W_ORsgAFTiuA9k2KuqZt8A")
- //// .header("x-api-key", "8ac97c856c6d6cae7eb8fd05511f7a165be798d032381cb8026de7b4aa9aaee2e6312a8888a3474d783a40913ab6b55d")
- //// .body("{
- //// }").asString();
- // String url = AirwallexConstant.url+"/api/v1/authentication/login";
- // List<BasicHeader> headers = new ArrayList<>();
- // BasicHeader header1 = new BasicHeader("x-client-id",AirwallexConstant.clientid);
- // BasicHeader header2 = new BasicHeader("x-api-key",AirwallexConstant.apiKey);
- // headers.add(header1);
- // headers.add(header2);
- // Map<String,String> map = new HashMap<>();
- // String data = JSON.toJSONString(map);
- // String response=null;
- // try {
- // response = HttpClientSslUtils.doPost(url, data,ContentType.APPLICATION_JSON,headers);
- // JSONObject resultJson = JSONObject.parseObject(response);
- // String token = resultJson.getString("token");
- // if(StringUtils.isNotEmpty(token)){
- // System.out.println("token="+token);
- // return token;
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // return response;
- // }
- /**
- * 创建支付意向
- * @param amount
- * @param currency
- * @param requestId
- * @param merchantOrderId
- * @return
- */
- @Override
- public Map<String, Object> caPaymentIntent(BigDecimal amount, String currency, String requestId, String merchantOrderId) {
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- // 调用付款意向 api,返回payment intent id和client secret
- // 请求头
- String url = AirwallexConstant.url+"/api/v1/pa/payment_intents/create";
- List<BasicHeader> headers = new ArrayList<>();
- // new BasicHeader("Authorization", AirwallexConstant.BEARER + this.getAccessToken());
- BasicHeader header1 = new BasicHeader("Authorization",AirwallexConstant.BEARER + accessToken);
- headers.add(header1);
- // body参数
- Map<String, Object> bodyMap = new HashMap<>();
- bodyMap.put("amount", amount);
- bodyMap.put("currency", currency);
- bodyMap.put("request_id", requestId);
- bodyMap.put("merchant_order_id", merchantOrderId);
- String data = JSON.toJSONString(bodyMap);
- log.info("请求body参数:" +data);
- Map<String, Object> responseMap = new HashMap<>();
- String resp = null;
- try {
- resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
- responseMap = JSON.parseObject(resp);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return responseMap;
- }
- /**
- * 获取某笔支付订单的信息
- * @param id
- * @return
- */
- @Override
- public String retrieveAPaymentIntent(String id) {
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- String url = AirwallexConstant.url + "/api/v1/pa/payment_intents/" + id;
- // 请求头
- List<BasicHeader> headers = new ArrayList<>();
- BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
- headers.add(header);
- // 请求体
- // Map<String, Object> bodyMap = new HashMap<>();
- // String data = JSON.toJSONString(bodyMap);
- String resp = null;
- try {
- resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return resp;
- }
- /**
- * 创建一笔退款
- * @param requestId
- * @param paymentIntentId
- * @return
- */
- @Override
- public String createARefund(String requestId, String paymentIntentId) {
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- //
- // 请求头
- String url = AirwallexConstant.url + "/api/v1/pa/refunds/create";
- List<BasicHeader> headers = new ArrayList<>();
- BasicHeader header1 = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
- headers.add(header1);
- // body参数
- Map<String, Object> bodyMap = new HashMap<>();
- bodyMap.put("request_id", requestId);
- bodyMap.put("payment_intent_id", paymentIntentId);
- // bodyMap.put("amount", amount); // 退款金额
- // reason = "退款原因-测试";
- // bodyMap.put("reason", reason);
- String data = JSON.toJSONString(bodyMap);
- String resp = null;
- try {
- resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return resp;
- }
- /**
- * 获取某笔退款的详情
- *
- * @param refundId
- * @return
- */
- @Override
- public String retrieveARefund(String refundId) {
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- String url = AirwallexConstant.url + "/api/v1/pa/refunds/" + refundId;
- // 请求头
- List<BasicHeader> headers = new ArrayList<>();
- BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
- headers.add(header);
- String resp = null;
- try {
- resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return resp;
- }
- /**
- * 获取 authorization_code
- * @param codeChallenge
- * @param scope
- * @return
- */
- @Override
- public String getAuthCode(String codeChallenge, String[] scope) {
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- String url = AirwallexConstant.url + "/api/v1/authentication/authorize";
- // 请求头
- List<BasicHeader> headers = new ArrayList<>();
- BasicHeader header1 = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
- // BasicHeader header2 = new BasicHeader("x-on-behalf-of", "string");
- headers.add(header1);
- // headers.add(header2);
- // 请求体
- Map<String, Object> bodyMap = new HashMap<>();
- bodyMap.put("code_challenge", codeChallenge);
- bodyMap.put("scope", scope);
- String data = JSON.toJSONString(bodyMap);
- String resp = null;
- try {
- resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return resp;
- }
- /**
- * 创建收款人
- * @param beneficiary
- * @param nickname
- * @param paymentMethods
- * @return
- */
- @Override
- public String caBeneficiary(BeneficiaryDTO beneficiary, String nickname, List<String> paymentMethods) {
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- String url = AirwallexConstant.url + "/api/v1/beneficiaries/create/";
- // 请求头
- List<BasicHeader> headers = new ArrayList<>();
- BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
- headers.add(header);
- // 请求体
- Map<String, Object> bodyMap = new HashMap<>();
- bodyMap.put("beneficiary", beneficiary);
- bodyMap.put("nickname", nickname);
- bodyMap.put("payment_methods", paymentMethods);
- String data = JSON.toJSONString(bodyMap);
- String resp = null;
- try {
- resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return resp;
- }
- /**
- * 创建一笔付款
- * @param caPaymentRequestDTO
- * @return
- */
- @Override
- public String caPayment(CaPaymentRequestDTO caPaymentRequestDTO) {
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- String url = AirwallexConstant.url + "/api/v1/payments/create/";
- // 请求头
- List<BasicHeader> headers = new ArrayList<>();
- BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
- headers.add(header);
- // 请求体
- Map<String, Object> bodyMap = new HashMap<>();
- String beneficiaryId = caPaymentRequestDTO.getBeneficiaryId();
- double paymentAmount = caPaymentRequestDTO.getPaymentAmount();
- String paymentCurrency = caPaymentRequestDTO.getPaymentCurrency();
- String paymentMethod = caPaymentRequestDTO.getPaymentMethod();
- String reason = caPaymentRequestDTO.getReason();
- String reference = caPaymentRequestDTO.getReference();
- String requestId;
- requestId = UUID.randomUUID().toString().trim();
- // String sourceAmount = caPaymentRequestDTO.getSourceAmount();
- String sourceCurrency = caPaymentRequestDTO.getSourceCurrency();
- String swiftChargeOption = caPaymentRequestDTO.getSwiftChargeOption();
- bodyMap.put("beneficiary_id", beneficiaryId);
- bodyMap.put("payment_amount", paymentAmount);
- bodyMap.put("payment_currency", paymentCurrency);
- bodyMap.put("payment_method", paymentMethod);
- bodyMap.put("reason", reason);
- bodyMap.put("reference", reference);
- bodyMap.put("request_id", requestId);
- // bodyMap.put("source_amount", sourceAmount);
- bodyMap.put("source_currency", sourceCurrency);
- bodyMap.put("swift_charge_option", swiftChargeOption);
- String data = JSON.toJSONString(bodyMap);
- String resp = null;
- try {
- resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return resp;
- }
- /**
- * 查询申泽钱包余额
- * @return
- */
- @Override
- public String getCurrentBalances() {
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- String url = AirwallexConstant.url + "/api/v1/balances/current";
- // 请求头
- List<BasicHeader> headers = new ArrayList<>();
- BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
- headers.add(header);
- String resp = null;
- try {
- resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return resp;
- }
- // @Override
- // public String refundWebhooks(HttpServletRequest request, HttpServletResponse response) {
- // return null;
- // }
- /**
- * 根据国家/付款方式查询表单字段
- *
- * @return
- */
- @Override
- public R getFormSchema() {
- // POST /api/v1/beneficiary_form_schemas/generate 根据国家/付款方式查询表单字段,返回前端所需表单信息(对接付款国较多)
- // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiary_form_schemas_generate/post
- // HttpResponse<String> response = Unirest.post("https://api-demo.airwallex.com/api/v1/beneficiary_form_schemas/generate")
- // .header("Content-Type", "application/json")
- // .header("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0b20iLCJyb2xlcyI6WyJ1c2VyIl0sImlhdCI6MTQ4ODQxNTI1NywiZXhwIjoxNDg4NDE1MjY3fQ.UHqau03y5kEk5lFbTp7J4a-U6LXsfxIVNEsux85hj-Q")
- // .header("headers", "[object Object]")
- // .body("{
- // \"account_currency\": \"USD\",
- // \"bank_country_code\": \"US\",
- // \"entity_type\": \"PERSONAL\",
- // \"local_clearing_system\": \"ACH\",
- // \"payment_method\": \"LOCAL\"
- // }")
- // .asString();
- return null;
- }
- @Override
- public R getAPISchema() {
- // POST /api/v1/beneficiary_api_schemas/generate 动态获取指定国家、支付方式,返回创建收款人请求所需字段
- // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiary_api_schemas_generate/post
- return null;
- }
- /**
- * 创建收款人,返回收款人ID
- * @param requestBody
- * @return
- */
- @Override
- public String createBeneficiary (Map<String, Object> requestBody) {
- // POST /api/v1/beneficiaries/create 创建收款人,返回收款人ID
- // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiaries_create/post
- Map beneficiary = new HashMap();
- Object address = null;
- ArrayList paymentMethods = new ArrayList<>();
- String beneficiaryId = "fsdfsd";
- return beneficiaryId;
- }
- @Override
- public String createPayout() {
- // POST /api/v1/payments/create 创建付款交易,返回创建结果paymentID
- // https://www.airwallex.com/docs/api#/Payouts/Payments/_api_v1_payments_create/post
- String paymentId = "111";
- return paymentId;
- }
- @Override
- public String getPaymentById(String paymentId) {
- // GET /api/v1/payments/{payment_id} 查询支付结果,返回支付结果状态
- // https://www.airwallex.com/docs/api#/Payouts/Payments/_api_v1_payments__payment_id_/get
- String accessToken = AccessTokenCommon.ACCESS_TOKEN;
- if(accessToken == null || "".equals(accessToken)) {
- accessToken = AccessTokenThreadUtil.getAccessToken();
- }
- String url = AirwallexConstant.url + "/api/v1/payments/" + paymentId;
- // 请求头
- List<BasicHeader> headers = new ArrayList<>();
- BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
- headers.add(header);
- // 请求体
- // Map<String, Object> bodyMap = new HashMap<>();
- // String data = JSON.toJSONString(bodyMap);
- String resp = null;
- try {
- resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return resp;
- }
- // Cancel、Retry
- }
|