|
| 1 | +package com.truelayer.java; |
| 2 | + |
| 3 | +import static org.apache.commons.lang3.ObjectUtils.isEmpty; |
| 4 | + |
| 5 | +import com.truelayer.java.entities.ResourceType; |
| 6 | +import java.net.URI; |
| 7 | +import java.text.MessageFormat; |
| 8 | +import lombok.AccessLevel; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | + |
| 11 | +@RequiredArgsConstructor(access = AccessLevel.PACKAGE) |
| 12 | +public class HostedPaymentPageLinkBuilder { |
| 13 | + private final Environment environment; |
| 14 | + |
| 15 | + private ResourceType resourceType = ResourceType.PAYMENT; |
| 16 | + private String resourceId; |
| 17 | + private String resourceToken; |
| 18 | + private URI returnUri; |
| 19 | + private Integer maxWaitForResultSeconds; |
| 20 | + private Boolean signup; |
| 21 | + |
| 22 | + public HostedPaymentPageLinkBuilder resourceType(ResourceType resourceType) { |
| 23 | + this.resourceType = resourceType; |
| 24 | + return this; |
| 25 | + } |
| 26 | + |
| 27 | + public HostedPaymentPageLinkBuilder resourceId(String resourceId) { |
| 28 | + this.resourceId = resourceId; |
| 29 | + return this; |
| 30 | + } |
| 31 | + |
| 32 | + public HostedPaymentPageLinkBuilder resourceToken(String resourceToken) { |
| 33 | + this.resourceToken = resourceToken; |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + public HostedPaymentPageLinkBuilder returnUri(URI returnUri) { |
| 38 | + this.returnUri = returnUri; |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + public HostedPaymentPageLinkBuilder maxWaitForResultSeconds(int maxWaitForResultSeconds) { |
| 43 | + this.maxWaitForResultSeconds = maxWaitForResultSeconds; |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public HostedPaymentPageLinkBuilder signup(boolean signup) { |
| 48 | + this.signup = signup; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + public URI build() { |
| 53 | + if (isEmpty(this.resourceId)) { |
| 54 | + throw new TrueLayerException("resource_id must be set"); |
| 55 | + } |
| 56 | + |
| 57 | + if (isEmpty(resourceToken)) { |
| 58 | + throw new TrueLayerException("resource_token must be set"); |
| 59 | + } |
| 60 | + |
| 61 | + if (isEmpty(returnUri) || isEmpty(returnUri.toString())) { |
| 62 | + throw new TrueLayerException("return_uri must be set"); |
| 63 | + } |
| 64 | + |
| 65 | + URI hppLink = URI.create(MessageFormat.format( |
| 66 | + "{0}/{1}#{2}={3}&resource_token={4}&return_uri={5}", |
| 67 | + environment.getHppUri(), |
| 68 | + resourceType.getHppLinkPath(), |
| 69 | + resourceType.getHppLinkQueryParameter(), |
| 70 | + resourceId, |
| 71 | + resourceToken, |
| 72 | + returnUri)); |
| 73 | + |
| 74 | + if (!isEmpty(maxWaitForResultSeconds)) { |
| 75 | + hppLink = URI.create(MessageFormat.format("{0}&max_wait_for_result={1}", hppLink, maxWaitForResultSeconds)); |
| 76 | + } |
| 77 | + |
| 78 | + if (!isEmpty(signup)) { |
| 79 | + hppLink = URI.create(MessageFormat.format("{0}&signup={1}", hppLink, signup)); |
| 80 | + } |
| 81 | + |
| 82 | + return hppLink; |
| 83 | + } |
| 84 | +} |
0 commit comments