Open Journal Systems  3.3.0
PaypalPaymentPlugin.inc.php
1 <?php
2 
16 import('lib.pkp.classes.plugins.PaymethodPlugin');
17 require_once(dirname(__FILE__) . '/vendor/autoload.php');
18 
20 
24  function getName() {
25  return 'PaypalPayment';
26  }
27 
31  function getDisplayName() {
32  return __('plugins.paymethod.paypal.displayName');
33  }
34 
38  function getDescription() {
39  return __('plugins.paymethod.paypal.description');
40  }
41 
45  function register($category, $path, $mainContextId = null) {
46  if (parent::register($category, $path, $mainContextId)) {
47  $this->addLocaleData();
48  \HookRegistry::register('Form::config::before', array($this, 'addSettings'));
49  return true;
50  }
51  return false;
52  }
53 
60  public function addSettings($hookName, $form) {
61  if ($form->id !== FORM_PAYMENT_SETTINGS) {
62  return;
63  }
64 
65  $context = Application::get()->getRequest()->getContext();
66  if (!$context) {
67  return;
68  }
69 
70  $form->addGroup([
71  'id' => 'paypalpayment',
72  'label' => __('plugins.paymethod.paypal.displayName'),
73  'showWhen' => 'paymentsEnabled',
74  ])
75  ->addField(new \PKP\components\forms\FieldOptions('testMode', [
76  'label' => __('plugins.paymethod.paypal.settings.testMode'),
77  'options' => [
78  ['value' => true, 'label' => __('common.enable')]
79  ],
80  'value' => (bool) $this->getSetting($context->getId(), 'testMode'),
81  'groupId' => 'paypalpayment',
82  ]))
83  ->addField(new \PKP\components\forms\FieldText('accountName', [
84  'label' => __('plugins.paymethod.paypal.settings.accountName'),
85  'value' => $this->getSetting($context->getId(), 'accountName'),
86  'groupId' => 'paypalpayment',
87  ]))
88  ->addField(new \PKP\components\forms\FieldText('clientId', [
89  'label' => __('plugins.paymethod.paypal.settings.clientId'),
90  'value' => $this->getSetting($context->getId(), 'clientId'),
91  'groupId' => 'paypalpayment',
92  ]))
93  ->addField(new \PKP\components\forms\FieldText('secret', [
94  'label' => __('plugins.paymethod.paypal.settings.secret'),
95  'value' => $this->getSetting($context->getId(), 'secret'),
96  'groupId' => 'paypalpayment',
97  ]));
98 
99  return;
100  }
101 
105  public function saveSettings($params, $slimRequest, $request) {
106  $allParams = $slimRequest->getParsedBody();
107  $saveParams = [];
108  foreach ($allParams as $param => $val) {
109  switch ($param) {
110  case 'accountName':
111  case 'clientId':
112  case 'secret':
113  $saveParams[$param] = (string) $val;
114  break;
115  case 'testMode':
116  $saveParams[$param] = $val === 'true';
117  break;
118  }
119  }
120  $contextId = $request->getContext()->getId();
121  foreach ($saveParams as $param => $val) {
122  $this->updateSetting($contextId, $param, $val);
123  }
124  return [];
125  }
126 
130  function getPaymentForm($context, $queuedPayment) {
131  $this->import('PaypalPaymentForm');
132  return new PaypalPaymentForm($this, $queuedPayment);
133  }
134 
138  function isConfigured($context) {
139  if (!$context) return false;
140  if ($this->getSetting($context->getId(), 'accountName') == '') return false;
141  return true;
142  }
143 
147  function handle($args, $request) {
148  $journal = $request->getJournal();
149  $queuedPaymentDao = DAORegistry::getDAO('QueuedPaymentDAO'); /* @var $queuedPaymentDao QueuedPaymentDAO */
150  import('classes.payment.ojs.OJSPaymentManager'); // Class definition required for unserializing
151  try {
152  $queuedPayment = $queuedPaymentDao->getById($queuedPaymentId = $request->getUserVar('queuedPaymentId'));
153  if (!$queuedPayment) throw new \Exception("Invalid queued payment ID $queuedPaymentId!");
154 
155  $gateway = Omnipay\Omnipay::create('PayPal_Rest');
156  $gateway->initialize(array(
157  'clientId' => $this->getSetting($journal->getId(), 'clientId'),
158  'secret' => $this->getSetting($journal->getId(), 'secret'),
159  'testMode' => $this->getSetting($journal->getId(), 'testMode'),
160  ));
161  $transaction = $gateway->completePurchase(array(
162  'payer_id' => $request->getUserVar('PayerID'),
163  'transactionReference' => $request->getUserVar('paymentId'),
164  ));
165  $response = $transaction->send();
166  if (!$response->isSuccessful()) throw new \Exception($response->getMessage());
167 
168  $data = $response->getData();
169  if ($data['state'] != 'approved') throw new \Exception('State ' . $data['state'] . ' is not approved!');
170  if (count($data['transactions']) != 1) throw new \Exception('Unexpected transaction count!');
171  $transaction = $data['transactions'][0];
172  if ((float) $transaction['amount']['total'] != (float) $queuedPayment->getAmount() || $transaction['amount']['currency'] != $queuedPayment->getCurrencyCode()) throw new \Exception('Amounts (' . $transaction['amount']['total'] . ' ' . $transaction['amount']['currency'] . ' vs ' . $queuedPayment->getAmount() . ' ' . $queuedPayment->getCurrencyCode() . ') don\'t match!');
173 
174  $paymentManager = Application::getPaymentManager($journal);
175  $paymentManager->fulfillQueuedPayment($request, $queuedPayment, $this->getName());
176  $request->redirectUrl($queuedPayment->getRequestUrl());
177  } catch (\Exception $e) {
178  error_log('PayPal transaction exception: ' . $e->getMessage());
179  $templateMgr = TemplateManager::getManager($request);
180  $templateMgr->assign('message', 'plugins.paymethod.paypal.error');
181  $templateMgr->display('frontend/pages/message.tpl');
182  }
183  }
184 
189  return ($this->getPluginPath() . DIRECTORY_SEPARATOR . 'emailTemplates.xml');
190  }
191 }
PaypalPaymentPlugin\getDescription
getDescription()
Definition: PaypalPaymentPlugin.inc.php:38
PaypalPaymentPlugin\addSettings
addSettings($hookName, $form)
Definition: PaypalPaymentPlugin.inc.php:60
PaypalPaymentPlugin\handle
handle($args, $request)
Definition: PaypalPaymentPlugin.inc.php:147
Plugin\updateSetting
updateSetting($contextId, $name, $value, $type=null)
Definition: Plugin.inc.php:495
PKP
PaymethodPlugin
Definition: PaymethodPlugin.inc.php:18
FieldOptions
A field to select from a set of checkbox or radio options.
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PaypalPaymentPlugin\getPaymentForm
getPaymentForm($context, $queuedPayment)
Definition: PaypalPaymentPlugin.inc.php:130
Application\getPaymentManager
static getPaymentManager($context)
Definition: Application.inc.php:226
PaypalPaymentPlugin\isConfigured
isConfigured($context)
Definition: PaypalPaymentPlugin.inc.php:138
PaypalPaymentPlugin
Paypal payment plugin class.
Definition: PaypalPaymentPlugin.inc.php:19
Plugin\getSetting
getSetting($contextId, $name)
Definition: Plugin.inc.php:473
PaypalPaymentForm
Definition: PaypalPaymentForm.inc.php:18
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
Plugin\getPluginPath
getPluginPath()
Definition: Plugin.inc.php:330
Plugin\$request
$request
Definition: Plugin.inc.php:68
Plugin\addLocaleData
addLocaleData($locale=null)
Definition: Plugin.inc.php:454
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
FieldText
A basic text field in a form.
PaypalPaymentPlugin\getInstallEmailTemplatesFile
getInstallEmailTemplatesFile()
Definition: PaypalPaymentPlugin.inc.php:188
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
PaypalPaymentPlugin\saveSettings
saveSettings($params, $slimRequest, $request)
Definition: PaypalPaymentPlugin.inc.php:105
PaypalPaymentPlugin\getName
getName()
Definition: PaypalPaymentPlugin.inc.php:24
PaypalPaymentPlugin\getDisplayName
getDisplayName()
Definition: PaypalPaymentPlugin.inc.php:31