Created
May 21, 2020 14:54
-
-
Save thlinux1107/4927cd60da217f91b764293e215e41d8 to your computer and use it in GitHub Desktop.
Paya Connect - PayForm - Sample to demonstrate send_parent_message and display data on parent page as well as sending data to an approved page upon a successful transaction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*---------------------------------------------- | |
Author: SDK Support Group | |
Company: Paya | |
Contact: [email protected] | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
!!! Samples intended for educational use only!!! | |
!!! Not intended for production !!! | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
-----------------------------------------------*/ | |
require('shared.php'); | |
// set variables | |
$locationID = $location['ID']; | |
$transactionAPIID = $_COOKIE["TransAPIID"]; | |
$host = $developer['Host']; | |
$developerID = $developer['ID']; | |
$user_id = $user['ID']; | |
$timestamp = time(); | |
// the RESTful API uses a user_api_key instead of the | |
// user_hash_key required for the PayForm | |
$user_api_key = $user['apiKey']; | |
// query variables required for the Transactions endpoint | |
$verb = "GET"; | |
$endpoint = "/v2/transactions/"; | |
$query = "?api_id=1&location_id=" . $locationID; | |
// Build URL | |
$url = $host . $endpoint . $transactionAPIID . $query; | |
// ok, let's make the request! cURL is always an option, of course, | |
// but i find that file_get_contents is a bit more intuitive. | |
$config = [ | |
"http" => [ | |
"header" => [ | |
"developer-id: " . $developerID, | |
"user-api-key: " . $user_api_key, | |
"user-id: " . $user_id, | |
"content-type: application/json", | |
], | |
"method" => $verb, | |
"ignore_errors" => true // exposes response body on 4XX errors | |
] | |
]; | |
$context = stream_context_create($config); | |
$result = file_get_contents($url, false, $context); | |
$response = json_decode($result); | |
$httpcode = http_response_code(); | |
?> | |
<html> | |
<h1>Your Transaction Has Been Approved</h1> | |
<h2>Response Details</h2> | |
<pre><?php print_r($response) ?></pre> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*---------------------------------------------- | |
Author: SDK Support Group | |
Company: Paya | |
Contact: [email protected] | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
!!! Samples intended for educational use only!!! | |
!!! Not intended for production !!! | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
-----------------------------------------------*/ | |
require('shared.php'); | |
?> | |
<html> | |
<h1>Your Transaction Has Been Declined</h1> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*---------------------------------------------- | |
Author: SDK Support Group | |
Company: Paya | |
Contact: [email protected] | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
!!! Samples intended for educational use only!!! | |
!!! Not intended for production !!! | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
-----------------------------------------------*/ | |
require('shared.php'); | |
// set variables | |
$locationID = $location['ID']; | |
$contactID = $location['ContactID']; | |
$host = $developer['Host']; | |
$developerID = $developer['ID']; | |
// set variables for generating the required hash | |
$user_id = $user['ID']; | |
$user_hash_key = $user['HashKEY']; | |
$timestamp = time(); | |
// Generate the secure hash, making sure the variables | |
// are in the proper sequence. | |
$data = $user_id . $timestamp; | |
$hash_key = hash_hmac('sha256', $data, $user_hash_key); | |
// set *api_id | |
$transactionAPIID = "SDK" . $timestamp; | |
// Create Request | |
$req = [ | |
"transaction" => [ | |
"payment_method" => "cc", | |
"action" => "sale", | |
"transaction_amount" => "7.00", | |
"location_id" => $locationID, | |
"contact_id" => $contactID, | |
"transaction_api_id" => $transactionAPIID, | |
"surcharge_amount" => "0.01", | |
"account_holder_name" => "john smith", | |
"billing_street" => "123 Main St", | |
"billing_zip" => "31405", | |
"entry_method" => "manual", | |
"show_account_holder_name" => true, | |
"show_street" => true, | |
"show_zip" => true, | |
"send_parent_message" => 1, | |
"parent_close" => 0, | |
"parent_close_delay" => 1, | |
"parent_origin" => null, | |
"display_close_button" => true, | |
"save_account" => 1, | |
"save_account_title" => "sdk payform test", | |
"redirect_url_on_approval" => "[HOST]/approved.php", | |
"redirect_url_on_decline" => "[HOST]/declined.php", | |
"redirect_url_delay" => 0, | |
"description" => "SDK Test PayForm Redirect" | |
] | |
]; | |
// Hex encode the request data | |
$hexReq = bin2hex(json_encode($req)); | |
// Build URL (URL + Developer ID + Hash Key + User ID + Timestamp + Hex-encoded Request Data) | |
$url = $host . "/v2/payform?developer-id=" . $developerID . "&hash-key=" . $hash_key . "&user-id=" . $user_id . "×tamp=" . $timestamp . "&data=" . $hexReq; | |
// create and set cookie to send the transaction_api_id | |
// to the approved page to GET transaction details for display | |
setcookie("TransAPIID", $transactionAPIID); | |
?> | |
<html> | |
<head> | |
<style> | |
a { | |
display:inline-block; | |
background-color:#428bca; | |
border-color:#357ebd; | |
border-radius:5px; | |
border-width:0; | |
border-style:none; | |
color:#ffffff; | |
font-size:12px; | |
height:30px; | |
width:100px; | |
margin:0px; | |
padding:7px; | |
text-decoration:none; | |
text-align:center; | |
} | |
</style> | |
<!-- Add this script tag prior to embedding the iFrame --> | |
<script> | |
window.addEventListener("message", receiveMessage, false); | |
function receiveMessage(event) { | |
// Make sure the value for allowed matches the domain of the iFrame you are embedding. | |
var allowed = "https://api.sandbox.payaconnect.com"; | |
// Verify sender's identity | |
if (event.origin !== allowed) return; | |
// Add logic here for doing something in response to the message | |
console.log(event); // for example only, log event object | |
console.log(JSON.parse(event.data)); // for example only, log JSON data | |
// Write Response from PayForm to Parent Page | |
//document.getElementById("form_response").innerHTML //= JSON.stringify(event.data); | |
// Write Response from PayForm to Parent Page | |
var response = document.getElementById("form_response"); | |
var obj = JSON.parse(event.data); | |
response.innerHTML = JSON.stringify(obj, undefined, 2); | |
} | |
</script> | |
</head> | |
<body> | |
<div> | |
<h1>Paya Connect Payment Form</h1> | |
<br /> | |
</div> | |
<!-- include the iframe after the script tag for the event listener --> | |
<iframe src="<?= $url ?>" width="400px" height="500px"></iframe> | |
<div> | |
<h1>Parent Page Response</h1> | |
<pre id="form_response"></pre> | |
</div> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment