Skip to content

Commit e9db62a

Browse files
committed
Added form processor integration
1 parent 52d019d commit e9db62a

File tree

5 files changed

+199
-2
lines changed

5 files changed

+199
-2
lines changed

CRM/Core/Payment/OmnipayMultiProcessor.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ protected function getCreditCardOptions(array $params): array {
572572
'description' => substr($params['description'], 0, 64),
573573
'transactionId' => $this->formatted_transaction_id,
574574
'clientIp' => CRM_Utils_System::ipAddress(),
575-
'returnUrl' => $this->getNotifyUrl(TRUE),
575+
'returnUrl' => $this->getReturnUrl(),
576576
'cancelUrl' => $this->getCancelUrl($this->getQfKey(), CRM_Utils_Array::value('participantID', $params)),
577577
'errorUrl' => $this->getReturnFailUrl($this->getQfKey(), $participantID, $eventID),
578578
'notifyUrl' => $this->getNotifyUrl(),
@@ -1160,8 +1160,14 @@ public function doPreApproval(&$params) {
11601160
// This is kinda tricky - in that it's not denoted on that class anywhere
11611161
// & as we integrate more we might need to refine this early return
11621162
// to be metadata based or to have some jsv4 specific paypal class.
1163-
return ['pre_approval_parameters' => ['token' => $response->getTransactionReference()]];
1163+
return [
1164+
'pre_approval_parameters' => ['token' => $response->getTransactionReference()],
1165+
'redirect_url' => $response->getRedirectURL(),
1166+
];
11641167
}
1168+
return [
1169+
'redirect_url' => $response->getRedirectURL(),
1170+
];
11651171
/*
11661172
* This is what we expect to do but no current processors.
11671173
$isTransparentRedirect = ($response->isTransparentRedirect() || !empty($this->gateway->transparentRedirect));

CRM/Core/Payment/PaymentExtended.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ abstract class CRM_Core_Payment_PaymentExtended extends CRM_Core_Payment {
8080
*/
8181
protected $history = [];
8282

83+
/**
84+
* @var String
85+
*/
86+
protected $returnUrl;
87+
8388
/**
8489
* @return \CRM_Utils_SystemLogger
8590
*/
@@ -129,6 +134,31 @@ protected function getReturnSuccessUrl($qfKey) {
129134
);
130135
}
131136

137+
/**
138+
* Get the URL for return.
139+
* Default value is the notify url
140+
*
141+
* @return string
142+
*/
143+
protected function getReturnUrl() {
144+
if (isset($this->returnUrl)) {
145+
return $this->returnUrl;
146+
}
147+
return $this->getNotifyUrl(TRUE);
148+
}
149+
150+
/**
151+
* Sets or unsets the return url.
152+
* If the return url is not set it is set to notify url
153+
*
154+
* @param string|NULL $returnUrl
155+
*
156+
* @return void
157+
*/
158+
public function setReturnUrl(string $returnUrl = null) {
159+
$this->returnUrl = $returnUrl;
160+
}
161+
132162
/**
133163
* @param $key
134164
* @param null $participantID
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Civi\OmnipayMultiProcessor\ActionProvider;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use CRM_Omnipaymultiprocessor_ExtensionUtil as E;
8+
9+
class CompilerPass implements CompilerPassInterface {
10+
11+
public function process(ContainerBuilder $container) {
12+
if ($container->hasDefinition('action_provider')) {
13+
$actionProviderDefinition = $container->getDefinition('action_provider');
14+
$actionProviderDefinition->addMethodCall('addAction',
15+
[
16+
'OmnipayMultiProcessor_OnlinePayment',
17+
'Civi\OmnipayMultiProcessor\ActionProvider\DoOnlinePayment',
18+
E::ts('Contribution: Do Online Payment with Omnipay Multi Processor'),
19+
[]
20+
]);
21+
}
22+
23+
}
24+
25+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
4+
namespace Civi\OmnipayMultiProcessor\ActionProvider;
5+
6+
use Civi\ActionProvider\Action\AbstractAction;
7+
use Civi\ActionProvider\Exception\ExecutionException;
8+
use Civi\ActionProvider\Parameter\OptionGroupSpecification;
9+
use Civi\ActionProvider\Parameter\ParameterBagInterface;
10+
use Civi\ActionProvider\Parameter\Specification;
11+
use Civi\ActionProvider\Parameter\SpecificationBag;
12+
use Civi\API\Exception\UnauthorizedException;
13+
use Civi\Api4\PaymentProcessor;
14+
use CRM_Core_Exception;
15+
use CRM_Omnipaymultiprocessor_ExtensionUtil as E;
16+
17+
class DoOnlinePayment extends AbstractAction {
18+
19+
/**
20+
* @var array
21+
*/
22+
protected $paymentProcessors;
23+
24+
/**
25+
* Run the action
26+
*
27+
* @param ParameterBagInterface $parameters
28+
* The parameters to this action.
29+
* @param ParameterBagInterface $output
30+
* The parameters this action can send back
31+
* @return void
32+
* @throws \Exception
33+
*/
34+
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
35+
$paymentParams['contribution_id'] = $parameters->getParameter('contribution_id');
36+
$paymentParams['amount'] = (float) $parameters->getParameter('total_amount');
37+
$paymentParams['currency'] = $parameters->getParameter('currency');
38+
$paymentParams['description'] = $parameters->getParameter('description');
39+
$successUrl = $parameters->getParameter('success_url');
40+
$cancelurl = $parameters->getParameter('cancel_url');
41+
42+
$paymentProcessor = $this->getPaymentProcessorByName($this->configuration->getParameter('payment_processor'));
43+
if (!$paymentProcessor) {
44+
throw new ExecutionException('Invalid Payment Processor');
45+
}
46+
$payment = \Civi\Payment\System::singleton()->getByProcessor($paymentProcessor);
47+
if ($payment instanceof \CRM_Core_Payment_PaymentExtended) {
48+
$payment->setReturnUrl($successUrl);
49+
}
50+
$payment->setBaseReturnUrl($successUrl);
51+
$payment->setSuccessUrl($successUrl);
52+
$payment->setCancelUrl($cancelurl);
53+
$result = $payment->doPreApproval($paymentParams);
54+
$output->setParameter('redirect_url', $result['redirect_url']);
55+
}
56+
57+
/**
58+
* @return \Civi\ActionProvider\Parameter\SpecificationBag
59+
*/
60+
public function getOutputSpecification() {
61+
return new SpecificationBag(array(
62+
new Specification('redirect_url', 'String', E::ts('Redirect URL'), false),
63+
));
64+
}
65+
66+
/**
67+
* Returns the specification of the configuration options for the actual action.
68+
*
69+
* @return SpecificationBag
70+
*/
71+
public function getConfigurationSpecification() {
72+
$paymentProcessorOptions = [];
73+
foreach($this->getPaymentProcessors() as $paymentProcessor) {
74+
$paymentProcessorOptions[$paymentProcessor['name']] = $paymentProcessor['title'];
75+
}
76+
return new SpecificationBag(array(
77+
new Specification('payment_processor', 'String', E::ts('Payment Processor'), TRUE, null, null, $paymentProcessorOptions),
78+
));
79+
}
80+
81+
/**
82+
* Returns the specification of the parameters of the actual action.
83+
*
84+
* @return SpecificationBag
85+
*/
86+
public function getParameterSpecification() {
87+
return new SpecificationBag([
88+
new Specification('contribution_id', 'Integer', E::ts('Contribution ID'), TRUE),
89+
new Specification('total_amount', 'Float', E::ts('Amount'), TRUE),
90+
new OptionGroupSpecification('currency', 'currencies_enabled', E::ts('Currency'), TRUE),
91+
new Specification('success_url', 'String', E::ts('Success URL'), TRUE),
92+
new Specification('cancel_url', 'String', E::ts('Cancel URL'), TRUE),
93+
new Specification('description', 'String', E::ts('Payment Description'), TRUE),
94+
]);
95+
}
96+
97+
public function getHelpText() {
98+
return E::ts('This action creates an online payment at a payment processor and returns the url at which the user can finish the payment');
99+
}
100+
101+
private function getPaymentProcessors() {
102+
if (empty($this->paymentProcessors)) {
103+
try {
104+
$this->paymentProcessors = PaymentProcessor::get(FALSE)
105+
->addWhere('is_active', '=', TRUE)
106+
->execute()
107+
->getArrayCopy();
108+
}
109+
catch (UnauthorizedException|CRM_Core_Exception $e) {
110+
}
111+
}
112+
return $this->paymentProcessors;
113+
}
114+
115+
private function getPaymentProcessorByName(string $name):? array {
116+
foreach($this->getPaymentProcessors() as $paymentProcessor) {
117+
if ($paymentProcessor['name'] == $name) {
118+
return $paymentProcessor;
119+
}
120+
}
121+
return null;
122+
}
123+
124+
}

omnipaymultiprocessor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
require_once 'omnipaymultiprocessor.civix.php';
44
use CRM_Omnipaymultiprocessor_ExtensionUtil as E;
5+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
6+
use \Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
/**
9+
* Implements hook_civicrm_container()
10+
*
11+
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_container/
12+
*/
13+
function omnipaymultiprocessor_civicrm_container(ContainerBuilder $container) {
14+
$container->addCompilerPass(new Civi\OmnipayMultiProcessor\ActionProvider\CompilerPass(), PassConfig::TYPE_OPTIMIZE);
15+
}
16+
517

618
/**
719
* Implementation of hook_civicrm_config

0 commit comments

Comments
 (0)