package com.stylefeng.guns.modular.system.utils;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.stripe.Stripe;
|
import com.stripe.exception.*;
|
import com.stripe.model.Customer;
|
import com.stripe.model.PaymentIntent;
|
import com.stripe.model.PaymentMethod;
|
import com.stripe.model.PaymentMethodCollection;
|
import com.stripe.net.Webhook;
|
import com.stripe.param.CustomerCreateParams;
|
import com.stripe.param.PaymentIntentCreateParams;
|
import com.stripe.param.PaymentIntentUpdateParams;
|
import com.stripe.param.PaymentMethodListParams;
|
|
import java.math.BigDecimal;
|
import java.util.HashMap;
|
import java.util.logging.Logger;
|
|
public class PaymentProcessor {
|
|
public static String getMethod(String customer) throws StripeException {
|
|
|
PaymentMethodListParams params =
|
PaymentMethodListParams.builder()
|
.setCustomer(customer)
|
.setType(PaymentMethodListParams.Type.CARD)
|
.build();
|
|
PaymentMethodCollection paymentMethods = PaymentMethod.list(params);
|
String s = JSON.toJSONString(paymentMethods);
|
return s;
|
}
|
|
|
public static void main(String[] args) {
|
try {
|
|
String cus_odnGgggJJ7iEJh = getMethod("cus_OdnGgggJJ7iEJh");
|
JSONObject jsonObject = JSON.parseObject(cus_odnGgggJJ7iEJh);
|
Object id1 = jsonObject.get("id");
|
long l = 1;
|
Boolean pay = PaymentProcessor.pay("cus_odnGgggJJ7iEJh", id1.toString(), l);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
public static Boolean pay(String cus,String method,Long money) throws StripeException {
|
// Stripe.apiKey = "sk_test_51Mu5D0KDN0sswRVwUNL5998QrX1VJkNrLsflVUXkEFH0cY2l9StrCc5O4c9gtzs7tLbVmeaTfyNL6pnrtw1tRClA00ZVfbNb3d";
|
Stripe.apiKey = "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";
|
PaymentIntentCreateParams params =
|
PaymentIntentCreateParams.builder()
|
.setCurrency("usd")
|
.setAmount(money)
|
.setCustomer(cus)
|
.setPaymentMethod(method)
|
.setErrorOnRequiresAction(true)
|
.setConfirm(true)
|
.build();
|
try {
|
PaymentIntent paymentIntent = PaymentIntent.create(params);
|
return true;
|
} catch (CardException err) {
|
// Error code will be authentication_required if authentication is needed
|
String paymentIntentId = err.getStripeError().getPaymentIntent().getId();
|
PaymentIntent paymentIntent = PaymentIntent.retrieve(paymentIntentId);
|
System.out.println(paymentIntent.getId());
|
return false;
|
}catch (RateLimitException e) {
|
// Too many requests made to the API too quickly
|
|
return false;
|
} catch (InvalidRequestException e) {
|
// Invalid parameters were supplied to Stripe's API
|
return false;
|
} catch (AuthenticationException e) {
|
// Authentication with Stripe's API failed
|
// (maybe you changed API keys recently)
|
return false;
|
} catch (StripeException e) {
|
// Display a very generic error to the user, and maybe send
|
// yourself an email
|
return false;
|
} catch (Exception e) {
|
// Something else happened, completely unrelated to Stripe
|
return false;
|
}
|
}
|
|
|
// 定义 Stripe API 密钥
|
// private static final String STRIPE_API_KEY = "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";
|
//
|
// public PaymentProcessor() {
|
// // 初始化 Stripe 对象并设置 API 密钥
|
// Stripe.apiKey = STRIPE_API_KEY;
|
// }
|
//
|
// /**
|
// * 创建一个 Stripe 客户,并保存他们的信用卡信息,以备将来使用。
|
// * 支持多种付款方式,例如支付宝、微信等。这里只是演示如何使用信用卡进行支付。
|
// *
|
// * @param email 客户电子邮件地址
|
// * @param cardToken 由 Stripe.js 获取的信用卡令牌
|
// * @return Stripe 客户对象
|
// * @throws StripeException 如果创建客户或付款交易时出现错误
|
// */
|
// public Customer createCustomer(String email, String cardToken) throws StripeException {
|
//
|
// // 构造付款交易参数
|
// PaymentIntentCreateParams params = PaymentIntentCreateParams.builder()
|
// .setAmount(1000L) // 设置要收取的金额,以美分为单位(例如 $10.00 = 1000 美分)
|
// .setCurrency("usd") // 设置货币类型:USD(美元)
|
// .setPaymentMethod(cardToken) // 设置由 Stripe.js 获取的信用卡令牌
|
// .build();
|
//
|
// // 创建付款交易
|
// PaymentIntent paymentIntent = PaymentIntent.create(params);
|
//
|
// // 创建一个 Stripe 客户,将他们的付款方法与 Stripe 帐户关联在一起
|
// Customer customer = Customer.create(
|
// CustomerCreateParams.builder()
|
// .setEmail(email) // 设置客户的电子邮件地址
|
// .setPaymentMethod(paymentIntent.getPaymentMethod()) // 将付款方法与客户端 Stripe 帐户相关联
|
// .build()
|
// );
|
//
|
// // 返回创建的 Stripe 客户对象
|
// return customer;
|
// }
|
//
|
// /**
|
// * 从一个 Stripe 客户处收取付款。
|
// *
|
// * @param customerId Stripe 客户 ID
|
// * @param amount 要收取的金额,以美分为单位
|
// * @return Stripe 付款交易对象
|
// * @throws StripeException 如果捕获到 Stripe API 返回的错误
|
// */
|
// public static PaymentIntent charge(String customerId, long amount) throws StripeException {
|
//
|
// // 构造付款交易参数
|
// PaymentIntentCreateParams params = PaymentIntentCreateParams.builder()
|
// .setCustomer(customerId) // 使用客户 ID 作为付款目标
|
// .setAmount(amount) // 设置要收取的金额,以美分为单位
|
// .setCurrency("usd").setDescription("1").setReturnUrl("") // 设置货币类型:USD(美元)
|
// .build();
|
//
|
// // 创建付款交易
|
// PaymentIntent paymentIntent = PaymentIntent.create(params);
|
//
|
// // 确认付款
|
// paymentIntent.confirm();
|
//
|
// // 返回创建的 Stripe 付款交易对象
|
// return paymentIntent;
|
// }
|
|
|
}
|