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

update

上级 1632dfd2
package com.example.afrishop_v3.config; 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.example.afrishop_v3.util.PayPalUtil;
import com.paypal.base.codec.binary.Base64; import com.paypal.base.codec.binary.Base64;
import com.paypal.base.rest.APIContext; import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential; import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException; import com.paypal.base.rest.PayPalRESTException;
import com.squareup.okhttp.*;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.apache.commons.lang3.builder.ToStringExclude; import org.apache.commons.lang3.builder.ToStringExclude;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*; import org.springframework.context.annotation.*;
import org.testng.annotations.Test;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*; import java.util.concurrent.*;
...@@ -34,7 +33,6 @@ public class PaypalConfig { ...@@ -34,7 +33,6 @@ public class PaypalConfig {
private String mode; private String mode;
@Bean @Bean
public Map<String, String> paypalSdkConfig() { public Map<String, String> paypalSdkConfig() {
Map<String, String> sdkConfig = new HashMap<>(); Map<String, String> sdkConfig = new HashMap<>();
...@@ -50,8 +48,9 @@ public class PaypalConfig { ...@@ -50,8 +48,9 @@ public class PaypalConfig {
@Scope("prototype") @Scope("prototype")
@Bean @Bean
public APIContext apiContext() throws IOException { public APIContext apiContext(TokenRepository tokenRepository) {
APIContext apiContext = new APIContext(PayPalUtil.getToken()); Optional<Token> firstToken = tokenRepository.findFirstToken();
APIContext apiContext = new APIContext(firstToken.get().getToken());
apiContext.setConfigurationMap(paypalSdkConfig()); apiContext.setConfigurationMap(paypalSdkConfig());
return apiContext; return apiContext;
} }
......
...@@ -249,6 +249,8 @@ public class PostController { ...@@ -249,6 +249,8 @@ public class PostController {
@Transactional() @Transactional()
@PostMapping("/upload") @PostMapping("/upload")
public List<String> handleFileUpload(@RequestParam("files") MultipartFile[] files, @RequestParam("thumbs") MultipartFile[] thumbs, @ModelAttribute("Post") Post ePost) { 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(); Post post = new Post();
TbCfUserInfo user = this.user.user(); TbCfUserInfo user = this.user.user();
post.setUser(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; 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.TbCfExchangeRepository;
import com.example.afrishop_v3.repository.TokenRepository;
import com.example.afrishop_v3.repository.UserRepository; import com.example.afrishop_v3.repository.UserRepository;
import com.example.afrishop_v3.util.HttpClientUtil; 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.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
...@@ -13,6 +17,8 @@ import org.springframework.stereotype.Component; ...@@ -13,6 +17,8 @@ import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Date;
import java.util.Optional;
@Component @Component
...@@ -20,10 +26,12 @@ public class QuartzMethod { ...@@ -20,10 +26,12 @@ public class QuartzMethod {
private final UserRepository userRepository; private final UserRepository userRepository;
private final TbCfExchangeRepository exchangeRepository; 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.userRepository = userRepository;
this.exchangeRepository = exchangeRepository; this.exchangeRepository = exchangeRepository;
this.tokenRepository = tokenRepository;
} }
// //@Scheduled(cron = "0 0/1 * * * ? ") // //@Scheduled(cron = "0 0/1 * * * ? ")
...@@ -41,6 +49,7 @@ public class QuartzMethod { ...@@ -41,6 +49,7 @@ public class QuartzMethod {
/** /**
* 美元-克瓦查 * 美元-克瓦查
*
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
...@@ -50,13 +59,14 @@ public class QuartzMethod { ...@@ -50,13 +59,14 @@ public class QuartzMethod {
// 解析为 Document 对象 // 解析为 Document 对象
Document document = Jsoup.parse(content); Document document = Jsoup.parse(content);
String rent = document.select("div[class=rate-value]").text(); String rent = document.select("div[class=rate-value]").text();
BigDecimal rate=new BigDecimal(rent); BigDecimal rate = new BigDecimal(rent);
String type="USD-ZMW"; String type = "USD-ZMW";
exchangeRepository.updateByType(type,rate); exchangeRepository.updateByType(type, rate);
} }
/** /**
* 美元-人民币 * 美元-人民币
*
* @throws IOException * @throws IOException
* @throws URISyntaxException * @throws URISyntaxException
*/ */
...@@ -66,8 +76,21 @@ public class QuartzMethod { ...@@ -66,8 +76,21 @@ public class QuartzMethod {
// 解析为 Document 对象 // 解析为 Document 对象
Document document = Jsoup.parse(content); Document document = Jsoup.parse(content);
String rent = document.select("div[class=rate-value]").text(); String rent = document.select("div[class=rate-value]").text();
BigDecimal rate=new BigDecimal(rent); BigDecimal rate = new BigDecimal(rent);
String type="USD-CNY"; String type = "USD-CNY";
exchangeRepository.updateByType(type,rate); 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 { ...@@ -14,7 +14,7 @@ public class PayPalUtil {
//测试 //测试
private static String url="https://api.sandbox.paypal.com/v1"; 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(); OkHttpClient client = new OkHttpClient();
...@@ -24,7 +24,7 @@ public class PayPalUtil { ...@@ -24,7 +24,7 @@ public class PayPalUtil {
.url(url+"/oauth2/token") .url(url+"/oauth2/token")
.post(body) .post(body)
.addHeader("Content-Type", "application/x-www-form-urlencoded") .addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Authorization", "Bearer A21AAI3JoFShZn_xG0Pr6fjqDFQG2dWcDC2WePIM6qpFgplWmj-b9KfXm-crSa4cq6Z5PwmiE5DdMcNDzJoSsYrYCZd9myh2g") .addHeader("Authorization", oldToken)
.addHeader("cache-control", "no-cache") .addHeader("cache-control", "no-cache")
.build(); .build();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论