-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswjprojects_payment.php
166 lines (149 loc) · 5.46 KB
/
swjprojects_payment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* @package SW JProjects Payment
* @version __DEPLOY_VERSION__
* @author Septdir Workshop - www.septdir.com
* @copyright Copyright (c) 2018 - 2019 Septdir Workshop. All rights reserved.
* @license GNU/GPL license: https://www.gnu.org/copyleft/gpl.html
* @link https://www.septdir.com/
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Table\Table;
class PlgSystemSWJProjects_Payment extends CMSPlugin
{
/**
* Add fields.
*
* @param Form $form The form to be altered.
* @param mixed $data The associated data for the form.
*
* @throws Exception
* @since __DEPLOY_VERSION__
*/
public function onContentPrepareForm($form, $data)
{
$formName = $form->getName();
if (preg_match('#com_swjprojects\.project_#', $formName))
{
$app = Factory::getApplication();
Form::addFormPath(__DIR__);
Form::addFormPath(__DIR__);
$form->loadFile('payment', false);
if ($app->isClient('administrator') && $app->input->get('view') == 'project'
&& $app->input->get('task') != 'apply')
{
$form->removeField('price', 'payment');
$form->removeField('link', 'payment');
if (empty($app->input->get('id')))
{
$form->removeField('payment_plugin_price', 'payment');
$form->removeField('payment_plugin_currency', 'payment');
$app->enqueueMessage('Payment enable only after save', 'warning');
}
}
}
}
/**
* Change before save.
*
* @param string $context Save context.
* @param object $data The associated data for the item.
* @param Form $form The associated data for the item.
*
* @since __DEPLOY_VERSION__
*/
public function onContentNormaliseRequestData($context, $data, $form)
{
if ($context === 'com_swjprojects.project' && !empty($data->id) && !empty($data->translates)
&& $data->download_type == 'paid')
{
$link = 'index.php?option=com_ajax&plugin=swjprojects_payment&group=system&step=checkout&format=html&id='
. $data->id;
foreach ($data->translates as $code => $translate)
{
if (!empty($translate['payment']) && !empty($translate['payment']['payment_plugin_price']))
{
$translate['payment']['link'] = $link;
$translate['payment']['price'] = $translate['payment']['payment_plugin_price'];
if (!empty($translate['payment']['payment_plugin_currency']))
{
$translate['payment']['price'] .= ' ' . $translate['payment']['payment_plugin_currency'];
}
$data->translates[$code] = $translate;
}
}
}
}
/**
* Functions conteoloer.
*
* @throws Exception
*
* @return mixed Function result.
*
* @since __DEPLOY_VERSION__
*/
public function onAjaxSWJProjects_Payment()
{
$action = Factory::getApplication()->input->get('step');
if (empty($action) || !method_exists($this, $action))
{
throw new Exception('Incorrect step', 500);
}
return $this->$action();
}
public function checkout()
{
JLoader::register('SWJProjectsHelperRoute', JPATH_SITE . '/components/com_swjprojects/helpers/route.php');
JLoader::register('SWJProjectsHelperImages', JPATH_SITE . '/components/com_swjprojects/helpers/images.php');
JLoader::register('SWJProjectsHelperTranslation', JPATH_ADMINISTRATOR . '/components/com_swjprojects/helpers/translation.php');
BaseDatabaseModel::addIncludePath(JPATH_SITE . '/components/com_swjprojects/models');
$model = BaseDatabaseModel::getInstance('Project', 'SWJProjectsModel', array('ignore_request' => false));
if ($project = $model->getItem())
{
$pay = 'index.php?option=com_ajax&plugin=swjprojects_payment&group=system&step=success&format=html&id='
. $project->id;
Factory::getDocument()->setTitle('Buy ' . $project->title);
include_once(__DIR__ . '/checkout.php');
}
}
public function success()
{
JLoader::register('SWJProjectsHelperRoute', JPATH_SITE . '/components/com_swjprojects/helpers/route.php');
JLoader::register('SWJProjectsHelperImages', JPATH_SITE . '/components/com_swjprojects/helpers/images.php');
JLoader::register('SWJProjectsHelperTranslation', JPATH_ADMINISTRATOR . '/components/com_swjprojects/helpers/translation.php');
BaseDatabaseModel::addIncludePath(JPATH_SITE . '/components/com_swjprojects/models');
$modelProject = BaseDatabaseModel::getInstance('Project', 'SWJProjectsModel', array('ignore_request' => false));
if ($project = $modelProject->getItem())
{
JLoader::register('SWJProjectsHelperKeys', JPATH_ADMINISTRATOR . '/components/com_swjprojects/helpers/keys.php');
Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_swjprojects/tables');
BaseDatabaseModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_swjprojects/models');
$modelKey = BaseDatabaseModel::getInstance('Key', 'SWJProjectsModel', array('ignore_request' => true));
$data = array(
'key' => '',
'id' => 0,
'project_id' => $project->id,
'order' => 'Payment plugin ' . rand(),
'email' => '',
'date_start' => '',
'date_end' => '',
'state' => 1,
'note' => 'Generate from paymnet plugin',
);
if ($modelKey->save($data))
{
$key = $modelKey->getItem();
$downloadLink = Route::_(SWJProjectsHelperRoute::getDownloadRoute(null, null,
$project->element, $key->key));
Factory::getDocument()->setTitle('Thank you for your purchase');
include_once(__DIR__ . '/success.php');
}
}
}
}