public function initiate($data = array())
    {
        if ($this->payment->Status !== 'Created') {
            throw new InvalidStateException('Cannot initiate a purchase with this payment. Status is not "Created"');
        }

        if (!$this->payment->isInDB()) {
            $this->payment->write();
        }

        $gateway = $this->oGateway();
        if (!$gateway->supportsPurchase()) {
            throw new InvalidConfigurationException(
                sprintf('The gateway "%s" doesn\'t support purchase', $this->payment->Gateway)
            );
        }

        $gatewayData = $this->gatherGatewayData($data);

        $this->extend('onBeforePurchase', $gatewayData);
        $request = $this->oGateway()->purchase($gatewayData);
        $this->extend('onAfterPurchase', $request);

        $this->createMessage(Message\PurchaseRequest::class, $request);

        try {
            $response = $this->response = $request->send();
        } catch (\Omnipay\Common\Exception\OmnipayException $e) {
            $this->createMessage(Message\PurchaseError::class, $e);
            // create an error response
            return $this->generateServiceResponse(ServiceResponse::SERVICE_ERROR);
        }

        ErrorHandling::safeExtend($this, 'onAfterSendPurchase', $request, $response);

        $serviceResponse = $this->wrapOmnipayResponse($response);

        if ($serviceResponse->isRedirect() || $serviceResponse->isAwaitingNotification()) {
            $this->payment->Status = 'PendingPurchase';
            $this->payment->write();

            $this->createMessage(
                $serviceResponse->isRedirect() ? Message\PurchaseRedirectResponse::class : Message\AwaitingPurchaseResponse::class,
                $response
            );
        } elseif ($serviceResponse->isError()) {
            $this->createMessage(Message\PurchaseError::class, $response);
        } else {
            $this->markCompleted('Captured', $serviceResponse, $response);
        }

        return $serviceResponse;
    }
public function initiate($data = array())
    {
        if ($this->payment->Status !== 'Created') {
            throw new InvalidStateException('Cannot initiate a purchase with this payment. Status is not "Created"');
        }

        if (!$this->payment->isInDB()) {
            $this->payment->write();
        }

        $gateway = $this->oGateway();
        if (!$gateway->supportsPurchase()) {
            throw new InvalidConfigurationException(
                sprintf('The gateway "%s" doesn\'t support purchase', $this->payment->Gateway)
            );
        }

        $gatewayData = $this->gatherGatewayData($data);

        $this->extend('onBeforePurchase', $gatewayData);
        $request = $this->oGateway()->purchase($gatewayData);
        $this->extend('onAfterPurchase', $request);

        $this->createMessage(Message\PurchaseRequest::class, $request);

        try {
            $response = $this->response = $request->send();
        } catch (\Omnipay\Common\Exception\OmnipayException $e) {
            $this->createMessage(Message\PurchaseError::class, $e);
            // create an error response
            return $this->generateServiceResponse(ServiceResponse::SERVICE_ERROR);
        }

        ErrorHandling::safeExtend($this, 'onAfterSendPurchase', $request, $response);

        $serviceResponse = $this->wrapOmnipayResponse($response);

        if ($serviceResponse->isRedirect() || $serviceResponse->isAwaitingNotification()) {
            $this->payment->Status = 'PendingPurchase';
            $this->payment->write();

            $this->createMessage(
                $serviceResponse->isRedirect() ? Message\PurchaseRedirectResponse::class : Message\AwaitingPurchaseResponse::class,
                $response
            );
        } elseif ($serviceResponse->isError()) {
            $this->createMessage(Message\PurchaseError::class, $response);
        } else {
            $this->markCompleted('Captured', $serviceResponse, $response);
        }

        return $serviceResponse;
    }
public function initiate ( $ data = array ( ) ) { if ( $ this -> payment -> Status !== 'Created' ) { throw new InvalidStateException ( 'Cannot initiate a purchase with this payment. Status is not "Created"' ) ; } if ( ! $ this -> payment -> isInDB ( ) ) { $ this -> payment -> write ( ) ; } $ gateway = $ this -> oGateway ( ) ; if ( ! $ gateway -> supportsPurchase ( ) ) { throw new InvalidConfigurationException ( sprintf ( 'The gateway "%s" doesn\'t support purchase' , $ this -> payment -> Gateway ) ) ; } $ gatewayData = $ this -> gatherGatewayData ( $ data ) ; $ this -> extend ( 'onBeforePurchase' , $ gatewayData ) ; $ request = $ this -> oGateway ( ) -> purchase ( $ gatewayData ) ; $ this -> extend ( 'onAfterPurchase' , $ request ) ; $ this -> createMessage ( Message \ PurchaseRequest :: class , $ request ) ; try { $ response = $ this -> response = $ request -> send ( ) ; } catch ( \ Omnipay \ Common \ Exception \ OmnipayException $ e ) { $ this -> createMessage ( Message \ PurchaseError :: class , $ e ) ; // create an error response return $ this -> generateServiceResponse ( ServiceResponse :: SERVICE_ERROR ) ; } ErrorHandling :: safeExtend ( $ this , 'onAfterSendPurchase' , $ request , $ response ) ; $ serviceResponse = $ this -> wrapOmnipayResponse ( $ response ) ; if ( $ serviceResponse -> isRedirect ( ) || $ serviceResponse -> isAwaitingNotification ( ) ) { $ this -> payment -> Status = 'PendingPurchase' ; $ this -> payment -> write ( ) ; $ this -> createMessage ( $ serviceResponse -> isRedirect ( ) ? Message \ PurchaseRedirectResponse :: class : Message \ AwaitingPurchaseResponse :: class , $ response ) ; } elseif ( $ serviceResponse -> isError ( ) ) { $ this -> createMessage ( Message \ PurchaseError :: class , $ response ) ; } else { $ this -> markCompleted ( 'Captured' , $ serviceResponse , $ response ) ; } return $ serviceResponse ; }
Attempt to make a payment.

@inheritdoc
@param  array $data returnUrl/cancelUrl + customer creditcard and billing/shipping details.
Some keys (e.g. "amount") are overwritten with data from the associated {@link $payment}.
If this array is constructed from user data (e.g. a form submission), please take care
to whitelist accepted fields, in order to ensure sensitive gateway parameters like "freeShipping" can't be set.
If using {@link Form->getData()}, only fields which exist in the form are returned,
effectively whitelisting against arbitrary user input.
Attempt to make a payment .