-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
174 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ proto-repo/ | |
.DS_Store | ||
/src/ | ||
/test.env | ||
|
||
samples/staging-config.properties |
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
81 changes: 38 additions & 43 deletions
81
samples/ecom/src/main/java/com/kody/ExampleGetPayments.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 |
---|---|---|
@@ -1,68 +1,63 @@ | ||
package com.kody; | ||
|
||
import com.kodypay.grpc.ecom.v1.*; | ||
import com.kodypay.grpc.ecom.v1.GetPaymentsRequest; | ||
import com.kodypay.grpc.ecom.v1.GetPaymentsResponse; | ||
import com.kodypay.grpc.ecom.v1.KodyEcomPaymentsServiceGrpc; | ||
import com.kodypay.grpc.sdk.common.PageCursor; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import io.grpc.Metadata; | ||
import io.grpc.stub.MetadataUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Properties; | ||
import java.util.UUID; | ||
import java.util.Date; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ExampleGetPayments { | ||
private static final Logger LOG = LoggerFactory.getLogger(ExampleGetPayments.class); | ||
private static final long TIMEOUT_MS = java.time.Duration.ofMinutes(3).toMillis(); | ||
//TODO: Replace this with the testing or live environment | ||
public static final String HOSTNAME = "grpc-staging.kodypay.com"; | ||
public static final String API_KEY = "API KEY"; | ||
|
||
public static void main(String[] args) { | ||
//TODO: Replace this with your Store ID | ||
String storeId = "STORE ID"; | ||
|
||
Properties properties = loadProperties(); | ||
var address = properties.getProperty("address", "grpc-developement.kodypay.com"); | ||
var apiKey = properties.getProperty("apiKey"); | ||
if (apiKey == null) { | ||
throw new IllegalArgumentException("Invalid config, expected apiKey"); | ||
} | ||
var storeId = properties.getProperty("storeId"); | ||
|
||
Metadata metadata = new Metadata(); | ||
metadata.put(Metadata.Key.of("X-API-Key", Metadata.ASCII_STRING_MARSHALLER), apiKey); | ||
|
||
ManagedChannel channel = ManagedChannelBuilder | ||
.forAddress(address, 443) | ||
.idleTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS) | ||
.keepAliveTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS) | ||
.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata)) | ||
.build(); | ||
getEcomPayments(storeId); | ||
} | ||
|
||
KodyEcomPaymentsServiceGrpc.KodyEcomPaymentsServiceBlockingStub paymentClient = KodyEcomPaymentsServiceGrpc.newBlockingStub(channel); | ||
public static void getEcomPayments(String storeId) { | ||
var paymentClient = createKodyEcomPaymentsClient(); | ||
|
||
GetPaymentsRequest getPaymentsRequest = GetPaymentsRequest.newBuilder() | ||
.setStoreId(storeId) | ||
.setPageCursor(PageCursor.newBuilder().setPageSize(1)).build(); | ||
LOG.info("getPayment"); | ||
.setPageCursor(PageCursor.newBuilder().setPageSize(10)).build(); | ||
|
||
GetPaymentsResponse payments = paymentClient.getPayments(getPaymentsRequest); | ||
LOG.info("GetPaymentsResponse: {}", payments); | ||
} | ||
|
||
private static Properties loadProperties() { | ||
Properties properties = new Properties(); | ||
try (var inputStream = ExampleNewPayment.class.getClassLoader().getResourceAsStream("config.properties")) { | ||
if (inputStream == null) { | ||
throw new IllegalArgumentException("Config file 'config.properties' not found in resources folder"); | ||
for (GetPaymentsResponse.Response.PaymentDetails payment : payments.getResponse().getPaymentsList()) { | ||
// Payment ID is Kody generated | ||
System.out.println("Payment ID: " + payment.getPaymentId()); | ||
// Payment reference and order ID is set by client | ||
System.out.println("Payment reference: " + payment.getPaymentReference()); | ||
System.out.println("Payment order ID: " + payment.getOrderId()); | ||
// Payment Status enumeration: PENDING, SUCCESS, FAILED, CANCELLED, EXPIRED, UNRECOGNIZED | ||
System.out.println("Payment status: " + payment.getStatus()); | ||
System.out.println("Payment created timestamp: " + new Date(payment.getDateCreated().getSeconds() * 1000L)); | ||
if (payment.getStatus() == GetPaymentsResponse.Response.PaymentDetails.PaymentStatus.SUCCESS) { | ||
System.out.println("Payment paid timestamp: " + new Date(payment.getDatePaid().getSeconds() * 1000L)); | ||
} | ||
properties.load(inputStream); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to load configuration", e); | ||
// Metadata sent by client in the payment initiation | ||
System.out.println("Payment order metadata: " + payment.getOrderMetadata()); | ||
// Data related with payment method | ||
System.out.println("Payment data: " + payment.getPaymentDataJson()); | ||
} | ||
return properties; | ||
} | ||
|
||
private static String generatePaymentReference() { | ||
return "pay_" + UUID.randomUUID(); | ||
private static KodyEcomPaymentsServiceGrpc.KodyEcomPaymentsServiceBlockingStub createKodyEcomPaymentsClient() { | ||
Metadata metadata = new Metadata(); | ||
metadata.put(Metadata.Key.of("X-API-Key", Metadata.ASCII_STRING_MARSHALLER), API_KEY); | ||
return KodyEcomPaymentsServiceGrpc.newBlockingStub(ManagedChannelBuilder | ||
.forAddress(HOSTNAME, 443) | ||
.idleTimeout(3, TimeUnit.MINUTES) | ||
.keepAliveTimeout(3, TimeUnit.MINUTES) | ||
.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata)) | ||
.build()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.