Skip to content

Instantly share code, notes, and snippets.

@thlinux1107
Created May 21, 2020 12:43
Show Gist options
  • Save thlinux1107/50fe6f63c4a95c55e8f7d9ac0c8c2a93 to your computer and use it in GitHub Desktop.
Save thlinux1107/50fe6f63c4a95c55e8f7d9ac0c8c2a93 to your computer and use it in GitHub Desktop.
Paya Connect - PayForm - Sample to demonstrate send_parent_message and display data on parent page
<?php
/*----------------------------------------------
Author: SDK Support Group
Company: Paya
Contact: sdksupport@paya.com
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Samples intended for educational use only!!!
!!! Not intended for production !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-----------------------------------------------*/
require('shared.php');
// set variable
$locationID = $location['ID'];
$contactID = $location['ContactID'];
$host = $developer['Host'];
$developerID = $developer['ID'];
// Define the secret ticket_hash_key used for hashing the variables
$user_hash_key = $user['HashKEY'];
// Define variables for generating the required hash
$user_id = $user['ID'];
$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", // Required field
"action" => "sale",
"transaction_amount" => "7.00",
"location_id" => $locationID, // Required field
"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"
]
];
// 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 . "&timestamp=" . $timestamp . "&data=" . $hexReq;
?>
<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
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>Response</h1>
<pre id="form_response"></pre>
</div>
</body>
</html>
<?php
/*----------------------------------------------
Author: SDK Support Group
Company: Paya
Contact: sdksupport@paya.com
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Samples intended for educational use only!!!
!!! Not intended for production !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-----------------------------------------------*/
// this is the location ID and Contact ID
$location = [
"ID" => "[Location ID]",
"ContactID" => "[Contact ID]"
];
// User Credentials
$user = [
"ID" => "[User ID]",
"HashKEY" => "[User Hash Key]"
];
$developer = [
"ID" => "[Developer ID]",
"Host" => "https://api.sandbox.payaconnect.com"
];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment