Skip to content

Instantly share code, notes, and snippets.

@thlinux1107
Created May 12, 2020 20:54
Show Gist options
  • Save thlinux1107/4b74dc53db46ac77a0c22734831f6cf3 to your computer and use it in GitHub Desktop.
Save thlinux1107/4b74dc53db46ac77a0c22734831f6cf3 to your computer and use it in GitHub Desktop.
Paya Connect - AccountForm - Java sample
// /*----------------------------------------------
// Author: SDK Support Group
// Company: Paya
// Contact: sdksupport@paya.com
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! Samples intended for educational use only!!!
// !!! Not intended for production !!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// -----------------------------------------------*/
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.security.spec.KeySpec;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.Scanner;
class Main {
public static void main(String[] args) throws Exception {
// Test credentials are provided when you register at
// https://developer.sandbox.payaconnect.com/users/register
// If you have any questions feel free to reach out
// to us directly at sdksupport@paya.com
String developerID = "[Developer ID]";
String userID = "[User ID]";
String userHashKey = "[User Hash Key]";
// The Location ID typically relates to a single merchant account
String locationID = "[Location ID]";
// The Contact ID is created when you create a contact record
// Using eithe the Contacts Endpoint or within the Paya Connect
// Sandbox UI. This value is optional.
String contactID = "[Contact ID]";
String host = "https://api.sandbox.payaconnect.com";
String endpoint = "/v2/accountform";
// Create the epoch timestamp for use with generating
// the Hash Key
long epoch = System.currentTimeMillis()/1000;
String timestamp = Long.toString(epoch);
System.out.println("Timestamp: " + timestamp);
// Generate the secure hash, making sure the variables
// are in the proper sequence. The Hash Key is the encoded string
// consisting of the User ID and timestamp.
String data = userID + timestamp;
String hashKey = encode(userHashKey, data);
// The Account Vault API ID is a integrator-generated value
// that serves as an alternative to the Account Vault ID returned
// within the response.
String accountVaultAPIID = "SDKTEST" + timestamp;
// Create the JSON request.
String cRequest = new String(Files.readAllBytes(Paths.get("request.json")), StandardCharsets.UTF_8);
String request = cRequest.replace("@locationID",locationID).replace("@contactID",contactID).replace("@accountVaultAPIID",accountVaultAPIID);
System.out.println("Request:");
System.out.println(request);
// Hex convert the request
String hexReq = toHexString(request.getBytes("UTF-8"));
// Build the custom URL
String link = host + endpoint + "?developer-id=" + developerID + "&hash-key=" + hashKey + "&user-id=" + userID + "&timestamp=" + timestamp + "&data=" + hexReq;
// Typically you'll host the link within an iFrame
System.out.println("Link:");
System.out.println(link);
}
public static String encode(String key, String data) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
return toHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
}
public static String toHexString(byte[] ba) {
StringBuilder str = new StringBuilder();
for(int i = 0; i < ba.length; i++)
str.append(String.format("%02x", ba[i]));
return str.toString();
}
}
{
"accountvault": {
"payment_method": "cc",
"location_id": "@locationID",
"account_vault_api_id": "@accountVaultAPIID",
"contact_id": "@contactID",
"title": "Account_Vault",
"account_holder_name": "SDK Test",
"show_account_holder_name": 1,
"show_street": 1,
"show_zip": 1,
"stylesheet_url": "",
"display_close_button": 1,
"parent_close": 1,
"parent_close_delay": 3,
"parent_origin": null,
"redirect_url_on_approval": "https://www.google.com",
"redirect_url_delay": 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment