| Server IP : 54.36.91.62 / Your IP : 216.73.217.111 Web Server : Apache System : Linux webm013.cluster127.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 User : coopiak ( 151928) PHP Version : 8.3.23 Disable Function : _dyuweyrj4,_dyuweyrj4r,dl MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/coopiak/amisdesseniors-fr/nimes/plugins/djcatalog2payment/paypal/ |
Upload File : |
<?php
/**
* @package DJ-Catalog2
* @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
*/
// no direct access
use Curl\Curl;
use Joomla\CMS\Date\Date;
defined('_JEXEC') or die('Restricted access');
use Joomla\Registry\Registry;
//jimport('joomla.plugin.plugin');
require_once __DIR__ . '/lib/autoloader.php';
require_once __DIR__ . '/vendor/autoload.php';
class plgDjcatalog2paymentPaypal extends JPlugin
{
protected $currency;
public function __construct(&$subject, $config = array())
{
parent::__construct($subject, $config);
$cparams = JComponentHelper::getParams('com_djcatalog2');
$this->currency = $cparams->get('cart_currency', 'USD');
$this->debug = $this->params->get('debug', 0);
$this->loadLanguage();
require_once JPATH_ROOT . '/plugins/djcatalog2payment/paypal/lib/PaypalIPN.php';
}
protected function isAllowed($plgInfo, $type = 'djcatalog2payment')
{
return (bool)($plgInfo->plugin == $this->_name && $this->_type == $type);
}
public function onContentPrepareForm($form, $data)
{
if ($form->getName() != 'com_djcatalog2.payment') {
return;
}
$plugin = '';
if (!empty($data) && !empty($data->plugin)) {
$plugin = $data->plugin;
} else {
$jform = JFactory::getApplication()->input->get('jform', array(), 'array');
if (!empty($jform) && isset($jform['plugin'])) {
$plugin = $jform['plugin'];
}
}
if ($plugin != $this->_name) {
return true;
}
return $form->loadFile(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'configuration.xml', false);
}
public function onDJC2BeforeSaveOrder($context, $table, $isNew, $plgInfo)
{
if ($context != 'com_djcatalog2.checkout.payment' || !$this->isAllowed($plgInfo)) {
return;
}
}
public function onDJC2AfterSaveOrder($context, $table, $isNew, $plgInfo)
{
if ($context != 'com_djcatalog2.checkout.payment' || !$this->isAllowed($plgInfo)) {
return;
}
}
public function onDJC2CheckoutDetailsDisplay($context, $plgInfo)
{
if ($context != 'com_djcatalog2.checkout.payment' || !$this->isAllowed($plgInfo)) {
return;
}
$html = '';
if (trim(strip_tags($plgInfo->description)) != '') {
$html = '<h4>' . $plgInfo->name . '</h4>';
$html .= $plgInfo->description;
}
return $html;
}
public function onDJC2OrderDetailsDisplay($context, $order, $plgInfo)
{
if ($context != 'com_djcatalog2.order.payment' || !$this->isAllowed($plgInfo)) {
return;
}
$app = JFactory::getApplication();
$message = '';
if ($app->input->get('view') == 'order' && $app->input->get('error')) {
$message = JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_ERROR_TEXT');
} else if ($app->input->get('success') == '1') {
$message = JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_RETURN_TEXT');
}
if ($order->status != 'A' && $order->status != 'N') {
return;
}
$paymentUrl = JRoute::_('index.php?option=com_djcatalog2&task=paymentProcess&oid=' . $order->id . '&plg=' . $plgInfo->plugin . '&plgid=' . $plgInfo->id);
$html = '<div class="paypalinfo">';
if ($message) {
$html .= '<p class="paypalalert">' . $message . '</p>';
}
if ($app->input->get('finished') == '1' && $plgInfo->params->get('autoredir', '1') == '1') {
$html .= '<script>jQuery(document).ready(function(){
setTimeout(function(){window.location.href="' . JRoute::_('index.php?option=com_djcatalog2&task=paymentProcess&oid=' . $order->id . '&plg=' . $plgInfo->plugin . '&plgid=' . $plgInfo->id, false) . '";}, 2000);
});</script>';
}
$payment_params = json_decode($order->payment_params);
$html .= '<p class="paypalmessage">';
$html .= JText::_('PLG_DJCATALOG2PAYMENT_PAYPAY_INFO_TEXT');
$html .= '</p>';
$success = $app->input->get('success');
$subscription_id = $app->input->getRaw('subscription_id');
if($subscription_id && $subscription_id == $order->payment_params) {
$html .= '<p class="paypalbutton">';
$html .= JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_RETURN_TEXT');
$html .= '</p>';
$html .= '</div>';
return $html;
}else {
$html .= '<p class="paypalbutton">';
$html .= '<a href="' . $paymentUrl . '" class="paypalbtn btn ">' . JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_BTN') . '</a>';
$html .= '</p>';
$html .= '</div>';
return $html;
}
}
function onDJC2PaymentProcess($context, $order, $plgInfo)
{
if ($context != 'com_djcatalog2.order.payment' || !$this->isAllowed($plgInfo)) {
return;
}
if (!empty($order->recurring) && $order->recurring == 1)
$this->tokenizePayment($order, $plgInfo);
else
$this->standardPaymentProcess($order, $plgInfo);
}
/* Recuring Payment Process */
private function tokenizePayment($order, $plgInfo)
{
$cliendId = $plgInfo->params->get('client_id');
$cliendSecret = $plgInfo->params->get('client_secret');
if (empty($cliendId) || empty($cliendSecret)) {
throw new Exception(JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_ERROR_MISSING_PARAMS'), 400);
}
$orderItem = $order->items[0];
$name = $orderItem->item_name;
$product_id = $orderItem->id;
$product_id = $orderItem->id;
$recurringParams = new Registry($order->recurring_params);
$amount = number_format($order->grand_total, 2);
$return = JUri::root() . DJCatalogHelperRoute::getOrderRoute($order->id) . '&success=1';
$notify_url = JUri::root() . 'index.php?option=com_djcatalog2&task=paymentResponse&plg=' . $plgInfo->plugin . '&plgid=' . $plgInfo->id;
$expiration = 0;
if ($orderItem->product_type == 'subscription') {
$db = JFactory::getDbo();
$db->setQuery('SELECT * FROM #__djc2_items WHERE id = ' . (int)$orderItem->item_id);
$item = $db->loadObject();
$expiration = $item->expiration;
}
// create paypal product
$product = [
'name' => $name,
'type' => 'SERVICE',
];
$planId = 'PLAN-' . $product_id . '-' . $order->id;
$curl = new Curl();
$curl->setHeader('Content-Type', 'application/json');
$curl->setBasicAuthentication($cliendId, $cliendSecret);
$curl->setHeader('PayPal-Request-Id', $planId);
$curl->post('https://api-m.sandbox.paypal.com/v1/catalogs/products', $product);
$body = [
'name' => $name,
'product_id' => $curl->response->id,
'description' => 'Subscription plan',
'payment_preferences' => [
'auto_bill_outstanding' => true,
'setup_fee_failure_action' => 'CANCEL',
'payment_failure_threshold' => 1,
'setup_fee' => [
'value' => $amount,
'currency_code' => $order->currency
],
],
'billing_cycles' => [
[
'pricing_scheme' => [
'fixed_price' => [
'value' => $amount,
'currency_code' => $order->currency
]
],
'frequency' => [
'interval_unit' => 'DAY',
'interval_count' => $expiration,
],
'tenure_type' => 'REGULAR',
'sequence' => 1
]
]
];
$access_token = $curl->response->access_token;
$curl = new Curl();
$curl->setHeader('Content-Type', 'application/json');
$curl->setBasicAuthentication($cliendId, $cliendSecret);
$curl->setHeader('PayPal-Request-Id', $planId);
$curl->post('https://api-m.sandbox.paypal.com/v1/billing/plans', $body);
$app = \JFactory::getApplication();
if ($curl->error) {
$app->enqueueMessage($curl->errorMessage, 'warning');
$app->redirect(JRoute::_('index.php?option=com_djcatalog2&view=order&oid=' . (int)$order->id));
return;
}
$now = new Date('now + 5 minutes');
$subscription = [
'plan_id' => $curl->response->id,
'start_time' => $now->toISO8601(),
'quantity' => 1,
'subscriber' => [
'name' => [
'given_name' => $order->first_name,
'surname' => $order->last_name
],
'email_address' => $order->email,
],
'application_context' => [
'return_url' => $return,
'user_action' => 'SUBSCRIBE_NOW',
]
];
$curl = new Curl();
$curl->setHeader('Content-Type', 'application/json');
$curl->setBasicAuthentication($cliendId, $cliendSecret);
$curl->setHeader('PayPal-Request-Id', $planId);
$curl->post('https://api-m.sandbox.paypal.com/v1/billing/subscriptions', $subscription);
$recurringParams->set('subscription_id', $curl->response->id);
foreach ($curl->response->links as $link) {
if($link->rel == 'approve') {
$orderUpdate = new stdClass();
$orderUpdate->payment_params = $curl->response->id;
$orderUpdate->id=$order->id;
$result = JFactory::getDbo()->updateObject('#__djc2_orders', $orderUpdate, 'id');
header( "refresh:1;url=" . $link->href );
break;
}
}
}
public function onDJC2CapturePayment($model, $plgInfo) {
if (!$this->isAllowed($plgInfo)) {
return;
}
$model->changeStatus($model->getItem(), 'P', true, true, JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_PAYMENT_COMPLETED'));
}
/* Standard Payment Process */
private function standardPaymentProcess($order, $plgInfo)
{
$emailId = $plgInfo->params->get('email_id');
$testMode = $plgInfo->params->get('test');
if (empty($emailId)) {
throw new Exception(JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_ERROR_MISSING_PARAMS'), 400);
}
$app = JFactory::getApplication();
$websiteName = $app->get('sitename', JURI::base());
$return = JRoute::_(DJCatalogHelperRoute::getOrderRoute($order->id) . '&success=1', false, (JUri::getInstance()->isSSL() ? 1 : -1));
$cancel_return = JRoute::_(DJCatalogHelperRoute::getOrderRoute($order->id) . '&error=1', false, (JUri::getInstance()->isSSL() ? 1 : -1));
//$notify_url = JRoute::_(DJCatalogHelperRoute::getCategoryRoute(0) . '&task=paymentResponse&plg=' . $plgInfo->plugin . '&plgid=' . $plgInfo->id . '&oid=' . $order->id, false, (JUri::getInstance()->isSSL() ? 1 : -1));
$notify_url = JRoute::_('index.php?option=com_djcatalog2&task=paymentResponse&plg=' . $plgInfo->plugin . '&plgid=' . $plgInfo->id . '&oid=' . $order->id . '&Itemid=', false, (JUri::getInstance()->isSSL() ? 1 : -1));
$cancel_return .= '&ts=' . time();
$notify_url .= '&ts=' . time();
$return .= '&ts=' . time();
$description = JText::sprintf('PLG_DJCATALOG2PAYMENT_PAYPAL_ORDER_DESCRIPTION', $order->order_number, $websiteName);
//$currency = (!empty($order->currency)) ? $order->currency : $plgInfo->params->get('currency_code', $this->currency);
$currency = $order->currency;
$amount = round($order->grand_total, 2);
$optional_fields = [
'address1' => $order->address,
'city' => $order->city,
'zip' => $order->postcode,
'country' => $order->country,
'email' => $order->email,
'first_name' => $order->firstname,
'last_name' => $order->lastname
];
$urlpaypal = "";
if ($testMode) {
$urlpaypal = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
$urlpaypal = "https://www.paypal.com/cgi-bin/webscr";
}
$html = '<!doctype html><html lang="en-US"><head><meta charset="utf-8"><title>' . JText::_('COM_DJCATALOG2_PAYMENT_REDIRECTION') . '</title></head>';
$html .= '<body><div style="margin: auto; text-align: center;">';
$html .= '<form method="post" action="' . $urlpaypal . '" name="paypalform">';
$html .= '<p>' . JText::_('COM_DJCATALOG2_PAYMENT_REDIRECTION') . '</p>';
$html .= '<input type="hidden" name="cmd" value="_xclick" />';
$html .= '<input id="custom" type="hidden" name="custom" value="' . $order->id . '" />';
$html .= '<input type="hidden" name="business" value="' . trim($emailId) . '" />';
$html .= '<input type="hidden" name="currency_code" value="' . trim($currency) . '" />';
$html .= '<input type="hidden" name="item_name" value="' . substr($description, 0, 127) . '" />';
$html .= '<input type="hidden" name="amount" value="' . $amount . '" />';
$html .= '<input type="hidden" name="charset" value="utf-8" />';
$html .= '<input type="hidden" name="cancel_return" value="' . ($cancel_return) . '" />';
$html .= '<input type="hidden" name="notify_url" value="' . ($notify_url) . '" />';
$html .= '<input type="hidden" name="return" value="' . ($return) . '" />';
foreach ($optional_fields as $fieldkey => $fieldval) {
if (trim($fieldval) == '') continue;
$html .= '<input type="hidden" name="' . $fieldkey . '" value="' . ($fieldval) . '" />';
}
$html .= '<noscript><input type="submit" value="' . JText::_('COM_DJCATALOG2_PAYMENT_REDIRECTION_BUTTON') . '" /></noscript>';
$html .= '</form></div>';
$html .= ' <script type="text/javascript">';
$html .= ' setTimeout(function(){ document.paypalform.submit(); }, 500);';
$html .= ' </script></body></html>';
echo $html;
$app->close();
}
public function onDJC2PaymentDiscover($context, $plgInfo)
{
if ($context != 'com_djcatalog2.order.payment' || !$this->isAllowed($plgInfo)) {
return;
}
$json = file_get_contents('php://input');
$data = json_decode($json);
if(isset($data->resource->billing_agreement_id)) {
if(!isset($data->event_type) || $data->event_type !== 'PAYMENT.SALE.COMPLETED') {
return 0;
}else {
$db = JFactory::getDbo();
$db->setQuery('SELECT * FROM #__djc2_orders WHERE payment_params = ' . $db->quote($data->resource->billing_agreement_id));
$order = $db->loadObject();
return $order->id;
}
}else {
$this->log('[onDJC2PaymentDiscover] ' . $plgInfo->plugin);
$this->log('[onDJC2PaymentDiscover] ' . print_r($_POST, true));
$app = JFactory::getApplication();
$oid = $app->input->getInt('oid');
$this->log('[onDJC2PaymentDiscover] order id: ' . $oid);
return $oid;
}
}
public function onDJC2PaymentResponse($context, $order, $model, $plgInfo)
{
if ($context != 'com_djcatalog2.order.payment' || !$this->isAllowed($plgInfo)) {
return;
}
$app = JFactory::getApplication();
if ($order->recurring == 1)
$this->recurringPaymentResponse($context, $order, $model, $plgInfo);
else
$this->standardPaymentResponse($context, $order, $model, $plgInfo);
$app->close();
}
private function recurringPaymentResponse($context, $order, $model, $plgInfo)
{
$app = JFactory::getApplication();
$json = file_get_contents('php://input');
$data = json_decode($json);
$status = $data->resource->state;
$json = file_get_contents('php://input');
$data = json_decode($json);
$filename = 'paypal3.log';
$handle = fopen($filename, 'w');
fwrite($handle, floatval($data->resource->amount->total) . ' ' . floatval($order->grand_total) . ' ' . $status);
fclose($handle);
if (floatval($data->resource->amount->total) != floatval($order->grand_total)) {
$this->log('[onDJC2PaymentResponse] failed verification price fraud');
$this->log('Price Paypal ' . $app->input->getString('mc_gross'));
$this->log('Price Order ' . $order->grand_total);
$app->close();
}
$this->log('[onDJC2PaymentResponse] payment status: ' . $status);
if (($status == 'completed')) {
$this->log('[onDJC2PaymentResponse] order status: ' . $order->status);
// confirm the order unless it has already been rejected or confirmed
if ($order->status == 'A' || $order->status == 'N') {
$this->log('[onDJC2PaymentResponse] payment status changed');
$model->changeStatus($order, 'P', true, true, JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_PAYMENT_COMPLETED'));
}
}
}
private function standardPaymentResponse($context, $order, $model, $plgInfo)
{
$emailId = $plgInfo->params->get('email_id');
$testMode = $plgInfo->params->get('test');
$app = JFactory::getApplication();
if (empty($emailId)) {
throw new Exception(JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_ERROR_MISSING_PARAMS'), 400);
}
$paypal_info = $_POST;
$this->log('[onDJC2PaymentResponse] POST DATA init: ');
$this->log(print_r($paypal_info, true));
$ipn = new PaypalIPN();
if ($testMode)
$ipn->useSandbox(true);
try {
if (!$ipn->verifyIPN()) {
$this->log('[onDJC2PaymentResponse] failed verification');
$app->close();
}
} catch (Exception $e) {
$this->log('[onDJC2PaymentResponse] exception ' . $e->getMessage());
$app->close();
}
if (floatval($app->input->getString('mc_gross')) != floatval($order->grand_total)) {
$this->log('[onDJC2PaymentResponse] failed verification price fraud');
$this->log('Price Paypal ' . $app->input->getString('mc_gross'));
$this->log('Price Order ' . $order->grand_total);
$app->close();
}
$status = $paypal_info['payment_status'];
$this->log('[onDJC2PaymentResponse] payment status: ' . $status);
if (($status == 'Completed') || ($status == 'Pending' && $testMode == 1)) {
$this->log('[onDJC2PaymentResponse] order status: ' . $order->status);
// confirm the order unless it has already been rejected or confirmed
if ($order->status == 'A' || $order->status == 'N') {
$this->log('[onDJC2PaymentResponse] payment status changed');
$model->changeStatus($order, 'P', true, true, JText::_('PLG_DJCATALOG2PAYMENT_PAYPAL_PAYMENT_COMPLETED'));
}
}
}
private function getOrderFreaquency($repeat)
{
switch ($repeat) {
case 'weekly' :
return 'WEEK';
case 'monthly' :
return 'MONTH';
case 'yearly' :
return 'YEAR';
}
}
private function getOrderCycles($order)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__djc2_orders_recurring'));
$query->where($db->quoteName('order_id') . ' = ' . (int)$order->id);
$query->order('start ASC');
$db->setQuery($query);
return $db->loadObjectList();
}
private function discoverOrderIdByRecurring($recurring_payment_id)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$payment_params_regex = $db->escape('%"recurring_payment_id":"' . $recurring_payment_id . '"%');
$query->select('id')
->from($db->quoteName('#__djc2_orders'))
->where($db->quoteName('payment_params') . ' LIKE ' . $db->quote($payment_params_regex));
$db->setQuery($query);
return $db->loadResult();
}
private function setOrderPaymentParams($order, $params)
{
$order_item = new stdClass();
$order_item->id = $order->id;
$order_item->payment_params = json_encode($params);
return JFactory::getDbo()->updateObject('#__djc2_orders', $order_item, 'id');
}
private function log($msg)
{
$fp = fopen(__DIR__ . '/lib/logs.txt', 'a');
fwrite($fp, print_r($msg, true));
fwrite($fp, "\n");
fclose($fp);
}
}
?>