Skip to content

Commit

Permalink
Refactored samples
Browse files Browse the repository at this point in the history
  • Loading branch information
sishbi committed Dec 18, 2024
1 parent d923e93 commit 485c10c
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 174 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ proto-repo/
.DS_Store
/src/
/test.env

samples/staging-config.properties
10 changes: 0 additions & 10 deletions samples/ecom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
<artifactId>kody-clientsdk-java</artifactId>
<version>0.0.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
Expand All @@ -45,10 +40,5 @@
<artifactId>protobuf-java</artifactId>
<version>4.29.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.5</version>
</dependency>
</dependencies>
</project>
72 changes: 39 additions & 33 deletions samples/ecom/src/main/java/com/kody/ExampleGetPaymentDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,61 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ExampleGetPaymentDetails {
private static final Logger LOG = LoggerFactory.getLogger(ExampleGetPaymentDetails.class);
private static final long TIMEOUT_MS = java.time.Duration.ofMinutes(3).toMillis();

public static void main(String[] args) {

Properties properties = loadProperties();
var address = properties.getProperty("address", "grpc-staging.kodypay.com");
var apiKey = properties.getProperty("apiKey");
if (apiKey == null) {
throw new IllegalArgumentException("Invalid config, expected apiKey");
}
var storeId = properties.getProperty("storeId");
var paymentId = properties.getProperty("paymentId");
//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";

Metadata metadata = new Metadata();
metadata.put(Metadata.Key.of("X-API-Key", Metadata.ASCII_STRING_MARSHALLER), apiKey);
public static void main(String[] args) {
//TODO: Replace this with your Store ID
String storeId = "STORE ID";
//TODO: Replace this with the payment ID received during payment initiation
String paymentId = "PAYMENT ID";

ManagedChannel channel = ManagedChannelBuilder
.forAddress(address, 443)
.idleTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS)
.keepAliveTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS)
.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata))
.build();
getEcomPaymentDetails(storeId, paymentId);
}

KodyEcomPaymentsServiceGrpc.KodyEcomPaymentsServiceBlockingStub paymentClient = KodyEcomPaymentsServiceGrpc.newBlockingStub(channel);
public static void getEcomPaymentDetails(String storeId, String paymentId) {
var paymentClient = createKodyEcomPaymentsClient();

PaymentDetailsRequest paymentDetailsRequest = PaymentDetailsRequest.newBuilder()
.setStoreId(storeId)
.setPaymentId(paymentId)
.build();

PaymentDetailsResponse payments = paymentClient.paymentDetails(paymentDetailsRequest);
LOG.info("PaymentDetailsResponse: {}", payments);
}
PaymentDetailsResponse.Response paymentDetails = paymentClient.paymentDetails(paymentDetailsRequest).getResponse();

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");
}
properties.load(inputStream);
} catch (Exception e) {
throw new RuntimeException("Failed to load configuration", e);
// Payment ID is Kody generated
System.out.println("Payment ID: " + paymentDetails.getPaymentId());
// Payment reference and order ID is set by client
System.out.println("Payment reference: " + paymentDetails.getPaymentReference());
System.out.println("Payment order ID: " + paymentDetails.getOrderId());
// Payment Status enumeration: PENDING, SUCCESS, FAILED, CANCELLED, EXPIRED, UNRECOGNIZED
System.out.println("Payment status: " + paymentDetails.getStatus());
System.out.println("Payment created timestamp: " + new Date(paymentDetails.getDateCreated().getSeconds() * 1000L));
if (paymentDetails.getStatus() == PaymentDetailsResponse.Response.PaymentStatus.SUCCESS) {
System.out.println("Payment paid timestamp: " + new Date(paymentDetails.getDatePaid().getSeconds() * 1000L));
}
return properties;
// Metadata sent by client in the payment initiation
System.out.println("Payment order metadata: " + paymentDetails.getOrderMetadata());
// Data related with payment method
System.out.println("Payment data: " + paymentDetails.getPaymentDataJson());
}

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());
}
}
81 changes: 38 additions & 43 deletions samples/ecom/src/main/java/com/kody/ExampleGetPayments.java
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());
}

}
89 changes: 41 additions & 48 deletions samples/ecom/src/main/java/com/kody/ExampleNewPayment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,67 @@
import com.kodypay.grpc.ecom.v1.KodyEcomPaymentsServiceGrpc;
import com.kodypay.grpc.ecom.v1.PaymentInitiationRequest;
import com.kodypay.grpc.ecom.v1.PaymentInitiationResponse;
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.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


public class ExampleNewPayment {
private static final Logger LOG = LoggerFactory.getLogger(ExampleNewPayment.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";

// Load configuration properties
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");
//TODO: Replace with your internal order ID
String orderId = "order_" + UUID.randomUUID();
//TODO: Replace with your internal payment reference
String paymentReference = "pay_" + UUID.randomUUID();

// Initialize PaymentClient
Metadata metadata = new Metadata();
metadata.put(Metadata.Key.of("X-API-Key", Metadata.ASCII_STRING_MARSHALLER), apiKey);
//TODO: Replace this with your store operating currency: ISO 4217
String currencyCode = "HKD";
//TODO: Set the payment amount in minor units
long amountMinorUnits = 1000;
//TODO: Set the URL where the payment page will redirect to after the user completes the payment
String returnUrl = "https://display-parameters.com/";

ManagedChannel channel = ManagedChannelBuilder
.forAddress(address, 443)
.idleTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS)
.keepAliveTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS)
.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata))
.build();
initiateEcomPayment(storeId, paymentReference, orderId, currencyCode, amountMinorUnits, returnUrl);
}

KodyEcomPaymentsServiceGrpc.KodyEcomPaymentsServiceBlockingStub paymentClient = KodyEcomPaymentsServiceGrpc.newBlockingStub(channel);
public static void initiateEcomPayment(String storeId, String orderId, String paymentReference,
String currencyCode, long amountMinorUnits, String returnUrl) {
var paymentClient = createKodyEcomPaymentsClient();

PaymentInitiationRequest paymentInitiationRequest = PaymentInitiationRequest.newBuilder()
.setStoreId(storeId)
.setPaymentReference(generatePaymentReference())
.setAmount(222)
.setCurrency("HKD")
.setOrderId("ORDERID")
.setReturnUrl("https://display-parameters.com/").build();
LOG.info("Send online payment");
.setPaymentReference(paymentReference)
.setAmount(amountMinorUnits)
.setCurrency(currencyCode)
.setOrderId(orderId)
.setReturnUrl(returnUrl)
.build();
System.out.println("Send online payment");

PaymentInitiationResponse paymentInitiationResponse = paymentClient.initiatePayment(paymentInitiationRequest);
LOG.info("paymentInitiationResponse: {}", paymentInitiationResponse);
System.out.println("paymentInitiationResponse.paymentUrl: " + paymentInitiationResponse.getResponse().getPaymentUrl());
System.out.println("paymentInitiationResponse.paymentId: " + paymentInitiationResponse.getResponse().getPaymentId());
}

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");
}
properties.load(inputStream);
} catch (Exception e) {
throw new RuntimeException("Failed to load configuration", e);
}
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());
}

}
}
Loading

0 comments on commit 485c10c

Please sign in to comment.