目前,我所拥有的是简单的
ChargeRequest
和PaymentStatus
作为我的实体,StripeService
,ChargeController
和CheckoutController
作为我的控制器。
我正在尝试在 .bashrc 中设置环境变量:
STRIPE_SECRET_KEY=my_key
但是,还是报错:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'STRIPE_SECRET_KEY' in value "${STRIPE_SECRET_KEY}"
这是StripeService:
package com.edoyou.k2sbeauty.services.implementations;
import com.edoyou.k2sbeauty.entities.model.ChargeRequest;
import com.stripe.Stripe;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.APIException;
import com.stripe.exception.CardException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.model.Charge;
import jakarta.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;
@Service
public class StripeService {
@Value("${STRIPE_SECRET_KEY}")
private String secretKey;
@PostConstruct
public void init() {
Stripe.apiKey = secretKey;
}
public Charge charge(ChargeRequest chargeRequest)
throws AuthenticationException, InvalidRequestException,
APIConnectionException, CardException, APIException, com.stripe.exception.AuthenticationException {
Map<String, Object> chargeParams = new HashMap<>();
chargeParams.put("amount", chargeRequest.getAmount());
chargeParams.put("currency", chargeRequest.getCurrency());
chargeParams.put("description", chargeRequest.getDescription());
chargeParams.put("source", chargeRequest.getStripeToken());
return Charge.create(chargeParams);
}
}
这不起作用的可能原因是您没有从 .bashrc 文件中导出环境变量。 Bash 环境变量需要
exported
才能对子进程可用。
将 .bashrc 文件中的变量声明更改为:
export STRIPE_SECRET_KEY=my_key
使用
. .bashrc
获取您的 .bashrc 文件,然后在启动服务之前验证环境变量是否存在。您必须在您的 .bashrc
更改产生的进程中启动该服务。例如,这个环境变量对您在进行这些更改之前打开的 ide 不可见。