Source of file SagePayExtension.php
Size: 4,511 Bytes - Last Modified: 2021-12-23T10:34:19+00:00
/var/www/docs.ssmods.com/process/src/src/Extensions/SagePayExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 | <?php namespace SilverStripe\Omnipay\Extensions; use Omnipay\SagePay\Message\ServerNotifyRequest; use SilverStripe\Omnipay\Model\Message\PurchaseRedirectResponse; use SilverStripe\Omnipay\Service\ServiceResponse; use SilverStripe\Core\Extension; use SilverStripe\Omnipay\Model\Message; use SilverStripe\Omnipay\Model\Payment; use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\Director; /** * Sagepay has some very indiviual needs so to help we have created this * extension that you can use as a beginning to try and help your SagePay * transaction to go through. Add it to your YML file e.g. * * SilverStripe\Omnipay\Service\PaymentService: * extensions: * - SagePayExtension * * The extension adds following: * - A Description into the initial request (reequired by SagePay) * - The proper transactionReference which is recieved back from SagePay * - A response to the notification (See use_async_notification) * * @see https://github.com/silverstripe/silverstripe-omnipay/issues/153 * @see https://github.com/silverstripe/silverstripe-omnipay/issues/159 */ class SagePayExtension extends Extension { /** * @param array $gatewayData */ public function onBeforePurchase(array &$gatewayData) { $this->addDescription($gatewayData); } /** * @param array $gatewayData */ public function onBeforeAuthorize(array &$gatewayData) { $this->addDescription($gatewayData); } /** * @param ServiceResponse $response */ public function updateServiceResponse($response) { $payment = $response->getPayment(); // We only want to respond to the notification if we are using SagePay if ($payment->Gateway !== 'SagePay_Server') { return; } $this->respondToNotification($response, $payment); } /** * @param array $gatewayData */ public function onBeforeCompletePurchase(array &$gatewayData) { $this->addTransactionReference($gatewayData); } /** * @param array $gatewayData */ public function onBeforeCompleteAuthorize(array &$gatewayData) { $this->addTransactionReference($gatewayData, true); } /** * Grabs the transactionReference from the previous received message and adds it to the * gateway data, so that it can be sent back to SagePay as clarification * @param array $gatewayData incoming gateway data * @param bool $isAuthorize whether or not we're dealing with a complete authorize request */ private function addTransactionReference(array &$gatewayData, $isAuthorize = false) { /** @var Payment $payment */ $payment = $this->owner->getPayment(); // Only apply the changes if the gateway is SagePay Server if ($payment->Gateway == 'SagePay_Server') { $type = ($isAuthorize) ? Message\AuthorizeRedirectResponse::class : Message\PurchaseRedirectResponse::class; /** @var PurchaseRedirectResponse $message */ $message = $payment->getLatestMessageOfType($type); $gatewayData['transactionReference'] = $message->Reference; } } /** * Description for SagePay must be < 100 characters * @param $gatewayData */ private function addDescription(array &$gatewayData) { $payment = $this->owner->getPayment(); if ($payment->Gateway == 'SagePay_Direct' || $payment->Gateway == 'SagePay_Server') { $gatewayData['description'] = sprintf( 'Payment made on %s', date('D j M Y') ); } } /** * Used to respond to the SagePay notification * @param ServiceResponse $response * @param Payment $payment */ private function respondToNotification(ServiceResponse $response, Payment $payment) { $omnipayResponse = $response->getOmnipayResponse(); if ($omnipayResponse !== null && $omnipayResponse->isSuccessful() && $response->isNotification() && !$response->isError() ) { $msg = [ 'Status=' . ServerNotifyRequest::RESPONSE_STATUS_OK, 'RedirectUrl=' . Director::absoluteURL($payment->SuccessUrl), 'StatusDetail=Accepted payment ' . $payment->Identifier ]; $response->setHttpResponse(new HTTPResponse(implode(ServerNotifyRequest::LINE_SEP, $msg), 200)); } } } |