Skip to content

Commit

Permalink
Added show_timer and expiring_seconds fields (#10)
Browse files Browse the repository at this point in the history
* Added show_timer and expiring_seconds fields
* Restrict expiration_seconds to positive numbers only
* Hide expiration options with checkbox
  • Loading branch information
ioannis-kody authored Aug 27, 2024
1 parent d5ccee3 commit 3543f59
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 28 deletions.
6 changes: 3 additions & 3 deletions samples/php7/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "project",
"license": "MIT",
"require": {
"kody/kody-php7-grpc-client": "v1.2.*",
"kody/kody-php7-grpc-client": "v1.3.*",
"grpc/grpc": "^1.57",
"google/protobuf": "^3.19.1"
},
Expand All @@ -13,10 +13,10 @@
"type": "package",
"package": {
"name": "kody/kody-php7-grpc-client",
"version": "v1.2.2",
"version": "v1.3.1",
"dist": {
"type": "zip",
"url": "https://github.com/KodyPay/kody-clientsdk-php/releases/download/v1.2.2/kody-php7-grpc-package.zip"
"url": "https://github.com/KodyPay/kody-clientsdk-php/releases/download/v1.3.1/kody-php7-grpc-package.zip"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions samples/php7/src/get_list_of_terminals.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Com\Kodypay\Grpc\Pay\V1\TerminalsRequest;
use Grpc\ChannelCredentials;

$kody_api_hostname = 'grpc.kodypay.com';
$store_id = '5fa2dd05-1805-494d-b843-fa1a7c34cf8a'; // Use your Kody store ID
$kody_api_hostname = 'grpc-staging.kodypay.com';
$store_id = ''; // Use your Kody store ID
$api_key = ''; // Put your API key

$client = new KodyPayTerminalServiceClient($kody_api_hostname, ['credentials' => ChannelCredentials::createSsl()]);
Expand Down
12 changes: 6 additions & 6 deletions samples/php8/ecom-server/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
"type": "project",
"license": "MIT",
"require": {
"kody/kody-php8-grpc-client": "v1.3.0",
"grpc/grpc": "^1.57",
"google/protobuf": "^3.19.1",
"vlucas/phpdotenv": "^4.3"
"kody/kody-php8-grpc-client": "v1.3.*",
"grpc/grpc": "^1.57.0",
"google/protobuf": "^4.27.3",
"vlucas/phpdotenv": "^5.6.1"
},
"repositories": [
{
"type": "package",
"package": {
"name": "kody/kody-php8-grpc-client",
"version": "v1.3.0",
"version": "v1.3.1",
"dist": {
"type": "zip",
"url": "https://github.com/KodyPay/kody-clientsdk-php/releases/download/v1.3.0/kody-php8-grpc-package.zip"
"url": "https://github.com/KodyPay/kody-clientsdk-php/releases/download/v1.3.1/kody-php8-grpc-package.zip"
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions samples/php8/ecom-server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
php-fpm:
build:
Expand All @@ -11,6 +9,7 @@ services:
KODY_HOSTNAME: "grpc-staging.kodypay.com"
KODY_STORE_ID: "c4cebf51-b006-4bb9-acd5-bb4bcdbd6e09"
KODY_API_KEY: "0aY8Gqnx95WpmGnl6w8bxYO99As1vycBsb6soE_CQwd2"
PAYMENT_EXPIRING_SECONDS: 900

nginx:
image: nginx:latest
Expand Down
41 changes: 39 additions & 2 deletions samples/php8/ecom-server/public/checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
$randomAmount = rand(1, 1000);

// Generate a random order ID with 8 random letters and numbers
function generateRandomOrderId($length = 8) {
function generateRandomOrderId($length = 8)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomOrderId = '';
Expand All @@ -18,7 +19,6 @@ function generateRandomOrderId($length = 8) {
$randomOrderId = generateRandomOrderId();
?>

?>
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -31,10 +31,12 @@ function generateRandomOrderId($length = 8) {
margin: 0 auto;
padding: 20px;
}

label {
display: block;
margin-top: 10px;
}

input[type="number"],
input[type="text"],
input[type="url"] {
Expand All @@ -45,9 +47,11 @@ function generateRandomOrderId($length = 8) {
border: 1px solid #ccc;
border-radius: 4px;
}

.readonly {
background-color: #f9f9f9;
}

button {
padding: 10px 20px;
background-color: #4CAF50;
Expand All @@ -56,6 +60,7 @@ function generateRandomOrderId($length = 8) {
border-radius: 4px;
cursor: pointer;
}

button:hover {
background-color: #45a049;
}
Expand All @@ -73,15 +78,47 @@ function generateRandomOrderId($length = 8) {
<label for="order_id">Order ID:</label>
<input type="text" id="order_id" name="order_id" value="<?php echo $randomOrderId; ?>" required>

<div style="display: flex; align-items: flex-end; margin-bottom: 20px;">
<label for="enable_expiration">Enable expiration: </label>
<input type="checkbox" id="enable_expiration" name="enable_expiration" onchange="valueChanged()">
</div>

<div id="expiration_fields" style="display: none; margin-bottom: 20px;
padding: 20px; border: 1px solid #ccc; border-radius: 4px">
<label for="expiring_seconds">Expiring seconds:</label>
<input type="number" id="expiring_seconds" name="expiring_seconds" min="0" oninput="validity.valid||(value='');"
value="<?php echo $config['expiring_seconds']; ?>">

<label for="show_timer">Show timer:</label>
<div style="margin-top:5px;">
<select id="show_timer" name="show_timer">
<option value="true">true</option>
<option value="">false</option>
</select>
</div>
</div>

<input type="hidden" name="store_id" value="<?php echo htmlspecialchars($config['store_id']); ?>">

<button type="submit">Pay</button>
</form>

<script type="text/javascript">
function valueChanged() {
if (document.getElementById('enable_expiration').checked) {
document.getElementById("expiration_fields").style.display = 'block';
} else {
document.getElementById("expiration_fields").style.display = 'none';
}
}
</script>

<?php
if (isset($_GET['error'])) {
echo '<p style="color:red;">Error: ' . htmlspecialchars($_GET['error']) . '</p>';
}
?>

<h2>Developer Information</h2>
<p>This page demonstrates how to initiate a payment. The form above collects the necessary information and sends a payment request to the backend.</p>
<ul>
Expand Down
10 changes: 10 additions & 0 deletions samples/php8/ecom-server/public/checkout_payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@
$metadata = ['X-API-Key' => [$config['api_key']]];

$request = new PaymentInitiationRequest();

$request->setStoreId($config['store_id']);
$request->setPaymentReference($paymentReference);
$request->setAmount($_POST['amount']);
$request->setCurrency($_POST['currency']);
$request->setOrderId($_POST['order_id']);
$request->setReturnUrl($config['redirect_url'].'?order_id='.$_POST['order_id']);

if (isset($_POST['enable_expiration'])) {
$expiry = new PaymentInitiationRequest\ExpirySettings();

$expiry->setShowTimer($_POST['show_timer']);
$expiry->setExpiringSeconds($_POST['expiring_seconds']);

$request->setExpiry($expiry);
}

list($response, $status) = $client->InitiatePayment($request, $metadata)->wait();

if ($status->code === \Grpc\STATUS_OK) {
Expand Down
17 changes: 9 additions & 8 deletions samples/php8/ecom-server/public/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();

$requiredEnvVars = ['KODY_HOSTNAME', 'KODY_STORE_ID', 'KODY_API_KEY'];

foreach ($requiredEnvVars as $envVar) {
if (empty(getenv($envVar))) {
if (empty($_ENV[$envVar])) {
throw new Exception("Environment variable $envVar is not set or empty");
}
}

$config = [
'hostname' => getenv('KODY_HOSTNAME') ?: 'grpc.kodypay.com',
'store_id' => getenv('KODY_STORE_ID') ?: '',
'api_key' => getenv('KODY_API_KEY') ?: '',
'currency' => getenv('KODY_STORE_CURRENCY') ?: '',
'redirect_url' => getenv('PAYMENT_REDIRECT_URL') ?: '',
'hostname' => $_ENV['KODY_HOSTNAME'] ?: 'grpc-staging.kodypay.com',
'store_id' => $_ENV['KODY_STORE_ID'] ?: '',
'api_key' => $_ENV['KODY_API_KEY'] ?: '',
'currency' => $_ENV['KODY_STORE_CURRENCY'] ?: '',
'redirect_url' => $_ENV['PAYMENT_REDIRECT_URL'] ?: '',
'expiring_seconds' => $_ENV['PAYMENT_EXPIRING_SECONDS'] ?: '',
];

return $config;
10 changes: 5 additions & 5 deletions samples/php8/pos/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
"type": "project",
"license": "MIT",
"require": {
"kody/kody-php8-grpc-client": "v1.2.2",
"kody/kody-php8-grpc-client": "v1.3.*",
"grpc/grpc": "^1.57",
"google/protobuf": "^3.19.1",
"vlucas/phpdotenv": "^4.3"
"google/protobuf": "^4.27.3",
"vlucas/phpdotenv": "^5.6.1"
},
"repositories": [
{
"type": "package",
"package": {
"name": "kody/kody-php8-grpc-client",
"version": "v1.2.2",
"version": "v1.3.1",
"dist": {
"type": "zip",
"url": "https://github.com/KodyPay/kody-clientsdk-php/releases/download/v1.2.2/kody-php8-grpc-package.zip"
"url": "https://github.com/KodyPay/kody-clientsdk-php/releases/download/v1.3.1/kody-php8-grpc-package.zip"
}
}
}
Expand Down

0 comments on commit 3543f59

Please sign in to comment.