-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds payment sessions support (#383)
- Loading branch information
1 parent
141828f
commit 1c6662d
Showing
11 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.checkout.payments.sessions; | ||
|
||
import com.checkout.common.Address; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public final class Billing { | ||
|
||
private Address address; | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/checkout/payments/sessions/PaymentMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.checkout.payments.sessions; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public final class PaymentMethods { | ||
|
||
private String type; | ||
|
||
@SerializedName("card_schemes") | ||
private List<String> cardSchemes; | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/checkout/payments/sessions/PaymentSessionsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.checkout.payments.sessions; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public interface PaymentSessionsClient { | ||
|
||
CompletableFuture<PaymentSessionsResponse> requestPaymentSessions(PaymentSessionsRequest paymentSessionsRequest); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/checkout/payments/sessions/PaymentSessionsClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.checkout.payments.sessions; | ||
|
||
import com.checkout.AbstractClient; | ||
import com.checkout.ApiClient; | ||
import com.checkout.CheckoutConfiguration; | ||
import com.checkout.SdkAuthorizationType; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static com.checkout.common.CheckoutUtils.validateParams; | ||
|
||
public class PaymentSessionsClientImpl extends AbstractClient implements PaymentSessionsClient { | ||
|
||
private static final String PAYMENT_SESSIONS_PATH = "payment-sessions"; | ||
|
||
public PaymentSessionsClientImpl(final ApiClient apiClient, final CheckoutConfiguration configuration) { | ||
super(apiClient, configuration, SdkAuthorizationType.SECRET_KEY); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<PaymentSessionsResponse> requestPaymentSessions(final PaymentSessionsRequest paymentSessionsRequest) { | ||
|
||
validateParams("paymentSessionsRequest", paymentSessionsRequest); | ||
|
||
return apiClient.postAsync(PAYMENT_SESSIONS_PATH, sdkAuthorization(), PaymentSessionsResponse.class, paymentSessionsRequest, null); | ||
|
||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/checkout/payments/sessions/PaymentSessionsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.checkout.payments.sessions; | ||
|
||
import com.checkout.common.Currency; | ||
import com.checkout.common.CustomerRequest; | ||
import com.google.gson.annotations.SerializedName; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public final class PaymentSessionsRequest { | ||
|
||
private Long amount; | ||
|
||
private Currency currency; | ||
|
||
private String reference; | ||
|
||
private Billing billing; | ||
|
||
private CustomerRequest customer; | ||
|
||
@SerializedName("success_url") | ||
private String successUrl; | ||
|
||
@SerializedName("failure_url") | ||
private String failureUrl; | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/checkout/payments/sessions/PaymentSessionsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.checkout.payments.sessions; | ||
|
||
import com.checkout.common.Currency; | ||
import com.checkout.common.CustomerResponse; | ||
import com.checkout.common.Resource; | ||
import com.google.gson.annotations.SerializedName; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.ToString; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
@ToString(callSuper = true) | ||
public final class PaymentSessionsResponse extends Resource { | ||
|
||
private String id; | ||
|
||
private Long amount; | ||
|
||
private String locale; | ||
|
||
private Currency currency; | ||
|
||
private CustomerResponse customer; | ||
|
||
@SerializedName("payment_methods") | ||
private List<PaymentMethods> paymentMethods; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/com/checkout/payments/sessions/PaymentSessionsClientImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.checkout.payments.sessions; | ||
|
||
import com.checkout.ApiClient; | ||
import com.checkout.CheckoutConfiguration; | ||
import com.checkout.SdkAuthorization; | ||
import com.checkout.SdkAuthorizationType; | ||
import com.checkout.SdkCredentials; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isNull; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class PaymentSessionsClientImplTest { | ||
|
||
private PaymentSessionsClient client; | ||
|
||
@Mock | ||
private ApiClient apiClient; | ||
|
||
@Mock | ||
private CheckoutConfiguration configuration; | ||
|
||
@Mock | ||
private SdkCredentials sdkCredentials; | ||
|
||
@Mock | ||
private SdkAuthorization authorization; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
when(sdkCredentials.getAuthorization(SdkAuthorizationType.SECRET_KEY)).thenReturn(authorization); | ||
when(configuration.getSdkCredentials()).thenReturn(sdkCredentials); | ||
client = new PaymentSessionsClientImpl(apiClient, configuration); | ||
} | ||
|
||
@Test | ||
void shouldRequestPaymentSessions() throws ExecutionException, InterruptedException { | ||
|
||
final PaymentSessionsRequest request = mock(PaymentSessionsRequest.class); | ||
final PaymentSessionsResponse response = mock(PaymentSessionsResponse.class); | ||
|
||
when(apiClient.postAsync(eq("payment-sessions"), eq(authorization), eq(PaymentSessionsResponse.class), | ||
eq(request), isNull())) | ||
.thenReturn(CompletableFuture.completedFuture(response)); | ||
|
||
final CompletableFuture<PaymentSessionsResponse> future = client.requestPaymentSessions(request); | ||
|
||
assertNotNull(future.get()); | ||
assertEquals(response, future.get()); | ||
|
||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/com/checkout/payments/sessions/PaymentSessionsTestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.checkout.payments.sessions; | ||
|
||
import com.checkout.PlatformType; | ||
import com.checkout.SandboxTestFixture; | ||
import com.checkout.common.Currency; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.checkout.TestHelper.createAddress; | ||
import static com.checkout.TestHelper.createCustomer; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
class PaymentSessionsTestIT extends SandboxTestFixture { | ||
|
||
PaymentSessionsTestIT() { | ||
super(PlatformType.DEFAULT); | ||
} | ||
|
||
@Test | ||
void shouldMakeAPaymentSessionsRequest() { | ||
|
||
final Billing billing = Billing.builder() | ||
.address(createAddress()) | ||
.build(); | ||
|
||
final PaymentSessionsRequest request = PaymentSessionsRequest.builder() | ||
.amount(1000L) | ||
.currency(Currency.GBP) | ||
.reference("ORD-123A") | ||
.billing(billing) | ||
.customer(createCustomer()) | ||
.successUrl("https://example.com/payments/success") | ||
.failureUrl("https://example.com/payments/failure") | ||
.build(); | ||
|
||
final PaymentSessionsResponse response = blocking(() -> checkoutApi.paymentSessionsClient().requestPaymentSessions(request)); | ||
|
||
assertNotNull(response); | ||
assertNotNull(response.getId()); | ||
assertEquals("en-GB", response.getLocale()); | ||
assertEquals(Currency.GBP, response.getCurrency()); | ||
assertNotNull(response.getPaymentMethods()); | ||
assertNotNull(response.getLinks()); | ||
|
||
} | ||
|
||
} |