Source of file AuthorizeService.php
Size: 5,368 Bytes - Last Modified: 2021-12-24T06:34:53+00:00
/var/www/docs.ssmods.com/process/src/src/Service/AuthorizeService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 | <?php namespace SilverStripe\Omnipay\Service; use SilverStripe\Omnipay\Exception\InvalidStateException; use SilverStripe\Omnipay\Exception\InvalidConfigurationException; use SilverStripe\Omnipay\Helper\ErrorHandling; use SilverStripe\Omnipay\Model\Message\AuthorizeRequest; use SilverStripe\Omnipay\Model\Message\AuthorizedResponse; use SilverStripe\Omnipay\Model\Message\CompleteAuthorizeError; use SilverStripe\Omnipay\Model\Message\AuthorizeError; use SilverStripe\Omnipay\Model\Message\AuthorizeRedirectResponse; use SilverStripe\Omnipay\Model\Message\AwaitingAuthorizeResponse; use SilverStripe\Omnipay\Model\Message\CompleteAuthorizeRequest; class AuthorizeService extends PaymentService { /** * Start an authorization request * * @inheritdoc */ public function initiate($data = array()) { if ($this->payment->Status !== 'Created') { throw new InvalidStateException('Cannot authorize this payment. Status is not "Created"'); } if (!$this->payment->isInDB()) { $this->payment->write(); } $gateway = $this->oGateway(); if (!$gateway->supportsAuthorize()) { throw new InvalidConfigurationException( sprintf('The gateway "%s" doesn\'t support authorize', $this->payment->Gateway) ); } $gatewayData = $this->gatherGatewayData($data); $this->extend('onBeforeAuthorize', $gatewayData); $request = $this->oGateway()->authorize($gatewayData); $this->extend('onAfterAuthorize', $request); $this->createMessage(AuthorizeRequest::class, $request); try { $response = $this->response = $request->send(); } catch (\Omnipay\Common\Exception\OmnipayException $e) { $this->createMessage(AuthorizeError::class, $e); // create an error response return $this->generateServiceResponse(ServiceResponse::SERVICE_ERROR); } ErrorHandling::safeExtend($this, 'onAfterSendAuthorize', $request, $response); $serviceResponse = $this->wrapOmnipayResponse($response); if ($serviceResponse->isRedirect() || $serviceResponse->isAwaitingNotification()) { $this->payment->Status = 'PendingAuthorization'; $this->payment->write(); $this->createMessage( $serviceResponse->isRedirect() ? AuthorizeRedirectResponse::class : AwaitingAuthorizeResponse::class, $response ); } elseif ($serviceResponse->isError()) { $this->createMessage(AuthorizeError::class, $response); } else { $this->markCompleted('Authorized', $serviceResponse, $response); } return $serviceResponse; } /** * Finalise this authorization, after off-site external processing. * This is usually only called by PaymentGatewayController. * @inheritdoc */ public function complete($data = array(), $isNotification = false) { $flags = $isNotification ? ServiceResponse::SERVICE_NOTIFICATION : 0; // The payment is already captured if ($this->payment->Status === 'Authorized') { return $this->generateServiceResponse($flags); } if ($this->payment->Status !== 'PendingAuthorization') { throw new InvalidStateException('Cannot complete this payment. Status is not "PendingAuthorization"'); } $gateway = $this->oGateway(); if (!$gateway->supportsCompleteAuthorize()) { throw new InvalidConfigurationException( sprintf('The gateway "%s" doesn\'t support completeAuthorize', $this->payment->Gateway) ); } // purchase and completePurchase should use the same data $gatewayData = $this->gatherGatewayData($data); $this->extend('onBeforeCompleteAuthorize', $gatewayData); $request = $gateway->completeAuthorize($gatewayData); $this->extend('onAfterCompleteAuthorize', $request); $this->createMessage(CompleteAuthorizeRequest::class, $request); $response = null; try { $response = $this->response = $request->send(); } catch (\Omnipay\Common\Exception\OmnipayException $e) { $this->createMessage(CompleteAuthorizeError::class, $e); return $this->generateServiceResponse($flags | ServiceResponse::SERVICE_ERROR); } $serviceResponse = $this->wrapOmnipayResponse($response, $isNotification); if ($serviceResponse->isError()) { $this->createMessage(CompleteAuthorizeError::class, $response); return $serviceResponse; } if (!$serviceResponse->isAwaitingNotification()) { $this->markCompleted('Authorized', $serviceResponse, $response); } else { ErrorHandling::safeExtend($this->payment, 'onAwaitingAuthorized', $serviceResponse); } return $serviceResponse; } protected function markCompleted($endStatus, ServiceResponse $serviceResponse, $gatewayMessage) { parent::markCompleted($endStatus, $serviceResponse, $gatewayMessage); $this->createMessage(AuthorizedResponse::class, $gatewayMessage); ErrorHandling::safeExtend($this->payment, 'onAuthorized', $serviceResponse); } } |