Skip to content

Instantly share code, notes, and snippets.

@wajihkm
Created May 17, 2020 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wajihkm/aafafcb46b918545243f407339dac0a2 to your computer and use it in GitHub Desktop.
Save wajihkm/aafafcb46b918545243f407339dac0a2 to your computer and use it in GitHub Desktop.
ExpressCheckout handle response
<?php
if (isset($_POST['transaction_id'])) {
file_put_contents('result.txt', json_encode($_POST));
echo 'Done';
} else if ($_REQUEST['t'] == 'cancel') {
file_put_contents('result.txt', json_encode(['response_message' => 'Payment canceled']));
} else {
echo '0';
}
<?php
$res = file_get_contents('result.txt');
if ($res) {
file_put_contents('result.txt', '');
echo $res;
} else {
echo '0';
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ExpressCheckout v3 response</title>
</head>
<body>
<style>
.PT_express_checkout {
height: 550px;
}
.result {
display: none;
}
.result.success {
background-color: green;
}
.result.failed {
background-color: red;
}
</style>
<script>
const credintials = {
merchant_id: "10051111",
secret_key: "xxx",
};
</script>
<h1>ExpressCheckout v3, response example</h1>
<div class="result success">Payment successed</div>
<div class="result failed">Payment failed</div>
<div>
<link rel="stylesheet" href="https://www.paytabs.com/theme/express_checkout/css/express.css">
<script src="https://www.paytabs.com/theme/express_checkout/js/jquery-1.11.1.min.js"></script>
<script src="https://www.paytabs.com/express/express_checkout_v3.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Button Code for PayTabs Express Checkout -->
<div class="PT_express_checkout"></div>
<script type="text/javascript">
Paytabs("#express_checkout").expresscheckout({
settings: {
...credintials,
amount: "10.00",
currency: "USD",
title: "Mr. John Doe",
product_names: "Product1,Product2,Product3",
order_id: 25,
url_redirect: "http://localhost/callback.php?t=redirect",
display_customer_info: 1,
display_billing_fields: 1,
display_shipping_fields: 0,
language: "en",
redirect_on_reject: 1,
is_iframe: {
load: "onbodyload",
show: 1
},
is_self: 1,
url_cancel: "http://localhost/callback.php?t=cancel"
},
customer_info: {
first_name: "John",
last_name: "Smith",
phone_number: "5486253",
email_address: "john@test.com",
country_code: "973"
},
billing_address: {
full_address: "Manama, Bahrain",
city: "Manama",
state: "Manama",
country: "BHR",
postal_code: "00973"
},
shipping_address: {
shipping_first_name: "Jane",
shipping_last_name: "Abdulla",
full_address_shipping: "Manama, Bahrain",
city_shipping: "Manama",
state_shipping: "Manama",
country_shipping: "BHR",
postal_code_shipping: "00973"
},
});
</script>
<script>
let pt_checker = setInterval(start, 3000);
function start() {
$.get('/ec/notify/check.php', (data, status) => {
if (data != '0') {
let json = JSON.parse(data);
let success = json.response_code == 100;
alert('Payment status: ' + json.response_message);
if (success) {
$('.success').show('fast');
} else {
$('.failed').show('fast');
}
console.log(json);
clearInterval(pt_checker);
} else {
console.log('.');
}
});
}
</script>
</div>
</body>
</html>
@wajihkm
Copy link
Author

wajihkm commented May 17, 2020

Make sure to add result.txt file with write permission.

@wajihkm
Copy link
Author

wajihkm commented May 17, 2020

Also it is possible to use Sockets which is much better (will add another example soon)

@shanjoyshajan
Copy link

How can we enable mada in this code on that jquery?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment