Current File : /home/pacjaorg/public_html/kmm/plugins/djclassifiedspayment/djcfauthorizenet/djcfauthorizenet.php |
<?php
/**
* @package DJ-Classifieds
* @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
* @license http://www.gnu.org/licenses GNU/GPL
* @author url: http://dj-extensions.com
* @author email contact@dj-extensions.com
*/
defined('_JEXEC') or die('Restricted access');
$lang = JFactory::getLanguage();
$lang->load('plg_djclassifiedspayment_djcfauthorizenet', JPATH_ADMINISTRATOR);
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djnotify.php');
require JPATH_ROOT.'/plugins/djclassifiedspayment/djcfauthorizenet/djcfauthorizenet/anet_php_sdk_v2/autoload.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
class plgdjclassifiedspaymentDjcfauthorizenet extends JPlugin
{
function __construct( &$subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage('plg_djcfauthorizenet');
$params["plugin_name"] = "djcfauthorizenet";
$params["icon"] = "";
$params["logo"] = "authorized.jpg";
$params["description"] = JText::_("PLG_DJCFAUTHORIZENET_PAYMENT_METHOD_DESC");
$params["payment_method"] = JText::_("PLG_DJCFAUTHORIZENET_PAYMENT_METHOD_NAME");
$params["login_id"] = $this->params->get('login_id');
$params["transaction_key"] = $this->params->get('transaction_key');
$params["account_type"] = $this->params->get('account_type', 'test');
$params["user_fields"] = $this->params->get('user_fields');
$this->params = $params;
}
function onProcessPayment()
{
$app = JFactory::getApplication();
$ptype = $app->input->getVar('ptype');
$html = '';
if($ptype == $this->params["plugin_name"])
{
$action = $app->input->getVar('pactiontype');
switch ($action)
{
case "notify" :
$html = $this->_notify_url();
break;
default :
$html = $this->_notify_url();
break;
}
}
return $html;
}
function _notify_url()
{
$app = JFactory::getApplication();
$account_type = $this->params["account_type"];
$Itemid = $app->input->getInt('Itemid', 0);
$id = $app->input->getInt('id', 0);
$ptype = $app->input->getVar('ptype');
$type = $app->input->getVar('type');
$msg_style = 'Warning';
$message = JText::_('PLG_DJCFAUTHORIZENET_AFTER_FAILED_MSG');
$redirect = JRoute::_('index.php?option=com_djclassifieds&view=payment&id='.$id.'&type='.$type.'&Itemid='.$Itemid, false);
$pdetails = DJClassifiedsPayment::processPayment($id, $type, $ptype);
$login_id = $this->params["login_id"];
$transaction_key = $this->params["transaction_key"];
$card_num = $app->input->getVar('card_no','0');
$exp_month = $app->input->getVar('exp_date','01');
$exp_year = $app->input->getVar('exp_year','2019');
$exp_date = $exp_year."-".$exp_month;
$cvv = $app->input->getInt('card_code', 0);
$user_field_missing = false;
foreach($this->params["user_fields"] as $user_field){
if(!$app->input->getVar($user_field)){
$user_field_missing = true;
break;
}
}
if($card_num=='0' || $cvv=='0' || $user_field_missing){
$message = JText::_('PLG_DJCFAUTHORIZENET_ENTER_ALL_VALUES');
$redirect = JRoute::_('index.php?option=com_djclassifieds&view=payment&id='.$id.($type ? '&type='.$type : '').'&Itemid='.$Itemid, false);
$app->enqueueMessage($message, 'Error');
$app->redirect($redirect);
}
//define("AUTHORIZENET_LOG_FILE", "phplog");
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($login_id);
$merchantAuthentication->setTransactionKey($transaction_key);
// Create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber($card_num);
$creditCard->setExpirationDate($exp_date);
$creditCard->setCardCode($cvv);
// Add the payment data to a paymentType object
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");
//$transactionRequestType->setAmount("5.99");
$transactionRequestType->setAmount($pdetails['amount']);
$transactionRequestType->setPayment($paymentOne);
// // Set the customer's identifying information
// $customerData = new AnetAPI\CustomerDataType();
// $customerData->setType("individual");
// $customerData->setId("99999456654");
// $customerData->setEmail("EllenJohnson@example.com");
// $transactionRequestType->setCustomer($customerData);
if($this->params["user_fields"]){
$user_fields = $this->params["user_fields"];
$customerAddress = new AnetAPI\CustomerAddressType();
if(in_array('first_name', $user_fields) && $app->input->getVar('first_name')){
$customerAddress->setFirstName($app->input->getVar('first_name'));
}
if(in_array('last_name', $user_fields) && $app->input->getVar('last_name')){
$customerAddress->setLastName($app->input->getVar('last_name'));
}
if(in_array('company', $user_fields) && $app->input->getVar('company')){
$customerAddress->setCompany($app->input->getVar('company'));
}
if(in_array('address', $user_fields) && $app->input->getVar('address')){
$customerAddress->setAddress($app->input->getVar('address'));
}
if(in_array('city', $user_fields) && $app->input->getVar('city')){
$customerAddress->setCity($app->input->getVar('city'));
}
if(in_array('state', $user_fields) && $app->input->getVar('state')){
$customerAddress->setState($app->input->getVar('state'));
}
if(in_array('zip', $user_fields) && $app->input->getVar('zip')){
$customerAddress->setZip($app->input->getVar('zip'));
}
if(in_array('country', $user_fields) && $app->input->getVar('country')){
$customerAddress->setCountry($app->input->getVar('country'));
}
$transactionRequestType->setBillTo($customerAddress);
}
// Add values for transaction settings
$duplicateWindowSetting = new AnetAPI\SettingType();
$duplicateWindowSetting->setSettingName("duplicateWindow");
$duplicateWindowSetting->setSettingValue("0");
$transactionRequestType->addToTransactionSettings($duplicateWindowSetting);
// Assemble the complete transaction request
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setTransactionRequest($transactionRequestType);
$request->setRefId($pdetails['item_id']);
// Create the controller and get the response
$controller = new AnetController\CreateTransactionController($request);
if($account_type=='secure'){
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);
}else{
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
}
if ($response != null) {
// Check to see if the API request was successfully received and acted upon
if ($response->getMessages()->getResultCode() == "Ok") {
// Since the API request was successful, look for a transaction response
// and parse it to display the results of authorizing the card
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null) {
if($tresponse->getResponseCode() == '1'){
$trans_id = $tresponse->getTransId();
DJClassifiedsPayment::completePayment($pdetails['item_id'], $pdetails['amount'], $trans_id);
$message = JText::_('COM_DJCLASSIFIEDS_THANKS_FOR_PAYMENT_WAIT_FOR_CONFIRMATION');
$redirect = 'index.php?option=com_djclassifieds&task=paymentReturn&r=ok&id='.$pdetails['item_id'];
$msg_style = '';
}
}
}else{
$tresponse = $response->getTransactionResponse();
if($tresponse){
$errors = $tresponse->getErrors();
if(isset($errors[0])){
$message = $errors[0]->getErrorText();
$msg_style = 'Error';
}
}
}
}
$app->enqueueMessage($message, $msg_style);
$app->redirect($redirect);
}
function onPaymentMethodList($val)
{
$app = JFactory::getApplication();
$logo_path = JURI::root()."plugins/djclassifiedspayment/".$this->params["plugin_name"]."/".$this->params["plugin_name"]."/images/".$this->params["logo"];
$action_url = DJClassifiedsSEO::getViewUri('payment')."&layout=process&task=processPayment&ptype=".$this->params["plugin_name"]."&pactiontype=notify&id=".$val["id"];
$ADN_form = "<script>
jQuery(function(){
jQuery('#adotnetform').on('submit', function(e){
if(document.paymentForm && !document.formvalidator.isValid(document.paymentForm)){
return false;
}
});
});
</script>
<form name='adotnetform' id='adotnetform' action='".$action_url."' method='post' class='form-validate'>
<table align='left'>
<td colspan=2><b>".JText::_('PLG_DJCFAUTHORIZENET_CREDIT_CART_PAYMENT').":</b></td>
<tr>
<td>".JText::_('PLG_DJCFAUTHORIZENET_CREDIT_CARD_NUMBER').": </td>
<td><input type='text' name='card_no' required class='required' /></td>
</tr>
<tr>
<td>".JText::_('PLG_DJCFAUTHORIZENET_CREDIT_CARD_SECURITY_CODE').": </td>
<td><input type='text' name='card_code' required class='required' /></td>
</tr>
<tr>
<td>".JText::_('PLG_DJCFAUTHORIZENET_EXPIRATION_DATE').": </td>
<td><select name='exp_date' id='exp_date'>
<option value='01'>".JText::_('JANUARY')."</option>
<option value='02'>".JText::_('FEBRUARY')."</option>
<option value='03'>".JText::_('MARCH')."</option>
<option value='04'>".JText::_('APRIL')."</option>
<option value='05'>".JText::_('MAY')."</option>
<option value='06'>".JText::_('JUNE')."</option>
<option value='07'>".JText::_('JULY')."</option>
<option value='08'>".JText::_('AUGUST')."</option>
<option value='09'>".JText::_('SEPTEMBER')."</option>
<option value='10'>".JText::_('OCTOBER')."</option>
<option value='11'>".JText::_('NOVEMBER')."</option>
<option value='12'>".JText::_('DECEMBER')."</option>
</select>
<select name='exp_year' id='exp_year'>";
for($i=date("Y");$i<date("Y")+10;$i++){
$ADN_form .= "<option value='".$i."'>".$i."</option>";
}
$ADN_form .= "</select></td>
</tr>";
if($this->params["user_fields"]){
foreach($this->params["user_fields"] as $u_field){
$ADN_form .= "<tr>
<td>".JText::_('PLG_DJCFAUTHORIZENET_CONFIG_USER_FIELDS_'.strtoupper($u_field)).": </td>
<td><input type='text' name='".$u_field."' /></td>
</tr>";
}
}
$ADN_form .= "<tr><td></td><td>";
$ADN_form .= "<input type='hidden' name='option' value='com_djclassifieds' /><input type='hidden' name='task' value='processPayment' /><input type='hidden' name='ptype' value='".$this->params["plugin_name"]."' /><input type='hidden' name='pactiontype' value='notify' /><input type='hidden' name='type' value='".$app->input->get('type')."' />";
$ADN_form .= "</td></tr> </table>
</form>";
return DJClassifiedsTheme::renderLayout('paymentmethod',
array(
'name' => $this->params["payment_method"],
'desc' => $this->params["description"],
'logo_path' => $logo_path,
'a_href' => 'javascript:void(0);',
'a_attr' => 'onclick="jQuery(\'#adotnetform\').submit()"',
'extra_desc' => '<p style="text-align:justify;">'.$ADN_form.'</p>',
'params' => $this->params
)
);
}
}