From 3d2747e6266bf8c78ac20c7bb7995ace28ef1e27 Mon Sep 17 00:00:00 2001 From: Komarudin Date: Mon, 13 Sep 2021 12:18:52 +0700 Subject: [PATCH 1/3] move authentication (3DS) param to backend --- examples/core-api/checkout-process.php | 70 +++----------- examples/core-api/checkout.php | 127 +++++++++++++++---------- 2 files changed, 88 insertions(+), 109 deletions(-) diff --git a/examples/core-api/checkout-process.php b/examples/core-api/checkout-process.php index 2000984..0487cc6 100644 --- a/examples/core-api/checkout-process.php +++ b/examples/core-api/checkout-process.php @@ -1,13 +1,12 @@ '; // Uncomment for append and override notification URL // Config::$appendNotifUrl = "https://example.com"; @@ -86,14 +85,17 @@ // Token ID from checkout page $token_id = $_POST['token_id']; +$authentication = isset($_POST['secure']); +$save_token_id = isset($_POST['save_cc']); // Transaction data to be sent $transaction_data = array( 'payment_type' => 'credit_card', 'credit_card' => array( - 'token_id' => $token_id, - // 'bank' => 'bni', // optional acquiring bank, must be the same bank with get-token bank - 'save_token_id' => isset($_POST['save_cc']) + 'token_id' => $token_id, + 'authentication' => $authentication, + // 'bank' => 'bni', // optional acquiring bank + 'save_token_id' => $save_token_id ), 'transaction_details' => $transaction_details, 'item_details' => $items, @@ -102,56 +104,8 @@ try { $response = CoreApi::charge($transaction_data); -} catch (Exception $e) { + header('Content-Type: application/json'); + echo json_encode($response); +} catch (\Exception $e) { echo $e->getMessage(); - die(); -} - -// Success -if ($response->transaction_status == 'capture') { - echo "

Transaksi berhasil.

"; - echo "

Status transaksi untuk order id $response->order_id: " . - "$response->transaction_status

"; - - echo "

Detail transaksi:

"; - echo "
";
-    var_dump($response);
-    echo "
"; } -// Deny -else if ($response->transaction_status == 'deny') { - echo "

Transaksi ditolak.

"; - echo "

Status transaksi untuk order id .$response->order_id: " . - "$response->transaction_status

"; - echo "

Detail transaksi:

"; - echo "
";
-    var_dump($response);
-    echo "
"; -} -// Challenge -else if ($response->transaction_status == 'challenge') { - echo "

Transaksi challenge.

"; - echo "

Status transaksi untuk order id $response->order_id: " . - "$response->transaction_status

"; - - echo "

Detail transaksi:

"; - echo "
";
-    var_dump($response);
-    echo "
"; -} -// Error -else { - echo "

Terjadi kesalahan pada data transaksi yang dikirim.

"; - echo "

Status message: [$response->status_code] " . - "$response->status_message

"; - - echo "
";
-    var_dump($response);
-    echo "
"; -} - -echo "
"; -echo "

Request

"; -echo "
";
-var_dump($response);
-echo "
"; diff --git a/examples/core-api/checkout.php b/examples/core-api/checkout.php index fb37298..ce7a317 100644 --- a/examples/core-api/checkout.php +++ b/examples/core-api/checkout.php @@ -1,17 +1,22 @@ Settings -> Access keys -Config::$clientKey = ""; - +Config::$clientKey = ''; if (strpos(Config::$clientKey, 'your ') != false ) { - echo "

"; - echo "Please set your client key in file " . __FILE__; - echo "

"; + echo ""; + echo "

Please set your server key from sandbox

"; + echo "In file: " . __FILE__; + echo "
"; + echo "
"; + echo htmlspecialchars('Config::$clientKey = \'\';'); + die(); } ?> @@ -22,7 +27,7 @@ - + @@ -33,29 +38,27 @@ Field that may be presented to customer:

- +

- + / - +

- +

- +

- Fields that shouldn't be presented to the customer:

- +

- @@ -84,35 +87,24 @@ From 126bb3353e8d5dc0bdb6e7b3f7511c40efbc0bd2 Mon Sep 17 00:00:00 2001 From: Komarudin Date: Mon, 13 Sep 2021 12:19:24 +0700 Subject: [PATCH 2/3] add notes and add try catch --- examples/core-api/tokenization-process.php | 24 +++++++++++++-- .../core-api/transaction-manipulation.php | 7 +++-- examples/notification-handler.php | 23 ++++++++++++-- examples/snap-redirect/checkout-process.php | 19 ++++++++++-- .../snap/checkout-process-simple-version.php | 29 +++++++++++++++--- examples/snap/checkout-process.php | 30 +++++++++++++++---- 6 files changed, 112 insertions(+), 20 deletions(-) diff --git a/examples/core-api/tokenization-process.php b/examples/core-api/tokenization-process.php index 7942c6b..fcf80e8 100644 --- a/examples/core-api/tokenization-process.php +++ b/examples/core-api/tokenization-process.php @@ -1,9 +1,21 @@ "; +// Set Your server key +// can find in Merchant Portal -> Settings -> Access keys +Config::$serverKey = ''; +if (strpos(Config::$serverKey, 'your ') != false ) { + echo ""; + echo "

Please set your server key from sandbox

"; + echo "In file: " . __FILE__; + echo "
"; + echo "
"; + echo htmlspecialchars('Config::$serverKey = \'\';'); + die(); +} // define variables and set to empty values $number = ""; @@ -21,7 +33,13 @@ ) ); -$response = CoreApi::linkPaymentAccount($params); +try { + $response = CoreApi::linkPaymentAccount($params); +} catch (\Exception $e) { + echo $e->getMessage(); + die(); +} + ?> diff --git a/examples/core-api/transaction-manipulation.php b/examples/core-api/transaction-manipulation.php index 356732c..846badb 100644 --- a/examples/core-api/transaction-manipulation.php +++ b/examples/core-api/transaction-manipulation.php @@ -1,11 +1,12 @@ Settings -> Access keys Config::$serverKey = ''; - if (strpos(Config::$serverKey, 'your ') != false ) { echo ""; echo "

Please set your server key from sandbox

"; @@ -21,7 +22,7 @@ // Get transaction status to Midtrans API try { $status = Transaction::status($orderId); -} catch (Exception $e) { +} catch (\Exception $e) { echo $e->getMessage(); die(); } diff --git a/examples/notification-handler.php b/examples/notification-handler.php index 13fe318..1f05b3c 100644 --- a/examples/notification-handler.php +++ b/examples/notification-handler.php @@ -1,12 +1,31 @@ '; -$notif = new Notification(); +Config::$serverKey = ''; +if (strpos(Config::$serverKey, 'your ') != false ) { + echo ""; + echo "

Please set your server key from sandbox

"; + echo "In file: " . __FILE__; + echo "
"; + echo "
"; + echo htmlspecialchars('Config::$serverKey = \'\';'); + die(); +} + +try { + $notif = new Notification(); +} +catch (\Exception $e) { + exit($e->getMessage()); +} +$notif = $notif->getResponse(); $transaction = $notif->transaction_status; $type = $notif->payment_type; $order_id = $notif->order_id; diff --git a/examples/snap-redirect/checkout-process.php b/examples/snap-redirect/checkout-process.php index 5b5fcbc..4eb6aec 100644 --- a/examples/snap-redirect/checkout-process.php +++ b/examples/snap-redirect/checkout-process.php @@ -1,10 +1,23 @@ "; +// Set Your server key +// can find in Merchant Portal -> Settings -> Access keys +Config::$serverKey = ''; +if (strpos(Config::$serverKey, 'your ') != false ) { + echo ""; + echo "

Please set your server key from sandbox

"; + echo "In file: " . __FILE__; + echo "
"; + echo "
"; + echo htmlspecialchars('Config::$serverKey = \'\';'); + die(); +} // Uncomment for production environment // Config::$isProduction = true; @@ -86,6 +99,6 @@ // Redirect to Snap Payment Page header('Location: ' . $paymentUrl); } -catch (Exception $e) { +catch (\Exception $e) { echo $e->getMessage(); } diff --git a/examples/snap/checkout-process-simple-version.php b/examples/snap/checkout-process-simple-version.php index fe896e3..ee53703 100644 --- a/examples/snap/checkout-process-simple-version.php +++ b/examples/snap/checkout-process-simple-version.php @@ -1,10 +1,25 @@ "; +// Set Your server key +// can find in Merchant Portal -> Settings -> Access keys +Config::$serverKey = ''; +if (strpos(Config::$serverKey, 'your ') != false ) { + echo ""; + echo "

Please set your server key from sandbox

"; + echo "In file: " . __FILE__; + echo "
"; + echo "
"; + echo htmlspecialchars('Config::$serverKey = \'\';'); + die(); +} +$client_key = ''; + // Uncomment for production environment // Config::$isProduction = true; Config::$isSanitized = Config::$is3ds = true; @@ -39,7 +54,13 @@ 'item_details' => $item_details, ); -$snapToken = Snap::getSnapToken($transaction); +$snapToken = ''; +try { + $snapToken = Snap::getSnapToken($transaction); +} +catch (\Exception $e) { + echo $e->getMessage(); +} echo "snapToken = ".$snapToken; ?> @@ -48,7 +69,7 @@ - + + + diff --git a/examples/snap/checkout-process.php b/examples/snap/checkout-process.php index 09a1c36..57a8a27 100644 --- a/examples/snap/checkout-process.php +++ b/examples/snap/checkout-process.php @@ -9,16 +9,10 @@ // Set Your server key // can find in Merchant Portal -> Settings -> Access keys Config::$serverKey = ''; -if (strpos(Config::$serverKey, 'your ') != false ) { - echo ""; - echo "

Please set your server key from sandbox

"; - echo "In file: " . __FILE__; - echo "
"; - echo "
"; - echo htmlspecialchars('Config::$serverKey = \'\';'); - die(); -} -$client_key = ''; +Config::$clientKey = ''; + +// non-relevant function only used for demo/example purpose +printExampleWarningMessage(); // Uncomment for production environment // Config::$isProduction = true; @@ -101,15 +95,28 @@ 'item_details' => $item_details, ); -$snapToken = ''; +$snap_token = ''; try { - $snapToken = Snap::getSnapToken($transaction); + $snap_token = Snap::getSnapToken($transaction); } catch (\Exception $e) { echo $e->getMessage(); } -echo "snapToken = ".$snapToken; +echo "snapToken = ".$snap_token; + +function printExampleWarningMessage() { + if (strpos(Config::$serverKey, 'your ') != false ) { + echo ""; + echo "

Please set your server key from sandbox

"; + echo "In file: " . __FILE__; + echo "
"; + echo "
"; + echo htmlspecialchars('Config::$serverKey = \'\';'); + die(); + } +} + ?> @@ -119,11 +126,11 @@
JSON result will appear here after payment:
- +