File: /home/assibfaf/public_html/prox-modules/pesapal/pesapal.php
<?php
use Proxim\Application;
use Proxim\Configuration;
use Proxim\Module\Module;
use Proxim\Module\PaymentModule;
use Proxim\Site\Site;
use Proxim\Util\ArrayUtils;
use Proxim\Validate;
define('CURRENT_PESAPAL_MODULE_DIR', realpath(dirname(__FILE__)));
require_once(CURRENT_PESAPAL_MODULE_DIR . '/classes/PesaPal.php');
class Pesapal extends PaymentModule
{
/**
* Pesapal configuration
*/
const PESAPAL_ENABLED = 'PESAPAL_ENABLED';
const PESAPAL_MODE = 'PESAPAL_MODE';
const PESAPAL_CONSUMER_KEY = 'PESAPAL_CONSUMER_KEY';
const PESAPAL_CONSUMER_SECRET = 'PESAPAL_CONSUMER_SECRET';
public function __construct()
{
$this->name = 'pesapal';
$this->icon = 'fa fa-credit-card';
$this->version = '1.0.0';
$this->prox_versions_compliancy = array('min' => '1.0.0', 'max' => Application::VERSION);
$this->author = 'Davison Pro';
$this->displayName = 'Pesapal';
$this->description = 'Set up Pesapal as a payment method on your website';
$this->bootstrap = true;
parent::__construct();
}
public function isActive() {
return Configuration::get(self::PESAPAL_ENABLED);
}
/**
* Echoes a template.
*
* @param string $templateName Template name
*/
public function showTemplate($templateName)
{
echo $this->getTemplateContent($templateName);
}
/**
* Return a template.
*
* @param string $templateName Template name
* @param array $additionnalParameters Additionnal parameters to inject on the Twig template
*
* @return string Parsed template
*/
private function getTemplateContent($templateName, $additionnalParameters = array())
{
$this->smarty->assign($additionnalParameters);
return $this->fetch(__DIR__ . '/views/' . $templateName.'.tpl');
}
public function processOrderCheckout($order, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$token = $params = NULL;
$api_url = Configuration::get(self::PESAPAL_MODE) == "live" ? "https://www.pesapal.com/api" : "https://demo.pesapal.com/api";
$post_url = $api_url . '/PostPesapalDirectOrderV4';
$status_request = $api_url . '/querypaymentstatus';
$consumer_key = Configuration::get( self::PESAPAL_CONSUMER_KEY );
$consumer_secret = Configuration::get( self::PESAPAL_CONSUMER_SECRET );
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$type = 'MERCHANT';
$payment_method = '';
$email = $user->email;
$username = $user->name ? $user->name : $user->email;
$currency = $order->currency;
$options['currency'] = array(
'symbol' => $currency->symbol,
'rate' => $currency->conversion_rate
);
$desc = "Payment of " . formatPrice($order->total, $options) . " to " . Configuration::get('SITE_NAME');
$code = "";
$amount = (float) $order->total*$currency->conversion_rate;
$currency_code = $currency->iso_code;
$callback_url = $app->base_uri . '/payment/'.$this->name.'/callback?handle=order';
$reference = $order->id;
$post_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PesapalDirectOrderInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Amount=\"" . $amount . "\" Description=\"" . $desc . "\" Code=\"" . $code . "\" Currency=\"" . $currency_code . "\" Type=\"" . $type . "\" PaymentMethod=\"" . $payment_method . "\" Reference=\"" . $reference . "\" Email=\"" . $email . "\" UserName=\"" . $username . "\" xmlns=\"http://www.pesapal.com\" />";
$post_xml = htmlentities($post_xml);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
//post transaction to pesapal
$iframe_src = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $post_url, $params);
$iframe_src->set_parameter("oauth_callback", $callback_url);
$iframe_src->set_parameter("pesapal_request_data", $post_xml);
$iframe_src->sign_request($signature_method, $consumer, $token);
$smarty->assign([
'iframe_src' => $iframe_src,
'payee_email' => $user->email
]);
return $this->getTemplateContent('pesapal.order');
}
public function processPaperCheckout($paper, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$token = $params = NULL;
$api_url = Configuration::get(self::PESAPAL_MODE) == "live" ? "https://www.pesapal.com/api" : "https://demo.pesapal.com/api";
$post_url = $api_url . '/PostPesapalDirectOrderV4';
$status_request = $api_url . '/querypaymentstatus';
$consumer_key = Configuration::get( self::PESAPAL_CONSUMER_KEY );
$consumer_secret = Configuration::get( self::PESAPAL_CONSUMER_SECRET );
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$type = 'MERCHANT';
$payment_method = '';
$email = $user->email;
$username = $user->name ? $user->name : $user->email;
$desc = "Payment of " . formatPrice($paper->price) . " to " . Configuration::get('SITE_NAME');
$code = "";
$callback_url = $app->base_uri . '/payment/' . $this->name.'/callback?handle=paper';
$reference = $paper->id;
$post_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PesapalDirectOrderInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Amount=\"" . $paper->price . "\" Description=\"" . $desc . "\" Code=\"" . $code . "\" Currency=\"" . $currency->iso_code . "\" Type=\"" . $type . "\" PaymentMethod=\"" . $payment_method . "\" Reference=\"" . $reference . "\" Email=\"" . $email . "\" UserName=\"" . $username . "\" xmlns=\"http://www.pesapal.com\" />";
$post_xml = htmlentities($post_xml);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
//post transaction to pesapal
$iframe_src = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $post_url, $params);
$iframe_src->set_parameter("oauth_callback", $callback_url);
$iframe_src->set_parameter("pesapal_request_data", $post_xml);
$iframe_src->sign_request($signature_method, $consumer, $token);
$smarty->assign([
'iframe_src' => $iframe_src,
'payee_email' => $user->email
]);
return $this->getTemplateContent('pesapal.paper');
}
public function processAddPaymentCheckout($addPayment, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$token = $params = NULL;
$api_url = Configuration::get(self::PESAPAL_MODE) == "live" ? "https://www.pesapal.com/api" : "https://demo.pesapal.com/api";
$post_url = $api_url . '/PostPesapalDirectOrderV4';
$status_request = $api_url . '/querypaymentstatus';
$consumer_key = Configuration::get( self::PESAPAL_CONSUMER_KEY );
$consumer_secret = Configuration::get( self::PESAPAL_CONSUMER_SECRET );
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$type = 'MERCHANT';
$payment_method = '';
$email = $user->email;
$username = $user->name ? $user->name : $user->email;
$options['currency'] = array(
'symbol' => $currency->symbol,
'rate' => $currency->conversion_rate
);
$desc = "Payment of " . formatPrice($addPayment->total, $options) . " to " . Configuration::get('SITE_NAME');
$code = "";
$amount = (float) $addPayment->total*$currency->conversion_rate;
$currency_code = $currency->iso_code;
$callback_url = $app->base_uri . '/payment/'.$this->name.'/callback?handle=add-payment';
$reference = $addPayment->id;
$post_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PesapalDirectOrderInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Amount=\"" . $amount . "\" Description=\"" . $desc . "\" Code=\"" . $code . "\" Currency=\"" . $currency_code . "\" Type=\"" . $type . "\" PaymentMethod=\"" . $payment_method . "\" Reference=\"" . $reference . "\" Email=\"" . $email . "\" UserName=\"" . $username . "\" xmlns=\"http://www.pesapal.com\" />";
$post_xml = htmlentities($post_xml);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
//post transaction to pesapal
$iframe_src = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $post_url, $params);
$iframe_src->set_parameter("oauth_callback", $callback_url);
$iframe_src->set_parameter("pesapal_request_data", $post_xml);
$iframe_src->sign_request($signature_method, $consumer, $token);
$smarty->assign([
'iframe_src' => $iframe_src,
'payee_email' => $user->email
]);
return $this->getTemplateContent('pesapal.add-payment');
}
public function processWalletCheckout($user_id, $amount, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$token = $params = NULL;
$api_url = Configuration::get(self::PESAPAL_MODE) == "live" ? "https://www.pesapal.com/api" : "https://demo.pesapal.com/api";
$post_url = $api_url . '/PostPesapalDirectOrderV4';
$status_request = $api_url . '/querypaymentstatus';
$consumer_key = Configuration::get( self::PESAPAL_CONSUMER_KEY );
$consumer_secret = Configuration::get( self::PESAPAL_CONSUMER_SECRET );
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$type = 'MERCHANT';
$payment_method = '';
$email = $user->email;
$username = $user->name ? $user->name : $user->email;
$options['currency'] = array(
'symbol' => $currency->symbol,
'rate' => $currency->conversion_rate
);
$desc = "Payment of " . formatPrice($amount, $options) . " to " . Configuration::get('SITE_NAME');
$code = "";
$amount = (float) $amount*$currency->conversion_rate;
$currency_code = $currency->iso_code;
$callback_url = $app->base_uri . '/payment/'.$this->name.'/callback?handle=wallet&amount=' . $amount;
$reference = $user_id;
$post_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PesapalDirectOrderInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Amount=\"" . $amount . "\" Description=\"" . $desc . "\" Code=\"" . $code . "\" Currency=\"" . $currency_code . "\" Type=\"" . $type . "\" PaymentMethod=\"" . $payment_method . "\" Reference=\"" . $reference . "\" Email=\"" . $email . "\" UserName=\"" . $username . "\" xmlns=\"http://www.pesapal.com\" />";
$post_xml = htmlentities($post_xml);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
//post transaction to pesapal
$iframe_src = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $post_url, $params);
$iframe_src->set_parameter("oauth_callback", $callback_url);
$iframe_src->set_parameter("pesapal_request_data", $post_xml);
$iframe_src->sign_request($signature_method, $consumer, $token);
$smarty->assign([
'iframe_src' => $iframe_src,
'payee_email' => $user->email
]);
return $this->getTemplateContent('pesapal.wallet');
}
public function processTipCheckout($order_id, $amount, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$token = $params = NULL;
$api_url = Configuration::get(self::PESAPAL_MODE) == "live" ? "https://www.pesapal.com/api" : "https://demo.pesapal.com/api";
$post_url = $api_url . '/PostPesapalDirectOrderV4';
$status_request = $api_url . '/querypaymentstatus';
$consumer_key = Configuration::get( self::PESAPAL_CONSUMER_KEY );
$consumer_secret = Configuration::get( self::PESAPAL_CONSUMER_SECRET );
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$type = 'MERCHANT';
$payment_method = '';
$email = $user->email;
$username = $user->name ? $user->name : $user->email;
$options['currency'] = array(
'symbol' => $currency->symbol,
'rate' => $currency->conversion_rate
);
$desc = "Payment of " . formatPrice($amount, $options) . " to " . Configuration::get('SITE_NAME');
$code = "";
$amount = (float) $amount*$currency->conversion_rate;
$currency_code = $currency->iso_code;
$callback_url = $app->base_uri . '/payment/'.$this->name.'/callback?handle=tip&amount=' . $amount;
$reference = $order_id;
$post_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PesapalDirectOrderInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Amount=\"" . $amount . "\" Description=\"" . $desc . "\" Code=\"" . $code . "\" Currency=\"" . $currency_code . "\" Type=\"" . $type . "\" PaymentMethod=\"" . $payment_method . "\" Reference=\"" . $reference . "\" Email=\"" . $email . "\" UserName=\"" . $username . "\" xmlns=\"http://www.pesapal.com\" />";
$post_xml = htmlentities($post_xml);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
//post transaction to pesapal
$iframe_src = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $post_url, $params);
$iframe_src->set_parameter("oauth_callback", $callback_url);
$iframe_src->set_parameter("pesapal_request_data", $post_xml);
$iframe_src->sign_request($signature_method, $consumer, $token);
$smarty->assign([
'iframe_src' => $iframe_src,
'payee_email' => $user->email
]);
return $this->getTemplateContent('pesapal.tip');
}
/**
* Hook action verify payment
* @param $params array
* @return void
*/
public function verifyPaymentAfter($params)
{
$app = $this->application;
$user = $app->container->user;
$consumer_key = Configuration::get( self::PESAPAL_CONSUMER_KEY );
$consumer_secret = Configuration::get( self::PESAPAL_CONSUMER_SECRET );
$api_url = Configuration::get( self::PESAPAL_MODE ) == "live" ? "https://www.pesapal.com/api" : "https://demo.pesapal.com/api";
$post_url = $api_url . '/PostPesapalDirectOrderV4';
$statusrequestAPI = $api_url . '/querypaymentstatus';
$handle = ArrayUtils::get($params, 'handle');
$amount = ArrayUtils::get($params, 'amount');
$orderreference = ArrayUtils::get($params, 'pesapal_merchant_reference');
$transaction_tracking_id = ArrayUtils::get($params, 'pesapal_transaction_tracking_id');
$pesapal_merchant_reference = ArrayUtils::get($params, 'pesapal_merchant_reference');
if($transaction_tracking_id) {
$token = $params = NULL;
$consumer = new \OAuthConsumer($consumer_key, $consumer_secret);
$signature_method = new \OAuthSignatureMethod_HMAC_SHA1();
//get transaction status
$request_status = \OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $statusrequestAPI, $params);
$request_status->set_parameter("pesapal_merchant_reference", $pesapal_merchant_reference);
$request_status->set_parameter("pesapal_transaction_tracking_id",$transaction_tracking_id);
$request_status->sign_request($signature_method, $consumer, $token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_status);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if(defined('CURL_PROXY_REQUIRED')) if (\CURL_PROXY_REQUIRED == 'True')
{
$proxy_tunnel_flag = (defined('CURL_PROXY_TUNNEL_FLAG') && strtoupper(\CURL_PROXY_TUNNEL_FLAG) == 'FALSE') ? false : true;
curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_tunnel_flag);
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt ($ch, CURLOPT_PROXY, \CURL_PROXY_SERVER_DETAILS);
}
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, \CURLINFO_HEADER_SIZE);
$raw_header = substr($response, 0, $header_size - 4);
$headerArray = explode("\r\n\r\n", $raw_header);
$header = $headerArray[count($headerArray) - 1];
//transaction status
$elements = preg_split("/=/",substr($response, $header_size));
$status = $elements[1];
$params['payment_method'] = 'pesapal';
$params['paymentId'] = $transaction_tracking_id;
$params['amount'] = $amount;
$params['customer_id'] = $user->id;
if( $status == 'COMPLETED' ) {
PaymentModule::processPayment( $orderreference, $handle, $params);
}
}
}
}