|
| 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 | +} |
0 commit comments