提交 55e11c10 authored 作者: 吴德鹏's avatar 吴德鹏

update

上级 a3a797ef
......@@ -173,13 +173,14 @@ public class PaypalContoller extends Controller {
//获取paypal的最新token
Optional<Token> tokenOptional = tokenRepository.findFirstToken();
Optional<String> token = Optional.empty();
//第一次执行时,token表都为空
if (tokenOptional.isPresent()) {
token = Optional.ofNullable(tokenOptional.get().getToken());
}
if (!token.isPresent()) {
String oldToken = oldApiContext.getAccessToken();
String token1 = PayPalUtil.getToken(oldToken);
String token1 = PayPalUtil.getToken(oldToken,mode);
token = Optional.ofNullable(token1);
}
......
......@@ -11,6 +11,7 @@ import com.example.afrishop_v3.util.PayPalUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
......@@ -25,6 +26,9 @@ import java.util.Optional;
public class QuartzMethod {
private final UserRepository userRepository;
@Value("${paypal.mode}")
private String mode;
private final TbCfExchangeRepository exchangeRepository;
private final TokenRepository tokenRepository;
......@@ -84,7 +88,7 @@ public class QuartzMethod {
@Scheduled(cron = "0 0 0/3 * * ? ")
public void getToken() throws IOException {
Optional<Token> firstToken = tokenRepository.findFirstToken();
String token = PayPalUtil.getToken(firstToken.get().getToken());
String token = PayPalUtil.getToken(firstToken.get().getToken(),mode);
if (!tokenRepository.existsByToken(token)) {
Token t = new Token();
t.setId(IdUtil.createIdbyUUID());
......
......@@ -8,20 +8,24 @@ import java.io.IOException;
public class PayPalUtil {
//正式
// private static String url="https://api.paypal.com/v1";
//测试
private static String url="https://api.sandbox.paypal.com/v1";
public static String getToken(String oldToken) throws IOException {
/**
* paypal token 默认是8小时有效
* 以旧token(必须是未失效的)才能换新token
* 如果不刷新token,8小时后paypal将无法使用
*/
public static String getToken(String oldToken, String mode) throws IOException {
OkHttpClient client = new OkHttpClient();
String url;
if ("live".equalsIgnoreCase(mode)) {
url = "https://api.paypal.com/v1";
} else {
url = "https://api.sandbox.paypal.com/v1";
}
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials");
Request request = new Request.Builder()
.url(url+"/oauth2/token")
.url(url + "/oauth2/token")
.post(body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Authorization", oldToken)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论