upgrade shopify OAuth processing

master
ycfxx 3 years ago
parent 0d4f9a848f
commit 480bc50442

@ -231,6 +231,11 @@
<version>0.1.55</version> <version>0.1.55</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

@ -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.ShopifyAccessToken;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyPermissionURL; 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.domain.service.ShopifyAuthService;
import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyPermissionRequest; import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyPermissionRequest;
import au.com.royalpay.payment.manage.shopify.store.domain.entity.ShopifyStore; 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.beans.LoginInfo;
import au.com.royalpay.payment.manage.signin.core.SignInStatusManager; import au.com.royalpay.payment.manage.signin.core.SignInStatusManager;
import au.com.royalpay.payment.tools.exceptions.BadRequestException; 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.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Date; import java.util.Date;
@Slf4j
@Component @Component
public class ShopifyMerchantAuthApplication { 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 @Autowired
private ShopifyAuthService shopifyAuthService; private ShopifyAuthService shopifyAuthService;
@ -36,6 +49,9 @@ public class ShopifyMerchantAuthApplication {
@Autowired @Autowired
private MerchantService merchantService; private MerchantService merchantService;
@Autowired
private PaymentsAppConfigureClient paymentsAppConfigureClient;
/** /**
* shopifyURL * shopifyURL
* *
@ -81,6 +97,15 @@ public class ShopifyMerchantAuthApplication {
throw new BadRequestException("Get access token error"); throw new BadRequestException("Get access token error");
} }
shopifyStoreService.modifyShopifyStore(shopifyStore.setAccessToken(accessToken.getAccess_token()).setScope(accessToken.getScope())); 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);
} }
} }

@ -1,11 +1,17 @@
package au.com.royalpay.payment.manage.shopify.auth.domain.entity; package au.com.royalpay.payment.manage.shopify.auth.domain.entity;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data @Data
@Accessors(chain = true)
@NoArgsConstructor
public class ShopifyAccessToken { public class ShopifyAccessToken {
private String access_token; private String access_token;
private String scope; private String scope;
private String redirectUrl;
} }

@ -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;
}

@ -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()));
}
}

@ -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;
}
}

@ -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());
}
}

@ -176,6 +176,7 @@ logging:
payment: debug payment: debug
shopify: shopify:
apiVersion: 2022-01
auth: auth:
apiKey: 99e631dd0faa1076ceffae36cf91a93b apiKey: 99e631dd0faa1076ceffae36cf91a93b
apiSecretKey: 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 apiSecretKey: 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

@ -0,0 +1,8 @@
mutation PaymentsAppConfigure($externalHandle: String, $ready: Boolean!) {
paymentsAppConfigure(externalHandle: $externalHandle, ready: $ready) {
userErrors{
field
message
}
}
}

@ -66,5 +66,14 @@
<span class="footer-font-color"><small>©2015-2021 RoyalPay.</small></span> <span class="footer-font-color"><small>©2015-2021 RoyalPay.</small></span>
</div> </div>
</footer> </footer>
<script type="text/javascript" data-th-inline="javascript">
let redirectUrl = /*[[${accessToken.redirectUrl}]]*/'';
function redirect() {
location.href = redirectUrl;
}
redirect()
</script>
</body> </body>
</html> </html>

@ -48,6 +48,10 @@ define(['angular', 'uiRouter', 'uiBootstrap'], function (angular) {
that.authDisable = false that.authDisable = false
}) })
} }
that.registerMerchant = function () {
$state.go('shopify.register');
}
}]); }]);
module.controller('ShopifyLoginController', ['$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) { module.controller('ShopifyLoginController', ['$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) {

@ -30,10 +30,17 @@
<div class="m-t-40"></div> <div class="m-t-40"></div>
<form> <form>
<div class="form-group form-group-lg"> <div class="form-group form-group-lg">
<input class="form-control input-lg" id="exampleInputEmail" placeholder="Partner Code" ng-model="store.partnerCode"> <input class="form-control input-lg" id="exampleInputEmail" placeholder="Partner Code"
ng-model="store.partnerCode">
</div> </div>
<button class="btn btn-warning btn-lg btn-block" ng-disabled="authDisable" ng-click="validStoreLoginId()">Next</button> <button class="btn btn-warning btn-lg btn-block" ng-disabled="authDisable"
ng-click="validStoreLoginId()">Next
</button>
</form> </form>
<div class="m-t-10"></div>
<span style="padding-top: 15px">
Not a RoyalPay merchant yet, <a href="" ng-click="registerMerchant()">please register</a>
</span>
</div> </div>
</div> </div>
</div> </div>

@ -0,0 +1,34 @@
package au.com.royalpay.payment.manage.shopify.auth.domain.graphqlclient;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles({"dev", "alipay", "bestpay", "jd", "wechat", "rpay", "yeepay", "rppaysvc", "common", "alipayplusaps"})
class PaymentsAppConfigureClientTest {
@Autowired
private PaymentsAppConfigureClient paymentsAppConfigureClient;
@Test
public void paymentsAppConfigureClientTest() {
try {
paymentsAppConfigureClient.paymentsAppConfigure("geek-test-shop.myshopify.com", true, "2022-01");
} catch (IOException e) {
log.error(String.format("PaymentsAppConfigure error: [%s]", e.getMessage()));
e.printStackTrace();
}
}
}
Loading…
Cancel
Save