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.*;
/**
*
* 服务实现类
*
*
* @author wuhs
* @since 2022-07-12
*/
@Service
@Slf4j
public class AirwallexServiceImpl implements AirwallexService {
// @Override
// public String getAccessToken() {
//// HttpResponse 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 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 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 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 headers = new ArrayList<>();
// new BasicHeader("Authorization", AirwallexConstant.BEARER + this.getAccessToken());
BasicHeader header1 = new BasicHeader("Authorization",AirwallexConstant.BEARER + accessToken);
headers.add(header1);
// body参数
Map 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 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 headers = new ArrayList<>();
BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
headers.add(header);
// 请求体
// Map 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 headers = new ArrayList<>();
BasicHeader header1 = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
headers.add(header1);
// body参数
Map 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 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 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 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 paymentMethods) {
String accessToken = AccessTokenCommon.ACCESS_TOKEN;
if(accessToken == null || "".equals(accessToken)) {
accessToken = AccessTokenThreadUtil.getAccessToken();
}
String url = AirwallexConstant.url + "/api/v1/beneficiaries/create/";
// 请求头
List headers = new ArrayList<>();
BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
headers.add(header);
// 请求体
Map 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 headers = new ArrayList<>();
BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
headers.add(header);
// 请求体
Map 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 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 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 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 headers = new ArrayList<>();
BasicHeader header = new BasicHeader("Authorization", AirwallexConstant.BEARER + accessToken);
headers.add(header);
// 请求体
// Map 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
}