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

update

上级 1632dfd2
package com.example.afrishop_v3.config;
import com.example.afrishop_v3.models.Token;
import com.example.afrishop_v3.repository.TokenRepository;
import com.example.afrishop_v3.util.PayPalUtil;
import com.paypal.base.codec.binary.Base64;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException;
import com.squareup.okhttp.*;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.*;
......@@ -34,7 +33,6 @@ public class PaypalConfig {
private String mode;
@Bean
public Map<String, String> paypalSdkConfig() {
Map<String, String> sdkConfig = new HashMap<>();
......@@ -50,11 +48,12 @@ public class PaypalConfig {
@Scope("prototype")
@Bean
public APIContext apiContext() throws IOException {
APIContext apiContext = new APIContext(PayPalUtil.getToken());
public APIContext apiContext(TokenRepository tokenRepository) {
Optional<Token> firstToken = tokenRepository.findFirstToken();
APIContext apiContext = new APIContext(firstToken.get().getToken());
apiContext.setConfigurationMap(paypalSdkConfig());
return apiContext;
}
}
\ No newline at end of file
}
......@@ -249,6 +249,8 @@ public class PostController {
@Transactional()
@PostMapping("/upload")
public List<String> handleFileUpload(@RequestParam("files") MultipartFile[] files, @RequestParam("thumbs") MultipartFile[] thumbs, @ModelAttribute("Post") Post ePost) {
logger.info("picture:"+files);
logger.info("thumbs:"+thumbs);
Post post = new Post();
TbCfUserInfo user = this.user.user();
post.setUser(user);
......
package com.example.afrishop_v3.models;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
/**
* @Auther: wudepeng
* @Date: 2020/12/28
* @Description:PayPal Token
*/
@Entity
@Data
public class Token {
@Id
private String id;
private String token;
private Date createTime;
}
package com.example.afrishop_v3.quartz;
import com.example.afrishop_v3.models.Token;
import com.example.afrishop_v3.repository.TbCfExchangeRepository;
import com.example.afrishop_v3.repository.TokenRepository;
import com.example.afrishop_v3.repository.UserRepository;
import com.example.afrishop_v3.util.HttpClientUtil;
import com.example.afrishop_v3.util.IdUtil;
import com.example.afrishop_v3.util.PayPalUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Qualifier;
......@@ -13,6 +17,8 @@ import org.springframework.stereotype.Component;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Optional;
@Component
......@@ -20,10 +26,12 @@ public class QuartzMethod {
private final UserRepository userRepository;
private final TbCfExchangeRepository exchangeRepository;
private final TokenRepository tokenRepository;
public QuartzMethod(UserRepository userRepository, @Qualifier("tbCfExchangeRepository") TbCfExchangeRepository exchangeRepository) {
public QuartzMethod(UserRepository userRepository, @Qualifier("tbCfExchangeRepository") TbCfExchangeRepository exchangeRepository, TokenRepository tokenRepository) {
this.userRepository = userRepository;
this.exchangeRepository = exchangeRepository;
this.tokenRepository = tokenRepository;
}
// //@Scheduled(cron = "0 0/1 * * * ? ")
......@@ -41,33 +49,48 @@ public class QuartzMethod {
/**
* 美元-克瓦查
*
* @throws IOException
* @throws URISyntaxException
*/
@Scheduled(cron = "0 0 3 * * ?")
public void getRent() throws IOException, URISyntaxException {
public void getRent() throws IOException, URISyntaxException {
String content = HttpClientUtil.getContentByUrl("https://cn.valutafx.com/USD-ZMW.htm?amount=1", "rent");
// 解析为 Document 对象
Document document = Jsoup.parse(content);
String rent = document.select("div[class=rate-value]").text();
BigDecimal rate=new BigDecimal(rent);
String type="USD-ZMW";
exchangeRepository.updateByType(type,rate);
BigDecimal rate = new BigDecimal(rent);
String type = "USD-ZMW";
exchangeRepository.updateByType(type, rate);
}
/**
* 美元-人民币
*
* @throws IOException
* @throws URISyntaxException
*/
@Scheduled(cron = "0 0 3 * * ?")
public void getRate() throws IOException, URISyntaxException {
public void getRate() throws IOException, URISyntaxException {
String content = HttpClientUtil.getContentByUrl("https://cn.valutafx.com/USD-CNY.htm?amount=1", "rent");
// 解析为 Document 对象
Document document = Jsoup.parse(content);
String rent = document.select("div[class=rate-value]").text();
BigDecimal rate=new BigDecimal(rent);
String type="USD-CNY";
exchangeRepository.updateByType(type,rate);
BigDecimal rate = new BigDecimal(rent);
String type = "USD-CNY";
exchangeRepository.updateByType(type, rate);
}
@Scheduled(cron = "0 0 0/3 * * ? ")
public void getToken() throws IOException {
Optional<Token> firstToken = tokenRepository.findFirstToken();
String token = PayPalUtil.getToken(firstToken.get().getToken());
if (!tokenRepository.existsByToken(token)) {
Token t = new Token();
t.setId(IdUtil.createIdbyUUID());
t.setToken(token);
t.setCreateTime(new Date());
tokenRepository.save(t);
}
}
}
package com.example.afrishop_v3.repository;
import com.example.afrishop_v3.models.Token;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Optional;
/**
* @Auther: wudepeng
* @Date: 2020/12/28
* @Description:
*/
public interface TokenRepository extends JpaRepository<Token, String> {
@Query(value = "select * from token s order by s.create_time desc limit 1 ", nativeQuery = true)
Optional<Token> findFirstToken();
boolean existsByToken(String token);
}
......@@ -14,7 +14,7 @@ public class PayPalUtil {
//测试
private static String url="https://api.sandbox.paypal.com/v1";
public static String getToken() throws IOException {
public static String getToken(String oldToken) throws IOException {
OkHttpClient client = new OkHttpClient();
......@@ -24,7 +24,7 @@ public class PayPalUtil {
.url(url+"/oauth2/token")
.post(body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Authorization", "Bearer A21AAI3JoFShZn_xG0Pr6fjqDFQG2dWcDC2WePIM6qpFgplWmj-b9KfXm-crSa4cq6Z5PwmiE5DdMcNDzJoSsYrYCZd9myh2g")
.addHeader("Authorization", oldToken)
.addHeader("cache-control", "no-cache")
.build();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论