Small payline.com relay. Only for dev purpose
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.6 KiB

<?php
/*
# payline.com dev relay
============================================
## Why ?
Payline doesn't allow any call from localhost and
most of dev naming uri. So... For dev purpose only
I make one of my server with a fully qualified
domain do the work with this script. Productions
servers won't ever use that relay.
It's not allowed to make card data transit trough
your servers in production context.
NOTE: Allow Origin headers are handled by the web server.
*/
class ksportRelay {
public $rInput = array(
"accessKeyRef" => null,
"data" => null,
"cardNumber" => null,
"cardExpirationDate" => null,
"cardCvx" => null
);
private $rCredential = array(
"uri" => "https://homologation-webpayment.payline.com/webpayment/getToken",
"appUser" => "",
"appSecret" => "",
"bearer" => "",
"bearerUri" => "https://api.sandbox.mangopay.com/v2.01/oauth/token/"
);
public $rResponse = array(
"iSatusCode" => 0,
"sMessage" => "",
"sPayload" => ""
);
function __construct() {
if ($this->input()) {
// If inputs are valids, then
// Get auth token.
$this->getAuthTwo();
// Ask to the card registration.
$this->send();
} else {
// Invalid input.
$this->rResponse["iSatusCode"] = -1;
$this->rResponse["sMessage"] = "Invalid input";
}
// display results.
echo json_encode($this->rResponse);
}
// Get auth tocken
function getAuthTwo() {
// Build security header
$securityHeader = base64_encode($this->rCredential["appUser"].
':'.$this->rCredential["appSecret"]);
// Init curl request
$resURL = curl_init();
// Set destination for the request
curl_setopt($resURL, CURLOPT_URL, $this->rCredential["bearerUri"]);
// Set headers
curl_setopt($resURL,CURLOPT_HTTPHEADER,array (
"Authorization: Basic ".$securityHeader,
/*"Content-Type: application/x-www-form-urlencoded;"
warning: Documentation tell to put that here.
It's an error on their end. */
));
// Set type of resquest.
curl_setopt($resURL, CURLOPT_POST, 1);
// Add post data to the request
curl_setopt($resURL, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
// Tell we want to collect the response
curl_setopt($resURL, CURLOPT_RETURNTRANSFER, 1);
// Get the response
$response = json_decode(curl_exec($resURL), true);
// Set the token for futur uses.
$this->rCredential["bearer"] = $response["access_token"];
}
// Ask for the card registration
function send() {
// init curl request
$resURL = curl_init();
// Set curl destination
curl_setopt($resURL, CURLOPT_URL, $this->rCredential["uri"]);
// Set curl headers
curl_setopt($resURL,CURLOPT_HTTPHEADER,array (
"Authorization: Bearer ".$this->rCredential["bearer"],
"Content-Type: application/x-www-form-urlencoded"
));
// Define type of curl request (POST)
curl_setopt($resURL, CURLOPT_POST, 1);
// Build and Add post data to the request
curl_setopt($resURL, CURLOPT_POSTFIELDS, http_build_query($this->rInput));
// Tell we want the response data
curl_setopt($resURL, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl request and get datas.
$this->rResponse["sPayload"] = curl_exec($resURL);
// If request is successuff
if (curl_getinfo($resURL, CURLINFO_HTTP_CODE) == 200) {
// if yes
$this->rResponse["sMessage"] = "SUCCESS (Everything where good on our end payline.com still may crash remotely... Please test the payload";
} else {
// if it's not
$this->rResponse["iSatusCode"] = -2;
$this->rResponse["sMessage"] = "Post resquest failed";
}
// close connection.
curl_close($resURL);
}
// Validate post input.
function input() {
$sPostData = file_get_contents("php://input");
$rPostData = json_decode($sPostData, true);
if (
isset($rPostData['accessKeyRef']) &&
isset($rPostData['data']) &&
isset($rPostData['cardNumber']) &&
isset($rPostData['cardExpirationDate']) &&
isset($rPostData['cardCvx'])
) {
$this->rInput["accessKeyRef"] = $rPostData['accessKeyRef'];
$this->rInput["data"] = $rPostData['data'];
$this->rInput["cardNumber"] = $rPostData['cardNumber'];
$this->rInput["cardExpirationDate"] = $rPostData['cardExpirationDate'];
$this->rInput["cardCvx"] = $rPostData['cardCvx'];
return true;
} else {
return false;
}
}
}
$ksport = new ksportRelay();