diff --git a/src/main/java/au/com/royalpay/payment/manage/dev/web/TestController.java b/src/main/java/au/com/royalpay/payment/manage/dev/web/TestController.java index d32749adf..2101e2f3c 100644 --- a/src/main/java/au/com/royalpay/payment/manage/dev/web/TestController.java +++ b/src/main/java/au/com/royalpay/payment/manage/dev/web/TestController.java @@ -67,10 +67,7 @@ import java.io.IOException; import java.math.RoundingMode; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Optional; +import java.util.*; /** * Created by yixian on 2016-07-06. @@ -840,7 +837,7 @@ public class TestController { @ManagerMapping(value = "/shopify/rotate_secret", method = RequestMethod.POST, role = ManagerRole.DEVELOPER) public JSONObject rotateShopifySecret(@RequestBody JSONObject rotateRequest) { - shopifyManageService.rotateAccessToken(rotateRequest.getString("secret"), rotateRequest.getString("refresh_token")); - return new JSONObject(); + List failedStores = shopifyManageService.rotateAccessToken(rotateRequest.getString("secret"), rotateRequest.getString("refresh_token")); + return new JSONObject(Map.of("failed", failedStores)); } } diff --git a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/manage/ShopifyManageService.java b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/manage/ShopifyManageService.java index f7571c147..66d70d98d 100644 --- a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/manage/ShopifyManageService.java +++ b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/manage/ShopifyManageService.java @@ -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 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 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())); + } + } }