From c50bf257a801e0bbcdaeaf6e60ede8dd6c7fc8a1 Mon Sep 17 00:00:00 2001 From: Joao Martins <> Date: Wed, 10 Jul 2024 14:21:38 +0800 Subject: [PATCH] New code for payment terminals --- .../ecom-server/public/checkout_payment.php | 2 +- .../public/terminal_payment_form.php | 2 +- .../public/terminal_payment_status_check.php | 42 ++++-- .../public/terminal_submit_payment.php | 98 +++++++----- samples/php8/pos/src/config.php | 21 --- samples/php8/pos/src/demo_payment.php | 141 ------------------ 6 files changed, 92 insertions(+), 214 deletions(-) delete mode 100644 samples/php8/pos/src/config.php delete mode 100644 samples/php8/pos/src/demo_payment.php diff --git a/samples/php8/ecom-server/public/checkout_payment.php b/samples/php8/ecom-server/public/checkout_payment.php index 265d46c..02f3efc 100644 --- a/samples/php8/ecom-server/public/checkout_payment.php +++ b/samples/php8/ecom-server/public/checkout_payment.php @@ -15,7 +15,7 @@ $request = new PaymentInitiationRequest(); $request->setStoreId($config['store_id']); $request->setPaymentReference($paymentReference); - $request->setAmount($_POST['amount']*100); + $request->setAmount($_POST['amount']); $request->setCurrency($_POST['currency']); $request->setOrderId($_POST['order_id']); $request->setReturnUrl($config['redirect_url'].'?order_id=$orderId'); diff --git a/samples/php8/ecom-server/public/terminal_payment_form.php b/samples/php8/ecom-server/public/terminal_payment_form.php index 588e844..9bf15de 100644 --- a/samples/php8/ecom-server/public/terminal_payment_form.php +++ b/samples/php8/ecom-server/public/terminal_payment_form.php @@ -71,7 +71,7 @@ function generateRandomOrderId($length = 8) { - + diff --git a/samples/php8/ecom-server/public/terminal_payment_status_check.php b/samples/php8/ecom-server/public/terminal_payment_status_check.php index d83e856..e8a5fe4 100644 --- a/samples/php8/ecom-server/public/terminal_payment_status_check.php +++ b/samples/php8/ecom-server/public/terminal_payment_status_check.php @@ -1,26 +1,36 @@ getDetails($orderId); - - echo json_encode([ - 'status' => $paymentDetails->getStatus(), - 'order_id' => $paymentDetails->getOrderId(), - 'date_created' => $paymentDetails->getDateCreated(), - 'date_paid' => $paymentDetails->getDatePaid(), - 'failure_reason' => $paymentDetails->getFailureReason(), - 'ext_payment_ref' => $paymentDetails->getExtPaymentRef(), - 'receipt_json' => $paymentDetails->getReceiptJson(), - ]); + $client = new KodyPayTerminalServiceClient($config['hostname'], ['credentials' => ChannelCredentials::createSsl()]); + $metadata = ['X-API-Key' => [$config['api_key']]]; + + $request = new PaymentDetailsRequest(); + $request->setStoreId($config['store_id']); + $request->setOrderId($orderId); + + list($response, $status) = $client->PaymentDetails($request, $metadata)->wait(); + + $status = $response->getStatus(); + $data = [ + 'status' => $status, + 'orderId' => $response->getOrderId(), + 'dateCreated' => $response->getDateCreated()->serializeToJsonString(), + 'datePaid' => $response->getDatePaid() ? $response->getDatePaid()->serializeToJsonString() : null, + 'failureReason' => $response->getFailureReason(), + 'extPaymentRef' => $response->getExtPaymentRef(), + 'receiptJson' => $response->getReceiptJson() + ]; + + echo json_encode($data); } else { - echo json_encode(['status' => 'ERROR', 'message' => 'Invalid request.']); + echo json_encode(['error' => 'Invalid request']); } diff --git a/samples/php8/ecom-server/public/terminal_submit_payment.php b/samples/php8/ecom-server/public/terminal_submit_payment.php index 8bba89e..0745d79 100644 --- a/samples/php8/ecom-server/public/terminal_submit_payment.php +++ b/samples/php8/ecom-server/public/terminal_submit_payment.php @@ -1,59 +1,89 @@ ChannelCredentials::createSsl()]); + $metadata = ['X-API-Key' => [$config['api_key']]]; // Sending initial payment request - $response = $client->sendPayment($amount, $terminalId, function ($orderId) { - $_SESSION['current_order_id'] = $orderId; - }); + $req = new PayRequest(); + $req->setStoreId($config['store_id']); + $req->setAmount(number_format($amount, 2, '.', '')); + $req->setTerminalId($_POST['terminal_id']); + + error_log("Sending request"); + $timeoutDateTime = (new DateTime())->add(new DateInterval('PT' . (3 * 60) . 'S')); + $call = $client->Pay($req, $metadata, ['timeout' => $timeoutDateTime]); - $orderId = $_SESSION['current_order_id'] ?? null; + error_log("Request submitted"); + + // Capture the orderId from the callback + $orderId = null; + foreach ($call->responses() as $reply) { + if ($reply->getStatus() === PaymentStatus::PENDING) { + $orderId = $reply->getOrderId(); + $_SESSION['current_order_id'] = $orderId; + break; + } + } if ($orderId) { echo "

Collecting payment for Order ID: $orderId

"; echo "
Loading... loading spinner
"; echo ""; } else { + error_log("Error: Unable to initiate payment."); echo "

Error: Unable to initiate payment.

"; } } else { + error_log("Error: Invalid request. Form parameters are missing."); echo "

Error: Invalid request.

"; } ?> - + setTimeout(checkPaymentStatus, 1000); + + diff --git a/samples/php8/pos/src/config.php b/samples/php8/pos/src/config.php deleted file mode 100644 index bf8755a..0000000 --- a/samples/php8/pos/src/config.php +++ /dev/null @@ -1,21 +0,0 @@ -load(); - -$requiredEnvVars = ['KODY_HOSTNAME', 'KODY_STORE_ID', 'KODY_API_KEY']; - -foreach ($requiredEnvVars as $envVar) { - if (empty(getenv($envVar))) { - throw new Exception("Environment variable $envVar is not set or empty"); - } -} - -return [ - 'hostname' => getenv('KODY_HOSTNAME') ?: 'grpc.kodypay.com', - 'store_id' => getenv('KODY_STORE_ID') ?: '', - 'api_key' => getenv('KODY_API_KEY') ?: '', -]; diff --git a/samples/php8/pos/src/demo_payment.php b/samples/php8/pos/src/demo_payment.php deleted file mode 100644 index f7079db..0000000 --- a/samples/php8/pos/src/demo_payment.php +++ /dev/null @@ -1,141 +0,0 @@ -store = $config['store_id']; - $this->apiKey = $config['api_key']; - $this->client = new KodyPayTerminalServiceClient($config['hostname'], [ - 'credentials' => ChannelCredentials::createSsl() - ]); - } - - /** - * Sends a payment request with the specified amount and terminal ID. - * - * @param float $amount The amount of the payment. - * @param string $terminalId The terminal ID. - * @param callable|null $orderIdCallback An optional callback function to be called with the generated order ID. - * @return PayResponse The payment response object. - */ - public function sendPayment(float $amount, string $terminalId, ?callable $orderIdCallback = null): PayResponse - { - $req = new PayRequest(); - $req->setStoreId($this->store); - $req->setAmount(number_format($amount, 2, '.', '')); - $req->setTerminalId($terminalId); - - $call = $this->client->Pay($req, $this->getApiKeyHeaders(), ['timeout' => $this->getTimeout()]); - $response = new PayResponse(); - $response->setStatus(PaymentStatus::PENDING); - - foreach ($call->responses() as $reply) { - if ($reply->getStatus() === PaymentStatus::PENDING) { - $response = $reply; - if ($orderIdCallback !== null) { - $orderIdCallback($response->getOrderId()); - } - } else { - $response = $reply; - break; - } - } - - return $response; - } - - /** - * Cancels a payment with the specified amount, terminal ID, and order ID. - * - * @param float $amount The amount of the payment to cancel. - * @param string $terminalId The ID of the terminal associated with the payment. - * @param string $orderId The ID of the order associated with the payment. - * @return PaymentStatus The status of the cancellation request. - */ - public function cancelPayment(float $amount, string $terminalId, string $orderId): PaymentStatus - { - $cancel = new CancelRequest(); - $cancel->setStoreId($this->store); - $cancel->setAmount(number_format($amount, 2, '.', '')); - $cancel->setTerminalId($terminalId); - $cancel->setOrderId($orderId); - - [$response, $status] = $this->client->Cancel($cancel, $this->getApiKeyHeaders(), ['timeout' => $this->getTimeout()])->wait(); - return $response->getStatus(); - } - - /** - * Retrieves a list of payment terminals. - * - * @return array A list of payment terminals. - */ - public function getTerminals(): array - { - $request = new TerminalsRequest(); - $request->setStoreId($this->store); - - [$response, $status] = $this->client->Terminals($request, $this->getApiKeyHeaders())->wait(); - return $response->getTerminals(); - } - - /** - * Retrieves payment details for a given order ID. - * - * @param string $orderId The ID of the order associated with the payment. - * @return PayResponse The payment response containing the payment details. - */ - public function getDetails(string $orderId): PayResponse - { - $request = new PaymentDetailsRequest(); - $request->setStoreId($this->store); - $request->setOrderId($orderId); - - [$response, $status] = $this->client->PaymentDetails($request, $this->getApiKeyHeaders(), ['timeout' => $this->getTimeout()])->wait(); - return $response; - } - - /** - * Gets the deadline for the request. - * - * @return Timeval The deadline for the request. - */ - private function getTimeout(): Timeval - { - return Timeval::fromSeconds(self::REQUEST_TIMEOUT_MINS * 60); - } - - /** - * Gets the API key headers for the request. - * - * @return array The API key headers. - */ - private function getApiKeyHeaders(): array - { - return ['X-API-Key' => [$this->apiKey]]; - } -} \ No newline at end of file