|
|
|
@ -5,8 +5,14 @@ import au.com.royalpay.payment.manage.shopify.store.domain.entity.ShopifyStore;
|
|
|
|
|
import au.com.royalpay.shopify.config.ShopifyAuthProvider;
|
|
|
|
|
import au.com.royalpay.shopify.entity.ShopifyAccessToken;
|
|
|
|
|
import au.com.royalpay.shopify.service.ShopifyAuthService;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class ShopifyManageService {
|
|
|
|
|
|
|
|
|
@ -20,15 +26,26 @@ public class ShopifyManageService {
|
|
|
|
|
this.shopifyStoreMapper = shopifyStoreMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void rotateAccessToken(String newSecret, String refreshToken) {
|
|
|
|
|
public List<JSONObject> rotateAccessToken(String newSecret, String refreshToken) {
|
|
|
|
|
shopifyAuthProvider.addSecret(newSecret);
|
|
|
|
|
shopifyStoreMapper.listAvailableStores().parallelStream().filter(store -> !store.getTokenSecret().equalsIgnoreCase(newSecret))
|
|
|
|
|
.forEach(store -> rotateStoreAccessToken(store, refreshToken));
|
|
|
|
|
shopifyAuthProvider.activateSecret(newSecret);
|
|
|
|
|
List<JSONObject> failedStore = shopifyStoreMapper.listAvailableStores().parallelStream().filter(store -> !store.getTokenSecret().equalsIgnoreCase(newSecret))
|
|
|
|
|
.map(store -> rotateStoreAccessToken(store, refreshToken))
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
if (failedStore.isEmpty()) {
|
|
|
|
|
shopifyAuthProvider.activateSecret(newSecret);
|
|
|
|
|
}
|
|
|
|
|
return failedStore;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void rotateStoreAccessToken(ShopifyStore store, String refreshToken) {
|
|
|
|
|
ShopifyAccessToken newToken = shopifyAuthService.rotateAccessToken(store.storeName(), refreshToken, store.getAccessToken());
|
|
|
|
|
shopifyStoreMapper.update(ShopifyStore.builder().id(store.getId()).accessToken(newToken.getAccess_token()).tokenSecret(newToken.getSecret()).build());
|
|
|
|
|
private JSONObject rotateStoreAccessToken(ShopifyStore store, String refreshToken) {
|
|
|
|
|
try {
|
|
|
|
|
ShopifyAccessToken newToken = shopifyAuthService.rotateAccessToken(store.storeName(), refreshToken, store.getAccessToken());
|
|
|
|
|
shopifyStoreMapper.update(ShopifyStore.builder().id(store.getId()).accessToken(newToken.getAccess_token()).tokenSecret(newToken.getSecret()).build());
|
|
|
|
|
return null;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return new JSONObject(Map.of("shop", store.getShopifyShop(), "client_moniker", store.getClientMoniker(), "message", e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|