Skip to content

Instantly share code, notes, and snippets.

@thlinux1107
Created May 7, 2020 23:47
Show Gist options
  • Save thlinux1107/dd5e540f3f95e30537bdefcb84489a5f to your computer and use it in GitHub Desktop.
Save thlinux1107/dd5e540f3f95e30537bdefcb84489a5f to your computer and use it in GitHub Desktop.
Paya Connect - Hosted Payment Page PHP Sample - includes custom fields
<?php
/*----------------------------------------------
Author: SDK Support Group
Company: Paya
Contact: sdksupport@paya.com
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Samples intended for educational use only!!!
!!! Not intended for production !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-----------------------------------------------*/
require('shared.php');
$id = $hpp['ID'];
$timestamp = time();
// Custom request for HPP.
$req = [
"id" => $id,
"field_configuration" => [
"body" => [
"fields" => [
[
"id" => "transaction_amount",
"value" => 7.00,
"label" => "Amount",
"readonly" => true,
"visible" => true
],
[
"id" => "action",
"value" => "sale",
"visible" => false
],
[
"id" => "order_num",
"value" => "SDK_TEST_INVOICE " . $timestamp,
"label" => "",
"readonly" => true,
"visible" => false
],
[
"id" => "description",
"value" => "SDK Test Auth" . $timestamp,
"label" => "",
"readonly" => true,
"visible" => false
],
[
"id" => "transaction_api_id",
"value" => "SDKTEST" . $timestamp,
"label" => "",
"readonly" => true,
"visible" => false
],
[
"id" => "billing_street",
"value" => "123 Main St",
"label" => "Billing Street",
"readonly" => false,
"visible" => true
],
[
"id" => "billing_city",
"value" => "Savannah",
"label" => "Billing City",
"readonly" => false,
"visible" => true
],
[
"id" => "billing_state",
"value" => "GA",
"label" => "Billing State",
"readonly" => false,
"visible" => true
],
[
"id" => "billing_zip",
"value" => "31405",
"label" => "Billing Zip",
"readonly" => false,
"visible" => true
],
[
"id" => "save_account",
"value" => true,
"visible" => false
],
[
"id" => "save_account_title",
"value" => "Primary Card",
"visible" => false
]
]
]
],
"redirect_url_on_approve" => "https://google.com",
"redirect_url_on_decline" => "https://google.com"
];
// Encode the request details
$sReq = json_encode($req);
$data = getEncodedData($sReq, $hpp['KEY']);
?>
<!--Create a page to display the payment link.-->
<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>
<div>
<h1>Paya Connect Hosted Payment Page</h1>
<br />
<!--Build the payment link.-->
<a href="https://api.sandbox.payaconnect.com/hostedpaymentpage?id=<?= $id ?>&data=<?= $data ?>" type="button">Pay Now</a>
</div>
<?php
/*----------------------------------------------
Author: SDK Support Group
Company: Paya
Contact: sdksupport@paya.com
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Samples intended for educational use only!!!
!!! Not intended for production !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-----------------------------------------------*/
// This is the Hosted Payment Page ID and Encryption Key.
// These are provided when you setup an HPP under a location.
$hpp = [
"ID" => "[HPP ID]",
"KEY" => "[HPP Hash Key]"
];
// Function to encrypt/encode data.
function getEncodedData($toBeHashed, $password){
$salt = openssl_random_pseudo_bytes(8);
$salted = '';
$dx = '';
while (strlen($salted) < 48) {
$dx = md5($dx . $password . $salt, true);
$salted .= $dx;
}
$key = substr($salted, 0, 32);
$iv = substr($salted, 32, 16);
// Encrypt the JSON object using the encryptionKey
$encryptedString = openssl_encrypt($toBeHashed, 'aes-256-cbc', $key, true, $iv);
$encodedEncryptedString = urlencode(base64_encode("Salted__" . $salt . $encryptedString));
return $encodedEncryptedString;
}
// Function to write debug information to console.
function debug_to_console( $data ) {
$output = $data;
if ( is_array( $output ) )
$output = implode( ',', $output);
echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment