From 7e7f901b2172281dc294dfbc67e6ad00625f09f4 Mon Sep 17 00:00:00 2001 From: puzhibing <393733352@qq.com> Date: 星期一, 04 三月 2024 10:11:21 +0800 Subject: [PATCH] 合并代码 --- cloud-server-management/src/main/java/com/dsh/course/util/midtrans/Payouts.java | 348 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 348 insertions(+), 0 deletions(-) diff --git a/cloud-server-management/src/main/java/com/dsh/course/util/midtrans/Payouts.java b/cloud-server-management/src/main/java/com/dsh/course/util/midtrans/Payouts.java new file mode 100644 index 0000000..dac5dd6 --- /dev/null +++ b/cloud-server-management/src/main/java/com/dsh/course/util/midtrans/Payouts.java @@ -0,0 +1,348 @@ +package com.dsh.course.util.midtrans; + +import com.dsh.guns.modular.system.util.ResultUtil; +import com.midtrans.Config; +import com.midtrans.Midtrans; +import com.midtrans.httpclient.IrisApi; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.*; + +/** + * 付款工具类 + * @author zhibing.pu + * @date 2023/5/4 19:22 + */ +public class Payouts { + + private static String sandbox_merchant_id = "G118246934"; + + private static String sandbox_serverKey = "IRIS-330198f0-e49d-493f-baae-585cfded355d"; + +// private static String sandbox_serverKey = "SB-Mid-server-qR4Xy4kbpyxND_7f6c1_knb9"; + + private static String sandbox_clientKey = "SB-Mid-client-xhRbIV8lsbu7bKCE"; + + private static String merchant_id = "G118246934"; + + private static String serverKey = "Mid-server-w1ATM2ldDpd0c4Q0s9sNfeNN"; + + private static String clientKey = "Mid-client-X22yMA1Wvs0v1vfO"; + + + /** + * 添加转账账户信息 + * @param beneficiary_name + * @param beneficiary_account + * @param beneficiary_bank + * @param beneficiary_email + * @return + */ + public static ResultUtil addBeneficiaries(String beneficiary_name, String alias_name, String beneficiary_account, String beneficiary_bank, String beneficiary_email){ + try { + //沙箱环境 + Midtrans.serverKey = sandbox_serverKey; + Midtrans.clientKey = sandbox_clientKey; + Midtrans.isProduction = false; + + //生产环境 +// Midtrans.serverKey = serverKey; +// Midtrans.clientKey = clientKey; +// Midtrans.isProduction = true; + Map<String, String> requestBody = new HashMap<>(); + requestBody.put("name", beneficiary_name); + requestBody.put("account", beneficiary_account); + requestBody.put("bank", beneficiary_bank); + requestBody.put("alias_name", alias_name); + requestBody.put("email", beneficiary_email); + JSONObject beneficiaries = IrisApi.createBeneficiaries(requestBody, Config.getGlobalConfig()); + String status = beneficiaries.getString("status"); + if("created".equals(status)){ + return ResultUtil.success(); + } + }catch (Exception e){ + e.printStackTrace(); + } + return ResultUtil.error("失败"); + } + + + /** + * 更新转账用户数据 + * @param beneficiary_name + * @param beneficiary_account + * @param beneficiary_bank + * @param beneficiary_email + * @return + */ + public static ResultUtil editBeneficiaries(String beneficiary_name, String alias_name, String beneficiary_account, String beneficiary_bank, String beneficiary_email){ + try { + //沙箱环境 + Midtrans.serverKey = sandbox_serverKey; + Midtrans.clientKey = sandbox_clientKey; + Midtrans.isProduction = false; + + //生产环境 +// Midtrans.serverKey = serverKey; +// Midtrans.clientKey = clientKey; +// Midtrans.isProduction = true; + Map<String, String> requestBody = new HashMap<>(); + requestBody.put("name", beneficiary_name); + requestBody.put("account", beneficiary_account); + requestBody.put("bank", beneficiary_bank); + requestBody.put("alias_name", alias_name); + requestBody.put("email", beneficiary_email); + JSONObject beneficiaries = IrisApi.updateBeneficiaries(alias_name, requestBody, Config.getGlobalConfig()); + String status = beneficiaries.getString("status"); + if("updated".equals(status)){ + return ResultUtil.success(); + } + }catch (Exception e){ + e.printStackTrace(); + } + return ResultUtil.error("失败"); + } + + + /** + * 获取转账用户 + * @param account + * @return + */ + public static Beneficiaries getBeneficiaries(String account){ + try { + //沙箱环境 + Midtrans.serverKey = sandbox_serverKey; + Midtrans.clientKey = sandbox_clientKey; + Midtrans.isProduction = false; + + //生产环境 +// Midtrans.serverKey = serverKey; +// Midtrans.clientKey = clientKey; +// Midtrans.isProduction = true; + JSONArray beneficiaries = IrisApi.getBeneficiaries(Config.getGlobalConfig()); + for (int i = 0; i < beneficiaries.length(); i++) { + JSONObject jsonObject = beneficiaries.getJSONObject(i); + String account1 = jsonObject.getString("account"); + if(account.equals(account1)){ + Beneficiaries beneficiaries1 = new Beneficiaries(); + beneficiaries1.setName(jsonObject.getString("name")); + beneficiaries1.setAccount(jsonObject.getString("account")); + beneficiaries1.setAlias_name(jsonObject.getString("alias_name")); + beneficiaries1.setBank(jsonObject.getString("bank")); + beneficiaries1.setEmail(jsonObject.getString("email")); + return beneficiaries1; + } + } + }catch (Exception e){ + e.printStackTrace(); + } + return null; + } + + + + /** + * 单笔转账 + * @param beneficiary_name 转账用户名称 + * @param alias_name 用户别名 + * @param beneficiary_account 转账用户账号 + * @param beneficiary_bank 转账银行 + * @param beneficiary_email 邮箱 + * @param amount 金额 + * @param notes 备注 + * @return + */ + public static ResultUtil<String> payout(String beneficiary_name, String alias_name, String beneficiary_account, String beneficiary_bank, String beneficiary_email, Double amount, String notes){ + try { + //沙箱环境 + Midtrans.serverKey = sandbox_serverKey; + Midtrans.clientKey = sandbox_clientKey; + Midtrans.isProduction = false; + + //生产环境 +// Midtrans.serverKey = serverKey; +// Midtrans.clientKey = clientKey; +// Midtrans.isProduction = true; + Beneficiaries beneficiaries = getBeneficiaries(beneficiary_account); + if(null == beneficiaries){ + ResultUtil resultUtil = addBeneficiaries(beneficiary_name, alias_name, beneficiary_account, beneficiary_bank, beneficiary_email); + if(resultUtil.getCode() != 200){ + System.err.println(resultUtil.getMsg()); + return resultUtil; + } + }else{ + ResultUtil resultUtil = editBeneficiaries(beneficiary_name, alias_name, beneficiary_account, beneficiary_bank, beneficiary_email); + if(resultUtil.getCode() != 200){ + return resultUtil; + } + } + + List<Map<String, Object>> list = new ArrayList<>(); + Map<String, Object> map = new HashMap<>(); + map.put("beneficiary_name", beneficiary_name); + map.put("beneficiary_account", beneficiary_account); + map.put("beneficiary_bank", beneficiary_bank); + map.put("beneficiary_email", beneficiary_email); + map.put("amount", amount.toString()); + map.put("notes", notes); + list.add(map); + + Map<String, Object> requestBody = new HashMap<>(); + requestBody.put("payouts", list); + JSONObject jsonObject = IrisApi.createPayouts(requestBody, Config.getGlobalConfig()); + JSONArray payouts = jsonObject.getJSONArray("payouts"); + JSONObject jsonObject1 = payouts.getJSONObject(0); + String status = jsonObject1.getString("status");//状态(queued:付款正在等待执行,processed:付款请求已发送到银行并完成,completed:付款请求将发送到银行并通过收款人帐户接收,failed:付款未通过) + String reference_no = jsonObject1.getString("reference_no");//流水号 + if("queued".equals(status)){ + return ResultUtil.success("付款正在等待执行", reference_no, "0"); + } + if("processed".equals(status)){ + return ResultUtil.success("付款请求已发送到银行并完成", reference_no, "1"); + } + if("completed".equals(status)){ + return ResultUtil.success("付款请求将发送到银行并通过收款人帐户接收", reference_no, "1"); + } + if("failed".equals(status)){ + return ResultUtil.error("付款未通过"); + } + }catch (Exception e){ + e.printStackTrace(); + return ResultUtil.runErr(); + } + return ResultUtil.error("未知错误"); + } + + + /** + * 确认转账付款操作 + * @param reference_no 转账流水号 + * @param otp 谷歌身份验证OTP(QR码将在合作伙伴注册期间发送到审批者的电子邮件) + * @return + */ + public static ResultUtil approve(String reference_no, String otp){ + try { + //沙箱环境 + Midtrans.serverKey = sandbox_serverKey; + Midtrans.clientKey = sandbox_clientKey; + Midtrans.isProduction = false; + + //生产环境 +// Midtrans.serverKey = serverKey; +// Midtrans.clientKey = clientKey; +// Midtrans.isProduction = true; + + List<String> list = new ArrayList<>(); + list.add(reference_no); + + Map<String, Object> requestBody = new HashMap<>(); + requestBody.put("reference_nos", list); + requestBody.put("otp", otp); + JSONObject jsonObject = IrisApi.approvePayouts(requestBody, Config.getGlobalConfig()); + String status = jsonObject.getString("status"); + if("ok".equals(status)){ + return ResultUtil.success(); + } + }catch (Exception e){ + e.printStackTrace(); + return ResultUtil.runErr(e.getMessage()); + } + return ResultUtil.error("未知错误"); + } + + + /** + * 拒绝转账付款操作 + * @param reference_no 转账流水号 + * @param reject_reason 拒绝付款的原因 + * @return + */ + public static ResultUtil reject(String reference_no, String reject_reason){ + try { + //沙箱环境 + Midtrans.serverKey = sandbox_serverKey; + Midtrans.clientKey = sandbox_clientKey; + Midtrans.isProduction = false; + + //生产环境 +// Midtrans.serverKey = serverKey; +// Midtrans.clientKey = clientKey; +// Midtrans.isProduction = true; + + List<String> list = new ArrayList<>(); + list.add(reference_no); + + Map<String, Object> requestBody = new HashMap<>(); + requestBody.put("reference_nos", list); + requestBody.put("reject_reason", reject_reason); + JSONObject jsonObject = IrisApi.rejectPayouts(requestBody, Config.getGlobalConfig()); + String status = jsonObject.getString("status"); + if("ok".equals(status)){ + return ResultUtil.success(); + } + }catch (Exception e){ + e.printStackTrace(); + return ResultUtil.runErr(e.getMessage()); + } + return ResultUtil.error("未知错误"); + } + + + /** + * 获取转账付款详情 + * @param reference_no + * @return + */ + public static ResultUtil<Integer> getPayout(String reference_no){ + /** + * { + * "amount": "200000.00", 已处理的付款金额 + * "beneficiary_name": "Ryan Renolds", 收款人姓名 + * "beneficiary_account": "33287352", 收款人的收款账号 + * "bank": "Bank Central Asia ( BCA )", 收款人使用的银行名称 + * "reference_no": "83hgf882", 付款流水号reference_no + * "notes": "Payout June 17", 备注 + * "beneficiary_email": "beneficiary@example.com", 邮箱 + * "status": "queued", 状态 + * "created_by": "John Doe", 由制造商创建的付款 + * "created_at": "2017-01-11T00:00:00Z", + * "updated_at": "2017-01-11T00:00:00Z" + * } + */ + try { + //沙箱环境 + Midtrans.serverKey = sandbox_serverKey; + Midtrans.clientKey = sandbox_clientKey; + Midtrans.isProduction = false; + + //生产环境 +// Midtrans.serverKey = serverKey; +// Midtrans.clientKey = clientKey; +// Midtrans.isProduction = true; + + JSONObject jsonObject = IrisApi.getPayoutDetails(reference_no, Config.getGlobalConfig()); + String status = jsonObject.getString("status"); + if("queued".equals(status)){ + return ResultUtil.success("付款正在等待执行", 0); + } + if("processed".equals(status)){ + return ResultUtil.success("付款请求已发送到银行并完成", 1); + } + if("completed".equals(status)){ + return ResultUtil.success("付款请求将发送到银行并通过收款人帐户接收", 1); + } + if("failed".equals(status)){ + return ResultUtil.error("付款未通过"); + } + }catch (Exception e){ + e.printStackTrace(); + return ResultUtil.runErr(); + } + return ResultUtil.error("未知错误"); + } + + + +} -- Gitblit v1.7.1