use parent shopify integration

master
Yixian 2 years ago
parent 45119c6e8e
commit 8bfb5077b5

@ -43,6 +43,10 @@
<groupId>au.com.royalpay.payment</groupId>
<artifactId>rpay-core</artifactId>
</dependency>
<dependency>
<groupId>au.com.royalpay.payment</groupId>
<artifactId>shopify-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.stuxuhai</groupId>
<artifactId>jpinyin</artifactId>

@ -1,9 +1,6 @@
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;
import au.com.royalpay.payment.manage.shopify.store.domain.entity.SimpleMerchant;
@ -14,6 +11,9 @@ 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 au.com.royalpay.shopify.entity.ShopifyAccessToken;
import au.com.royalpay.shopify.entity.ShopifyPermissionURL;
import au.com.royalpay.shopify.service.ShopifyAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

@ -1,17 +0,0 @@
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;
}

@ -1,20 +0,0 @@
package au.com.royalpay.payment.manage.shopify.auth.domain.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ShopifyCommonParameter {
private String shop;
private String code;
private String state;
private String hmac;
private String host;
private String timestamp;
}

@ -1,18 +0,0 @@
package au.com.royalpay.payment.manage.shopify.auth.domain.entity;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class ShopifyPermissionBackParam {
@NotBlank(message = "code not blank")
private String code;
private String hmac;
private String host;
@NotBlank(message = "shop not blank")
private String shop;
private String state;
private String timestamp;
}

@ -1,11 +0,0 @@
package au.com.royalpay.payment.manage.shopify.auth.domain.entity;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class ShopifyPermissionURL {
private String url;
}

@ -1,70 +0,0 @@
package au.com.royalpay.payment.manage.shopify.auth.domain.service;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyAccessToken;
import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyAccessTokenRequest;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyPermissionURL;
import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyPermissionRequest;
import au.com.royalpay.payment.tools.env.PlatformEnvironment;
import au.com.royalpay.payment.tools.exceptions.ServerErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
@Service
@Slf4j
public class ShopifyAuthService {
@Value("${shopify.auth.apiKey}")
private String clientId;
@Value("${shopify.auth.apiSecretKey}")
private String clientSecret;
@Value("${shopify.auth.scope}")
private String scope;
private static final String PERMISSION_URL = "https://%s/admin/oauth/authorize?client_id=%s&scope=%s&redirect_uri=%s&state=%s&grant_options[]=per-user";
private static final String ACCESS_TOKEN_URL = "https://%s/admin/oauth/access_token";
@Autowired
@Qualifier("shopifyRestTemplate")
private RestTemplate restTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
public ShopifyPermissionURL shopifyPermission(String shopifyStoreHost) {
String redirectUri = PlatformEnvironment.getEnv().concatUrl("/shopify/auth/back");
String state = String.valueOf(new Date().getTime()).substring(0,10);
stringRedisTemplate.boundValueOps("shopifyAuthState:"+shopifyStoreHost).set(state);
String permissionUrl = String.format(PERMISSION_URL, shopifyStoreHost, clientId, scope, redirectUri, state);
return ShopifyPermissionURL.builder().url(permissionUrl).build();
}
public ShopifyAccessToken getAccessToken(String shop, String code) {
ShopifyAccessTokenRequest request = new ShopifyAccessTokenRequest(clientId, clientSecret, code);
ResponseEntity<ShopifyAccessToken> responseEntity;
try {
responseEntity = restTemplate.postForEntity(String.format(ACCESS_TOKEN_URL, shop), request, ShopifyAccessToken.class);
} catch (RestClientException e) {
log.error(String.format("Shopify merchant [%s] get access token error: %s", shop, e.getMessage()));
throw new ServerErrorException(e.getMessage());
}
ShopifyAccessToken shopifyAccessToken = responseEntity.getBody();
log.info(String.format("Shopify merchant [%s] access token: %s", shop, shopifyAccessToken));
return shopifyAccessToken;
}
}

@ -1,31 +0,0 @@
package au.com.royalpay.payment.manage.shopify.auth.domain.service;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyCommonParameter;
import au.com.royalpay.payment.manage.shopify.support.HmacVerificationUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ShopifyRequestValidator {
@Value("${shopify.auth.apiSecretKey}")
private String clientSecret;
public Boolean valid(ShopifyCommonParameter parameter) {
StringBuilder message = new StringBuilder();
message.append("code=").append(parameter.getCode())
.append("&host=").append(parameter.getHost())
.append("&shop=").append(parameter.getShop())
.append("&state=").append(parameter.getState())
.append("&timestamp=").append(parameter.getTimestamp());
return HmacVerificationUtil.hmacSHA256(message.toString(), clientSecret, parameter.getHmac());
}
public boolean verifyPermission(String queryStrWithoutHmac, String hmac) {
return HmacVerificationUtil.hmacSHA256(queryStrWithoutHmac, clientSecret, hmac);
}
public boolean verify(String message, String hmac) {
return HmacVerificationUtil.hmacSHA256(message, clientSecret, hmac);
}
}

@ -1,8 +1,8 @@
package au.com.royalpay.payment.manage.shopify.auth.web;
import au.com.royalpay.payment.manage.shopify.auth.domain.application.ShopifyMerchantAuthApplication;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyAccessToken;
import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyPermissionRequest;
import au.com.royalpay.shopify.entity.ShopifyAccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@ -1,13 +1,13 @@
package au.com.royalpay.payment.manage.shopify.auth.web;
import au.com.royalpay.payment.manage.shopify.auth.domain.application.ShopifyMerchantAuthApplication;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyCommonParameter;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyPermissionURL;
import au.com.royalpay.payment.manage.shopify.auth.domain.exception.ShopifyRequestVerifyException;
import au.com.royalpay.payment.manage.shopify.auth.domain.service.ShopifyRequestValidator;
import au.com.royalpay.payment.manage.shopify.support.ShopifyEndpoint;
import au.com.royalpay.payment.tools.env.PlatformEnvironment;
import au.com.royalpay.payment.tools.exceptions.BadRequestException;
import au.com.royalpay.shopify.entity.ShopifyCommonParameter;
import au.com.royalpay.shopify.entity.ShopifyPermissionURL;
import au.com.royalpay.shopify.service.ShopifyRequestValidator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

@ -1,17 +0,0 @@
package au.com.royalpay.payment.manage.shopify.auth.web.command;
import lombok.Data;
@Data
public class ShopifyAccessTokenRequest {
private String client_id;
private String client_secret;
private String code;
public ShopifyAccessTokenRequest(String clientId, String clientSecret, String code) {
this.client_id = clientId;
this.client_secret = clientSecret;
this.code = code;
}
}

@ -1,30 +0,0 @@
package au.com.royalpay.payment.manage.shopify.config;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.ImmutableList;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
@Configuration
public class ShopifyApplicationConfig {
@Bean
@Qualifier("shopifyRestTemplate")
public RestTemplate restTemplate() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
fastJsonHttpMessageConverter.setSupportedMediaTypes(ImmutableList.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN));
return new RestTemplateBuilder()
.messageConverters(new ByteArrayHttpMessageConverter(),
new StringHttpMessageConverter(StandardCharsets.UTF_8),
fastJsonHttpMessageConverter)
.build();
}
}

@ -1,7 +1,6 @@
package au.com.royalpay.payment.manage.shopify.hooks;
import au.com.royalpay.payment.manage.shopify.auth.domain.exception.ShopifyRequestVerifyException;
import au.com.royalpay.payment.manage.shopify.auth.domain.service.ShopifyRequestValidator;
import au.com.royalpay.payment.manage.shopify.hooks.command.ShopifyCustomerRedactCommand;
import au.com.royalpay.payment.manage.shopify.hooks.command.ShopifyCustomerRequestCommand;
import au.com.royalpay.payment.manage.shopify.hooks.command.ShopifyShopRedactCommand;
@ -9,6 +8,7 @@ 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.ShopifyEndpoint;
import au.com.royalpay.payment.manage.shopify.support.ShopifyHttpUtils;
import au.com.royalpay.shopify.service.ShopifyRequestValidator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

@ -1,7 +1,6 @@
package au.com.royalpay.payment.manage.shopify.store.domain.application;
import au.com.royalpay.payment.manage.shopify.auth.domain.application.ShopifyMerchantAuthApplication;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyAccessToken;
import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyPermissionRequest;
import au.com.royalpay.payment.manage.shopify.store.domain.context.MerchantCreateContext;
import au.com.royalpay.payment.manage.shopify.store.domain.entity.MerchantAccountRequest;
@ -11,6 +10,7 @@ import au.com.royalpay.payment.manage.shopify.store.domain.service.MerchantAccou
import au.com.royalpay.payment.manage.shopify.store.domain.service.MerchantService;
import au.com.royalpay.payment.manage.shopify.store.web.command.CreateShopifyMerchantCommand;
import au.com.royalpay.payment.manage.shopify.support.PlatformMerchantProvider;
import au.com.royalpay.shopify.entity.ShopifyAccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

@ -1,6 +1,6 @@
package au.com.royalpay.payment.manage.shopify.store.domain.entity;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyAccessToken;
import au.com.royalpay.shopify.entity.ShopifyAccessToken;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -49,6 +49,7 @@ public class ShopifyStore {
.creator("shopify store")
.accessToken(accessToken.getAccess_token())
.scope(accessToken.getScope())
.modifyTime(new Date()).build();
.modifyTime(new Date())
.status(1).build();
}
}

@ -1,8 +1,8 @@
package au.com.royalpay.payment.manage.shopify.store.web;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyAccessToken;
import au.com.royalpay.payment.manage.shopify.store.domain.application.ShopifyStoreApplication;
import au.com.royalpay.payment.manage.shopify.store.web.command.CreateShopifyMerchantCommand;
import au.com.royalpay.shopify.entity.ShopifyAccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

Loading…
Cancel
Save