118 lines
3.5 KiB
Java
118 lines
3.5 KiB
Java
|
|
package com.example.demo.config;
|
|||
|
|
|
|||
|
|
import com.paypal.base.rest.APIContext;
|
|||
|
|
import com.paypal.base.rest.OAuthTokenCredential;
|
|||
|
|
import com.paypal.base.rest.PayPalRESTException;
|
|||
|
|
import org.slf4j.Logger;
|
|||
|
|
import org.slf4j.LoggerFactory;
|
|||
|
|
import org.springframework.beans.factory.annotation.Value;
|
|||
|
|
import org.springframework.context.annotation.Bean;
|
|||
|
|
import org.springframework.context.annotation.Configuration;
|
|||
|
|
|
|||
|
|
import java.util.HashMap;
|
|||
|
|
import java.util.Map;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* PayPal配置类
|
|||
|
|
* 配置PayPal SDK和API上下文
|
|||
|
|
*/
|
|||
|
|
@Configuration
|
|||
|
|
public class PayPalConfig {
|
|||
|
|
|
|||
|
|
private static final Logger logger = LoggerFactory.getLogger(PayPalConfig.class);
|
|||
|
|
|
|||
|
|
@Value("${paypal.client-id:}")
|
|||
|
|
private String clientId;
|
|||
|
|
|
|||
|
|
@Value("${paypal.client-secret:}")
|
|||
|
|
private String clientSecret;
|
|||
|
|
|
|||
|
|
@Value("${paypal.mode:sandbox}")
|
|||
|
|
private String mode;
|
|||
|
|
|
|||
|
|
@Value("${paypal.success-url:https://vionow.com/api/payment/paypal/success}")
|
|||
|
|
private String successUrl;
|
|||
|
|
|
|||
|
|
@Value("${paypal.cancel-url:https://vionow.com/api/payment/paypal/cancel}")
|
|||
|
|
private String cancelUrl;
|
|||
|
|
|
|||
|
|
// CNY到USD的汇率配置,默认7.2(即1美元=7.2人民币)
|
|||
|
|
@Value("${paypal.exchange-rate:7.2}")
|
|||
|
|
private double exchangeRate;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 创建PayPal API上下文
|
|||
|
|
* @return API上下文对象
|
|||
|
|
*/
|
|||
|
|
@Bean
|
|||
|
|
public APIContext apiContext() {
|
|||
|
|
try {
|
|||
|
|
// 验证配置
|
|||
|
|
if (clientId == null || clientId.isEmpty()) {
|
|||
|
|
logger.warn("PayPal Client ID未配置,PayPal功能将不可用");
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (clientSecret == null || clientSecret.isEmpty()) {
|
|||
|
|
logger.warn("PayPal Client Secret未配置,PayPal功能将不可用");
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
logger.info("=== 初始化PayPal配置 ===");
|
|||
|
|
logger.info("Client ID: {}...", clientId.substring(0, Math.min(10, clientId.length())));
|
|||
|
|
logger.info("Mode: {}", mode);
|
|||
|
|
logger.info("Success URL: {}", successUrl);
|
|||
|
|
logger.info("Cancel URL: {}", cancelUrl);
|
|||
|
|
|
|||
|
|
// 创建PayPal配置
|
|||
|
|
Map<String, String> configMap = new HashMap<>();
|
|||
|
|
configMap.put("mode", mode);
|
|||
|
|
|
|||
|
|
// 创建OAuth凭证
|
|||
|
|
OAuthTokenCredential credential = new OAuthTokenCredential(clientId, clientSecret, configMap);
|
|||
|
|
|
|||
|
|
// 创建API上下文
|
|||
|
|
APIContext context = new APIContext(credential.getAccessToken());
|
|||
|
|
context.setConfigurationMap(configMap);
|
|||
|
|
|
|||
|
|
logger.info("✅ PayPal配置初始化成功");
|
|||
|
|
|
|||
|
|
return context;
|
|||
|
|
|
|||
|
|
} catch (PayPalRESTException e) {
|
|||
|
|
logger.error("❌ PayPal配置初始化失败", e);
|
|||
|
|
logger.error("错误信息: {}", e.getMessage());
|
|||
|
|
logger.error("详细错误: {}", e.getDetails());
|
|||
|
|
return null;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
logger.error("❌ PayPal配置初始化失败", e);
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Getters
|
|||
|
|
public String getClientId() {
|
|||
|
|
return clientId;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public String getClientSecret() {
|
|||
|
|
return clientSecret;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public String getMode() {
|
|||
|
|
return mode;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public String getSuccessUrl() {
|
|||
|
|
return successUrl;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public String getCancelUrl() {
|
|||
|
|
return cancelUrl;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public double getExchangeRate() {
|
|||
|
|
return exchangeRate;
|
|||
|
|
}
|
|||
|
|
}
|