This repository was archived by the owner on Dec 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRazorPayPlugin.inc.php
More file actions
175 lines (151 loc) · 4.9 KB
/
RazorPayPlugin.inc.php
File metadata and controls
175 lines (151 loc) · 4.9 KB
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
167
168
169
170
171
172
173
174
175
<?php
use Razorpay\Api\Api;
import('lib.pkp.classes.plugins.PaymethodPlugin');
class RazorPayPlugin extends PaymethodPlugin
{
/**
* @see Plugin::getDisplayName
*/
function getDisplayName()
{
return 'RazorPay Payment Plugin';
}
/**
* @see Plugin::getDescription
*/
function getDescription()
{
return 'Payments will be processed using the RazorPay service.';
}
/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
$this->addLocaleData();
HookRegistry::register('Form::config::before', array($this, 'addSettings'));
return true;
}
return false;
}
/**
* Add settings to the payments form
*
* @param $hookName string
* @param $form FormComponent
*/
public function addSettings($hookName, $form)
{
import('lib.pkp.classes.components.forms.context.PKPPaymentSettingsForm'); // Load constant
if ($form->id !== FORM_PAYMENT_SETTINGS) {
return;
}
$context = Application::get()->getRequest()->getContext();
if (!$context) {
return;
}
$groupid = 'razorpay';
$form->addGroup([
'id' => $groupid,
'label' => $this->getDisplayName(),
'showWhen' => 'paymentsEnabled',
])
->addField(new \PKP\components\forms\FieldText('key_id', [
'label' => 'Key Id',
'value' => $this->getSetting($context->getId(), 'key_id'),
'groupId' => $groupid,
]))
->addField(new \PKP\components\forms\FieldText('key_secret', [
'label' => 'Key Secret',
'value' => $this->getSetting($context->getId(), 'key_secret'),
'groupId' => $groupid,
]));
return;
}
/**
* @copydoc PaymethodPlugin::saveSettings()
*/
public function saveSettings($params, $slimRequest, $request)
{
$allParams = $slimRequest->getParsedBody();
$saveParams = [];
foreach ($allParams as $param => $val) {
switch ($param) {
case 'key_id':
case 'key_secret':
$saveParams[$param] = (string) $val;
break;
}
}
$contextId = $request->getContext()->getId();
foreach ($saveParams as $param => $val) {
$this->updateSetting($contextId, $param, $val);
}
return [];
}
/**
* @copydoc PaymethodPlugin::getPaymentForm()
*/
function getPaymentForm($context, $queuedPayment)
{
$this->import('RazorPayForm');
return new RazorPayForm($this, $queuedPayment);
}
/**
* @copydoc PaymethodPlugin::isConfigured
*/
function isConfigured($context)
{
if (!$context) return false;
if ($this->getSetting($context->getId(), 'key_id') == '') return false;
if ($this->getSetting($context->getId(), 'key_secret') == '') return false;
return true;
}
function handle($args, $request)
{
$journal = $request->getJournal();
$queuedPaymentDao = DAORegistry::getDAO('QueuedPaymentDAO'); /* @var $queuedPaymentDao QueuedPaymentDAO */
import('classes.payment.ojs.OJSPaymentManager');
try {
$queuedPayment = $queuedPaymentDao->getById($queuedPaymentId = $request->getUserVar('queuedPaymentId'));
// Prevent errors when users are automatically logged out by OJS by assigning users to the registry
if (!Validation::isLoggedIn()) {
// Validation::redirectLogin();
$userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
$user = $userDao->getById($queuedPayment->getUserId());
Registry::set('user', $user);
};
if (!$queuedPayment) throw new \Exception("Invalid queued payment ID $queuedPaymentId!");
if ($error = $request->getUserVar('error')) {
$description = array_key_exists('description', $error) ? $error['description'] : 'Payment failed';
throw new \Exception($description);
}
$api = $this->getApi();
$attributes = array('razorpay_signature' => $request->getUserVar('razorpay_signature'), 'razorpay_payment_id' => $request->getUserVar('razorpay_payment_id'), 'razorpay_order_id' => $request->getUserVar('razorpay_order_id'));
$api->utility->verifyPaymentSignature($attributes);
$paymentManager = Application::getPaymentManager($journal);
$paymentManager->fulfillQueuedPayment($request, $queuedPayment, $this->getName());
$request->redirectUrl($queuedPayment->getRequestUrl());
} catch (\Throwable $th) {
error_log('RazorPay transaction exception: ' . $th->getMessage());
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('messageTranslated', 'A transaction error occurred. Please contact the journal manager for details.');
$templateMgr->display('frontend/pages/message.tpl');
}
}
public function getContextSpecificPluginVersionFile()
{
return $this->getPluginPath() . '/version.xml';
}
public function getPluginVersion()
{
import('lib.pkp.classes.site.VersionCheck');
$version = VersionCheck::parseVersionXML($this->getContextSpecificPluginVersionFile());
return $version['release'];
}
function getApi()
{
return new Api($this->getSetting($this->getCurrentContextId(), 'key_id'), $this->getSetting($this->getCurrentContextId(), 'key_secret'));
}
}