16 import(
'lib.pkp.classes.plugins.PaymethodPlugin');
17 require_once(dirname(__FILE__) .
'/vendor/autoload.php');
25 return 'PaypalPayment';
32 return __(
'plugins.paymethod.paypal.displayName');
39 return __(
'plugins.paymethod.paypal.description');
45 function register($category, $path, $mainContextId =
null) {
46 if (parent::register($category, $path, $mainContextId)) {
61 if ($form->id !== FORM_PAYMENT_SETTINGS) {
71 'id' =>
'paypalpayment',
72 'label' => __(
'plugins.paymethod.paypal.displayName'),
73 'showWhen' =>
'paymentsEnabled',
76 'label' => __(
'plugins.paymethod.paypal.settings.testMode'),
78 [
'value' =>
true,
'label' => __(
'common.enable')]
80 'value' => (
bool) $this->
getSetting($context->getId(),
'testMode'),
81 'groupId' =>
'paypalpayment',
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',
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',
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',
106 $allParams = $slimRequest->getParsedBody();
108 foreach ($allParams as $param => $val) {
113 $saveParams[$param] = (string) $val;
116 $saveParams[$param] = $val ===
'true';
120 $contextId =
$request->getContext()->getId();
121 foreach ($saveParams as $param => $val) {
131 $this->
import(
'PaypalPaymentForm');
139 if (!$context)
return false;
140 if ($this->
getSetting($context->getId(),
'accountName') ==
'')
return false;
150 import(
'classes.payment.ojs.OJSPaymentManager');
152 $queuedPayment = $queuedPaymentDao->getById($queuedPaymentId =
$request->getUserVar(
'queuedPaymentId'));
153 if (!$queuedPayment)
throw new \Exception(
"Invalid queued payment ID $queuedPaymentId!");
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'),
161 $transaction = $gateway->completePurchase(array(
162 'payer_id' =>
$request->getUserVar(
'PayerID'),
163 'transactionReference' =>
$request->getUserVar(
'paymentId'),
165 $response = $transaction->send();
166 if (!$response->isSuccessful())
throw new \Exception($response->getMessage());
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!');
175 $paymentManager->fulfillQueuedPayment(
$request, $queuedPayment, $this->
getName());
176 $request->redirectUrl($queuedPayment->getRequestUrl());
177 }
catch (\Exception $e) {
178 error_log(
'PayPal transaction exception: ' . $e->getMessage());
180 $templateMgr->assign(
'message',
'plugins.paymethod.paypal.error');
181 $templateMgr->display(
'frontend/pages/message.tpl');
189 return ($this->
getPluginPath() . DIRECTORY_SEPARATOR .
'emailTemplates.xml');