Skip to content

Commit

Permalink
add notes and add try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
omarxp committed Sep 13, 2021
1 parent 3d2747e commit 126bb33
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 20 deletions.
24 changes: 21 additions & 3 deletions examples/core-api/tokenization-process.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
<?php
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.

namespace Midtrans;

require_once dirname(__FILE__) . '/../../Midtrans.php';
//Set Your server key
Config::$serverKey = "<your server key>";
// Set Your server key
// can find in Merchant Portal -> Settings -> Access keys
Config::$serverKey = '<your server key>';
if (strpos(Config::$serverKey, 'your ') != false ) {
echo "<code>";
echo "<h4>Please set your server key from sandbox</h4>";
echo "In file: " . __FILE__;
echo "<br>";
echo "<br>";
echo htmlspecialchars('Config::$serverKey = \'<your server key>\';');
die();
}

// define variables and set to empty values
$number = "";
Expand All @@ -21,7 +33,13 @@
)
);

$response = CoreApi::linkPaymentAccount($params);
try {
$response = CoreApi::linkPaymentAccount($params);
} catch (\Exception $e) {
echo $e->getMessage();
die();
}

?>

<!DOCTYPE HTML>
Expand Down
7 changes: 4 additions & 3 deletions examples/core-api/transaction-manipulation.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.

namespace Midtrans;

require_once dirname(__FILE__) . '/../../Midtrans.php';

// Set Your server key
// can find in Merchant Portal -> Settings -> Access keys
Config::$serverKey = '<your server key>';

if (strpos(Config::$serverKey, 'your ') != false ) {
echo "<code>";
echo "<h4>Please set your server key from sandbox</h4>";
Expand All @@ -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();
}
Expand Down
23 changes: 21 additions & 2 deletions examples/notification-handler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
<?php
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
// Please refer to this docs for sample HTTP notifications:
// https://docs.midtrans.com/en/after-payment/http-notification?id=sample-of-different-payment-channels

namespace Midtrans;

require_once dirname(__FILE__) . '/../Midtrans.php';
Config::$isProduction = false;
Config::$serverKey = '<your serverkey>';
$notif = new Notification();
Config::$serverKey = '<your server key>';
if (strpos(Config::$serverKey, 'your ') != false ) {
echo "<code>";
echo "<h4>Please set your server key from sandbox</h4>";
echo "In file: " . __FILE__;
echo "<br>";
echo "<br>";
echo htmlspecialchars('Config::$serverKey = \'<your server key>\';');
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;
Expand Down
19 changes: 16 additions & 3 deletions examples/snap-redirect/checkout-process.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<?php
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
// Please refer to this docs for snap-redirect:
// https://docs.midtrans.com/en/snap/integration-guide?id=alternative-way-to-display-snap-payment-page-via-redirect

namespace Midtrans;

require_once dirname(__FILE__) . '/../../Midtrans.php';
//Set Your server key
Config::$serverKey = "<your server key>";
// Set Your server key
// can find in Merchant Portal -> Settings -> Access keys
Config::$serverKey = '<your server key>';
if (strpos(Config::$serverKey, 'your ') != false ) {
echo "<code>";
echo "<h4>Please set your server key from sandbox</h4>";
echo "In file: " . __FILE__;
echo "<br>";
echo "<br>";
echo htmlspecialchars('Config::$serverKey = \'<your server key>\';');
die();
}

// Uncomment for production environment
// Config::$isProduction = true;
Expand Down Expand Up @@ -86,6 +99,6 @@
// Redirect to Snap Payment Page
header('Location: ' . $paymentUrl);
}
catch (Exception $e) {
catch (\Exception $e) {
echo $e->getMessage();
}
29 changes: 25 additions & 4 deletions examples/snap/checkout-process-simple-version.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
<?php
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
// Please refer to this docs for snap popup:
// https://docs.midtrans.com/en/snap/integration-guide?id=integration-steps-overview

namespace Midtrans;

require_once dirname(__FILE__) . '/../../Midtrans.php';
//Set Your server key
Config::$serverKey = "<your server key>";
// Set Your server key
// can find in Merchant Portal -> Settings -> Access keys
Config::$serverKey = '<your server key>';
if (strpos(Config::$serverKey, 'your ') != false ) {
echo "<code>";
echo "<h4>Please set your server key from sandbox</h4>";
echo "In file: " . __FILE__;
echo "<br>";
echo "<br>";
echo htmlspecialchars('Config::$serverKey = \'<your server key>\';');
die();
}
$client_key = '<your client key>';

// Uncomment for production environment
// Config::$isProduction = true;
Config::$isSanitized = Config::$is3ds = true;
Expand Down Expand Up @@ -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;
?>

Expand All @@ -48,7 +69,7 @@
<body>
<button id="pay-button">Pay!</button>
<!-- TODO: Remove ".sandbox" from script src URL for production environment. Also input your client key in "data-client-key" -->
<script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="<Set your ClientKey here>"></script>
<script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="<?php echo $client_key;?>"></script>
<script type="text/javascript">
document.getElementById('pay-button').onclick = function(){
// SnapToken acquired from previous step
Expand Down
30 changes: 25 additions & 5 deletions examples/snap/checkout-process.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
<?php
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
// Please refer to this docs for snap popup:
// https://docs.midtrans.com/en/snap/integration-guide?id=integration-steps-overview

namespace Midtrans;

require_once dirname(__FILE__) . '/../../Midtrans.php';

//Set Your server key
Config::$serverKey = "<your server key>";
// Set Your server key
// can find in Merchant Portal -> Settings -> Access keys
Config::$serverKey = '<your server key>';
if (strpos(Config::$serverKey, 'your ') != false ) {
echo "<code>";
echo "<h4>Please set your server key from sandbox</h4>";
echo "In file: " . __FILE__;
echo "<br>";
echo "<br>";
echo htmlspecialchars('Config::$serverKey = \'<your server key>\';');
die();
}
$client_key = '<your client key>';

// Uncomment for production environment
// Config::$isProduction = true;
Expand Down Expand Up @@ -88,7 +101,14 @@
'item_details' => $item_details,
);

$snapToken = Snap::getSnapToken($transaction);
$snapToken = '';
try {
$snapToken = Snap::getSnapToken($transaction);
}
catch (\Exception $e) {
echo $e->getMessage();
}

echo "snapToken = ".$snapToken;
?>

Expand All @@ -99,7 +119,7 @@
<pre><div id="result-json">JSON result will appear here after payment:<br></div></pre>

<!-- TODO: Remove ".sandbox" from script src URL for production environment. Also input your client key in "data-client-key" -->
<script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="<Set your ClientKey here>"></script>
<script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="<?php echo $client_key;?>"></script>
<script type="text/javascript">
document.getElementById('pay-button').onclick = function(){
// SnapToken acquired from previous step
Expand Down

0 comments on commit 126bb33

Please sign in to comment.