Created
February 7, 2019 20:49
-
-
Save thlinux1107/7e87fc0e5c9b8a95d48d1892e8f6145d to your computer and use it in GitHub Desktop.
Paya Connect - AccountForm C# Core Sample
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
using System; | |
using System.IO; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Diagnostics; | |
using System.Web; | |
/*---------------------------------------------- | |
Author: SDK Support Group | |
Company: Paya | |
Contact: sdksupport@paya.com | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
!!! Samples intended for educational use only!!! | |
!!! Not intended for production !!! | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
-----------------------------------------------*/ | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
Trans().Wait(); | |
} | |
static async Task Trans() | |
{ | |
// TH - 20170304 - Added the equivalent of vbCrLf in vb.net | |
var nl = Environment.NewLine; | |
// TH - Test Data is provided when you register at | |
// https://developer.sandbox.payaconnect.com/applicationrequest | |
// If you have any questions feel free to reach out | |
// to us directly at sdksupport@paya.com | |
var locationID = "[Location ID]"; | |
var contactID = "[Contact ID]"; | |
var acctVaultAPIID = "SDK40"; // Should be unique per vaulted account | |
// TH - The Developer ID is assigned when registering with the | |
// Paya Connect developer portal listed above. | |
var developerID = "[Developer ID]"; | |
// TH - User credentials | |
var userID = "[User ID]"; | |
var user_hash_key = "[User Hash Key]"; | |
// Build Timestamp for the Hash Key | |
Int32 timestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; | |
// Use JSON request from file. | |
// This will submit a sale using card data, return the result and a vault token. | |
StreamReader sr = new StreamReader("accountvault.json"); | |
string jsonReq = sr.ReadToEnd(); | |
// Replace "@" placeholders in the JSON file with variables. | |
string request = jsonReq.Replace("@locationID", locationID) | |
.Replace("@contactID", contactID) | |
.Replace("@acctVaultAPIID", acctVaultAPIID); | |
sr.Close(); | |
// Generate the secure hash, making sure the variables | |
// are in the proper sequence. | |
var data = userID + timestamp; | |
byte[] hash_data = new HMACSHA256(Encoding.UTF8.GetBytes(user_hash_key)).ComputeHash(Encoding.UTF8.GetBytes(data)); | |
string hash_key = BitConverter.ToString(hash_data).ToLower().Replace("-", string.Empty); | |
// Convert request from json string to bytes to hex for transport | |
byte[] bytes = Encoding.Default.GetBytes(request); | |
string hexReq = BitConverter.ToString(bytes).ToLower().Replace("-", string.Empty); | |
// Build URL | |
var endpoint = "/v2/accountform?"; | |
var url = "https://api.sandbox.payaconnect.com" + endpoint; | |
// Build link | |
var link = url + "developer-id=" + developerID + "&hash-key=" + hash_key + "&user-id=" + userID + "×tamp=" + timestamp + "&data=" + hexReq; | |
// Console output for debugging. | |
Console.WriteLine("EXECUTING THE FOLLOWING:"); | |
Console.WriteLine(nl); | |
Console.WriteLine("Request:"); | |
Console.WriteLine(request); | |
Console.WriteLine(nl); | |
Console.WriteLine("Data for Hash Key:" + data); | |
Console.WriteLine(nl); | |
Console.WriteLine("Timestamp: " + timestamp); | |
Console.WriteLine(nl); | |
Console.WriteLine("WebLink:"); | |
Console.WriteLine(link); | |
Console.WriteLine(nl); | |
// Location of the executable for the browser differs based on system. May not | |
// need to enter the location if you have IE as your default. | |
var browserExecutable = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"; | |
Process.Start(browserExecutable, link); | |
Console.WriteLine("Press Enter to exit:"); | |
Console.ReadLine(); | |
} | |
} |
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
{ | |
"accountvault": { | |
"payment_method": "cc", | |
"location_id": "@locationID", | |
"contact_id": "@contactID", | |
"account_vault_api_id": "@acctVaultAPIID", | |
"title": "Account_Vault", | |
"account_holder_name": "SDK Test", | |
"show_street": true, | |
"show_account_holder_name": true, | |
"show_zip": true, | |
"send_parent_message": 0, | |
"parent_close": true, | |
"parent_close_delay": 5, | |
"parent_origin": null, | |
"display_close_button": true, | |
"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