-
Knutsford
Member
-
Posts: 92
Threads: 40
Joined: Dec 2016
Reputation:
0
01-07-2019, 02:37 PM
(This post was last modified: 01-07-2019, 02:38 PM by Knutsford.)
Has anyone got codeignitor to work with SagePay? I am using PHP 5.3.29 and the encryption isn't working. According to SagePay the value isn't right even though my Key is and I am using the way they do it in their documentation. I am calling it using Curl. If someone has got it working how are they doing the encryption please? Thanks
Code: function to($order_id) {
$data['order'] = $this->order->get_by_id($order_id);
$the_country = trim($data['order']->order_country);
$countries = $this->db->get_where("countries", array( "Name" => $the_country))->result();
foreach($countries as $country) {
$country_code = $country->Code2;
}
$paymentAmount = urlencode($data['order']->getTotalPrice());
$paymentAmount = number_format($paymentAmount,2);
$query = array();
$query['VendorTxCode'] = "Order" . $order_id;
$query['Amount'] = $paymentAmount;
$query['Currency'] = "GPB";
$query['Description'] = urlencode("Sign order on RusticStone.net/test - ORDER ID: " . $data['order']->order_id . " - £ " . $paymentAmount);
$query['CustomerName'] = trim($data['order']->order_first_name) . " " . trim($data['order']->order_last_name);
$query['CustomerEMail'] = trim($data['order']->order_email);
$query['VendorEMail'] = "[email protected]";
$query['BillingFirstnames'] = trim($data['order']->order_first_name);
$query['BillingSurname'] = trim($data['order']->order_last_name);
$query['BillingAddress1'] = trim($data['order']->order_address);
$query['BillingCity'] = trim($data['order']->order_city);
$query['BillingState'] = trim($data['order']->order_state);
$query['BillingPostCode'] = trim($data['order']->order_post_code);
$query['BillingCountry'] =$country_code;
$query['BillingPhone'] = trim($data['order']->order_phone_1);
$query['DeliveryFirstnames'] = trim($data['order']->order_first_name);
$query['DeliverySurname'] = trim($data['order']->order_last_name);
$query['DeliveryAddress1'] = trim($data['order']->order_address);
$query['DeliveryCity'] = trim($data['order']->order_city);
$query['DeliveryState'] = trim($data['order']->order_state);
$query['DeliveryPostCode'] = trim($data['order']->order_post_code);
$query['DeliveryCountry'] = $country_code;
$query['DeliveryPhone'] = trim($data['order']->order_phone_1);
$query['SuccessURL'] = site_url("test/payments/sagepay/confirm/" . $data['order']->order_id);
$query['FailureURL'] = site_url("test/payments/sagepay/failure/" . $data['order']->order_id);
$queryStr = self::arrayToQueryString($query);
$formValues = array();
$formValues['Vendor'] = config_item('Vendor');
$formValues['VPSProtocol'] = config_item('VPSProtocol');
$formValues['TxType'] = "PAYMENT";
$formValues['Crypt'] = self::encryptAes($queryStr, config_item('Key'));
$url = "https://test.sagepay.com/gateway/service/vspform-register.vsp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, self::arrayToQueryString($formValues));
}
static public function arrayToQueryString(array $data, $delimiter = '&', $urlencoded = false)
{
$queryString = '';
$delimiterLength = strlen($delimiter);
// Parse each value pairs and concate to query string
foreach ($data as $name => $value)
{
// Apply urlencode if it is required
if ($urlencoded)
{
$value = urlencode($value);
}
$queryString .= $name . '=' . $value . $delimiter;
}
// remove the last delimiter
return substr($queryString, 0, -1 * $delimiterLength);
}
static public function encryptAes($string, $key)
{
// AES encryption, CBC blocking with PKCS5 padding then HEX encoding.
// Add PKCS5 padding to the text to be encypted.
$string = self::addPKCS5Padding($string);
// Perform encryption with PHP's MCRYPT module.
$crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $key);
// Perform hex encoding and return.
return "@" . strtoupper(bin2hex($crypt)); }
/**
* PHP's mcrypt does not have built in PKCS5 Padding, so we use this.
*
* @param string $input The input string.
*
* @return string The string with padding.
*/
static protected function addPKCS5Padding($input)
{
$blockSize = 16;
$padd = "";
// Pad input to an even block size boundary.
$length = $blockSize - (strlen($input) % $blockSize);
for ($i = 1; $i <= $length; $i++)
{
$padd .= chr($length);
}
}
Thanks
-
php_rocs
Administrator
-
Posts: 1,410
Threads: 98
Joined: Jun 2016
Reputation:
72
|