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 "