From 480bc50442238900885e2b21ac7f44a3673646a9 Mon Sep 17 00:00:00 2001 From: ycfxx Date: Wed, 12 Jan 2022 16:39:00 +0800 Subject: [PATCH] upgrade shopify OAuth processing --- pom.xml | 5 ++ .../ShopifyMerchantAuthApplication.java | 27 ++++++++++- .../domain/entity/ShopifyAccessToken.java | 6 +++ .../graphqlclient/GraphqlRequestBody.java | 15 ++++++ .../PaymentsAppConfigureClient.java | 48 +++++++++++++++++++ .../PaymentsAppConfigureRequestBody.java | 16 +++++++ .../support/GraphqlSchemaReaderUtil.java | 11 +++++ src/main/resources/application.yml | 1 + .../graphql/paymentsAppConfigure.graphql | 8 ++++ .../templates/shopify/auth_back.html | 9 ++++ .../ui/static/shopify/auth/shopify.auth.js | 4 ++ .../shopify/auth/templates/shopify_auth.html | 13 +++-- .../PaymentsAppConfigureClientTest.java | 34 +++++++++++++ 13 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/GraphqlRequestBody.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureClient.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureRequestBody.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/shopify/support/GraphqlSchemaReaderUtil.java create mode 100644 src/main/resources/graphql/paymentsAppConfigure.graphql create mode 100644 src/test/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureClientTest.java diff --git a/pom.xml b/pom.xml index d692c8bf6..8682c5ddc 100644 --- a/pom.xml +++ b/pom.xml @@ -231,6 +231,11 @@ 0.1.55 + + org.springframework.boot + spring-boot-starter-webflux + + diff --git a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/application/ShopifyMerchantAuthApplication.java b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/application/ShopifyMerchantAuthApplication.java index 08707668a..2731087dd 100644 --- a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/application/ShopifyMerchantAuthApplication.java +++ b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/application/ShopifyMerchantAuthApplication.java @@ -2,6 +2,7 @@ package au.com.royalpay.payment.manage.shopify.auth.domain.application; import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyAccessToken; import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyPermissionURL; +import au.com.royalpay.payment.manage.shopify.auth.domain.graphqlclient.PaymentsAppConfigureClient; import au.com.royalpay.payment.manage.shopify.auth.domain.service.ShopifyAuthService; import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyPermissionRequest; import au.com.royalpay.payment.manage.shopify.store.domain.entity.ShopifyStore; @@ -13,14 +14,26 @@ import au.com.royalpay.payment.manage.shopify.store.domain.service.ShopifyStoreS import au.com.royalpay.payment.manage.signin.beans.LoginInfo; import au.com.royalpay.payment.manage.signin.core.SignInStatusManager; import au.com.royalpay.payment.tools.exceptions.BadRequestException; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.io.IOException; import java.util.Date; +@Slf4j @Component public class ShopifyMerchantAuthApplication { + @Value("${shopify.version:2021-10}") + private String apiVersion; + + @Value("${shopify.auth.apiKey}") + private String apiKey; + + private static final String PAYMENT_SETTING_URL = "https://%s/services/payments_partners/gateways/%s/settings"; + @Autowired private ShopifyAuthService shopifyAuthService; @@ -36,6 +49,9 @@ public class ShopifyMerchantAuthApplication { @Autowired private MerchantService merchantService; + @Autowired + private PaymentsAppConfigureClient paymentsAppConfigureClient; + /** * 获取shopify店铺授权URL * @@ -81,6 +97,15 @@ public class ShopifyMerchantAuthApplication { throw new BadRequestException("Get access token error"); } shopifyStoreService.modifyShopifyStore(shopifyStore.setAccessToken(accessToken.getAccess_token()).setScope(accessToken.getScope())); - return accessToken; + + try { + paymentsAppConfigureClient.paymentsAppConfigure(shop, true, apiVersion); + } catch (IOException e) { + log.error(String.format("Shopify store [%s] payment app setting error: %s", shop, e.getMessage())); + throw new BadRequestException("Payment app setting error"); + } + + String redirectUrl = String.format(PAYMENT_SETTING_URL, shop, apiKey); + return accessToken.setRedirectUrl(redirectUrl); } } diff --git a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/entity/ShopifyAccessToken.java b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/entity/ShopifyAccessToken.java index a28858350..3e5f7e25d 100644 --- a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/entity/ShopifyAccessToken.java +++ b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/entity/ShopifyAccessToken.java @@ -1,11 +1,17 @@ package au.com.royalpay.payment.manage.shopify.auth.domain.entity; import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; @Data +@Accessors(chain = true) +@NoArgsConstructor public class ShopifyAccessToken { private String access_token; private String scope; + + private String redirectUrl; } diff --git a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/GraphqlRequestBody.java b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/GraphqlRequestBody.java new file mode 100644 index 000000000..b96930786 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/GraphqlRequestBody.java @@ -0,0 +1,15 @@ +package au.com.royalpay.payment.manage.shopify.auth.domain.graphqlclient; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class GraphqlRequestBody { + private String query; + private Object variables; +} diff --git a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureClient.java b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureClient.java new file mode 100644 index 000000000..943481c20 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureClient.java @@ -0,0 +1,48 @@ +package au.com.royalpay.payment.manage.shopify.auth.domain.graphqlclient; + +import au.com.royalpay.payment.manage.shopify.store.domain.entity.ShopifyStore; +import au.com.royalpay.payment.manage.shopify.store.domain.service.ShopifyStoreService; +import au.com.royalpay.payment.manage.shopify.support.GraphqlSchemaReaderUtil; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +import java.io.IOException; + +@Slf4j +@Component +public class PaymentsAppConfigureClient { + + private static final String url = "https://%s/payments_apps/api/%s/graphql.json"; + + @Autowired + private ShopifyStoreService shopifyStoreService; + + public void paymentsAppConfigure(String shopDomain, Boolean ready, String apiVersion) throws IOException { + + WebClient webClient = WebClient.builder().build(); + + GraphqlRequestBody graphQLRequestBody = new GraphqlRequestBody(); + + final String query = GraphqlSchemaReaderUtil.getSchemaFromFileName("paymentsAppConfigure"); + + graphQLRequestBody.setQuery(query); + PaymentsAppConfigureRequestBody requestBody = new PaymentsAppConfigureRequestBody(shopDomain, ready); + graphQLRequestBody.setVariables(requestBody); + + ShopifyStore shopifyStore = shopifyStoreService.getByShopifyShop(shopDomain); + JSONObject result = webClient + .post() + .uri(String.format(url, shopDomain, apiVersion)) + .header("X-Shopify-Access-Token", shopifyStore.getAccessToken()) + .bodyValue(graphQLRequestBody) + .retrieve() + .bodyToMono(JSONObject.class) + .block(); + + log.info(String.format("retrieve: [%s]", result.toString())); + } + +} diff --git a/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureRequestBody.java b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureRequestBody.java new file mode 100644 index 000000000..a9ce7c90d --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/shopify/auth/domain/graphqlclient/PaymentsAppConfigureRequestBody.java @@ -0,0 +1,16 @@ +package au.com.royalpay.payment.manage.shopify.auth.domain.graphqlclient; + +import lombok.Data; + +@Data +public class PaymentsAppConfigureRequestBody { + + private String externalHandle; + + private boolean ready; + + public PaymentsAppConfigureRequestBody(String shopDomain, Boolean ready) { + this.externalHandle = shopDomain; + this.ready = ready; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/shopify/support/GraphqlSchemaReaderUtil.java b/src/main/java/au/com/royalpay/payment/manage/shopify/support/GraphqlSchemaReaderUtil.java new file mode 100644 index 000000000..6fdfe7c21 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/shopify/support/GraphqlSchemaReaderUtil.java @@ -0,0 +1,11 @@ +package au.com.royalpay.payment.manage.shopify.support; + +import java.io.IOException; + +public class GraphqlSchemaReaderUtil { + + public static String getSchemaFromFileName(final String filename) throws IOException { + return new String( + GraphqlSchemaReaderUtil.class.getClassLoader().getResourceAsStream("graphql/" + filename + ".graphql").readAllBytes()); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 5d2a85f93..056979324 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -176,6 +176,7 @@ logging: payment: debug shopify: + apiVersion: 2022-01 auth: apiKey: 99e631dd0faa1076ceffae36cf91a93b apiSecretKey: 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 diff --git a/src/main/resources/graphql/paymentsAppConfigure.graphql b/src/main/resources/graphql/paymentsAppConfigure.graphql new file mode 100644 index 000000000..e6e0d32dc --- /dev/null +++ b/src/main/resources/graphql/paymentsAppConfigure.graphql @@ -0,0 +1,8 @@ +mutation PaymentsAppConfigure($externalHandle: String, $ready: Boolean!) { + paymentsAppConfigure(externalHandle: $externalHandle, ready: $ready) { + userErrors{ + field + message + } + } +} \ No newline at end of file diff --git a/src/main/resources/templates/shopify/auth_back.html b/src/main/resources/templates/shopify/auth_back.html index 5df9ad276..afe68c072 100644 --- a/src/main/resources/templates/shopify/auth_back.html +++ b/src/main/resources/templates/shopify/auth_back.html @@ -66,5 +66,14 @@ ©2015-2021 RoyalPay. + + + \ No newline at end of file diff --git a/src/main/ui/static/shopify/auth/shopify.auth.js b/src/main/ui/static/shopify/auth/shopify.auth.js index c9c577453..d3f97904a 100644 --- a/src/main/ui/static/shopify/auth/shopify.auth.js +++ b/src/main/ui/static/shopify/auth/shopify.auth.js @@ -48,6 +48,10 @@ define(['angular', 'uiRouter', 'uiBootstrap'], function (angular) { that.authDisable = false }) } + + that.registerMerchant = function () { + $state.go('shopify.register'); + } }]); module.controller('ShopifyLoginController', ['$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) { diff --git a/src/main/ui/static/shopify/auth/templates/shopify_auth.html b/src/main/ui/static/shopify/auth/templates/shopify_auth.html index 53236e748..2573ea842 100644 --- a/src/main/ui/static/shopify/auth/templates/shopify_auth.html +++ b/src/main/ui/static/shopify/auth/templates/shopify_auth.html @@ -5,7 +5,7 @@ Title