Skip to content

Instantly share code, notes, and snippets.

@thlinux1107
Last active February 25, 2020 15:17
Show Gist options
  • Save thlinux1107/2cc08c9d4177509035fb09892dc2418d to your computer and use it in GitHub Desktop.
Save thlinux1107/2cc08c9d4177509035fb09892dc2418d to your computer and use it in GitHub Desktop.
Paya Gateway - JAVA Direct API Sample - Sale
///*----------------------------------------------
//Author: Thomas Hagan
//Company: Paya
//Contact: sdksupport@paya.com
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!! Samples intended for educational use only!!!
//!!! Not intended for production !!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//-----------------------------------------------*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws Exception {
try {
//Set Variables
String merchantID = "173859436515";
String merchantKey = "P1J2V8P2Q3D8";
String clientID = "W8yvKQ5XbvAn7dUDJeAnaWCEwA4yXEgd";
String clientSecret = "iLzODV5AUsCGWGkr";
String verb = "POST";
String host = "https://api-cert.sagepayments.com/bankcard/v1/charges";
String query = "?type=Sale";
String endpoint = host + query;
long epoch = System.currentTimeMillis()/1000;
String timestamp = Long.toString(epoch);
String nonce = timestamp;
String body = new String(Files.readAllBytes(Paths.get("request.json")), StandardCharsets.UTF_8);
System.out.println("Body:");
System.out.println(body);
// Build string to be hashed comprised of the verb, url, request body, Merchant ID, // nonce, and the timestamp.
String authToken = verb + endpoint + body + merchantID + nonce + timestamp;
System.out.println("authToken:");
System.out.println(authToken);
// Create authorization
final String HMAC_SHA512 = "HmacSHA512";
Mac hasher = Mac.getInstance(HMAC_SHA512);
hasher.init(new SecretKeySpec(clientSecret.getBytes(), HMAC_SHA512));
byte[] hash = hasher.doFinal((authToken.toString()).getBytes());
//String hmac = Base64.encode(hash);
String hmac = Base64.getEncoder().encodeToString(hash);
System.out.println("HMAC:");
System.out.println(hmac);
URL url = new URL(endpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("authorization", hmac);
conn.setRequestProperty("merchantid", merchantID);
conn.setRequestProperty("merchantkey", merchantKey);
conn.setRequestProperty("clientid", clientID);
conn.setRequestProperty("nonce", nonce);
conn.setRequestProperty("timestamp", timestamp);
conn.setRequestProperty("cache-control", "no-cache");
conn.setRequestProperty("Content-Type", "application/json");
String input = body;
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode() + " Server Response: " + conn.getResponseMessage());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
{
"eCommerce": {
"amounts": {
"total": 7.00,
"tax": 0,
"shipping": 0
},
"orderNumber": "SDK Invoice 123",
"cardData": {
"number": "5454545454545454",
"expiration": "1230",
"cvv": "123"
},
"customer": {
"email": "kur@foo.com",
"telephone": "8885555555",
"fax": "8885555555"
},
"billing": {
"name": "John Smith",
"address": "123 Main St",
"city": "Savannah",
"state": "GA",
"postalCode": "31405",
"country": "USA"
},
"shipping": {
"name": "John Smith",
"address": "123 Main St",
"city": "Savannah",
"state": "GA",
"postalCode": "31405",
"country": "USA"
}
},
"vault": {
"operation": "create"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment