simnom
3/27/2015 - 10:39 AM

Potential callback method for OmniPay Sage P

Potential callback method for OmniPay Sage P

public function callback()
    {
        $this->load->config('payments/sagepay_server');

        if ($this->form_validation->run('callback') === true) {
            $post_array = $this->form_validation->import_post($this->input->post(), 'callback');

            // Load order record
            $order = new Order_m($post_array['VendorTxCode']);

            //Validate the post array and transaction details
            $response = $this->gateway->completePurchase([
                'amount' => $order->total,
                'transactionId' => $order->id,
                'transactionReference' => $order->sagepay_transaction_reference
                ])->send();

            if (! $response->isValid()) {
                log_message('info', 'Transaction ID: ' . $order->id . ' - Security token invalid');
                $response->invalid($this->config->item('failure_url'), 'Security token invalid');
            }

            if ($response->isSuccessful()) {
                //Process the successful transaction information
                $order->authorise($response->getData());

                log_message('info', 'Transaction ID: ' . $order->id . ' - Successfully processed');

                //Send the confirm response back to Sage Pay
                $response->confirm(
                    $this->config->item('success_url') . '?tx=' . $response->getData()['VendorTxCode'] . '&type=CC',
                    'Transaction processed sucessfully'
                );

            } else {
                $order->reject($response->getData());

                log_message('info', 'Transaction ID: ' . $order->id . ' - Rejected - ' . $response->getData()['Status']);

                //Sage Pay response for failed transactions
                $response->confirm(
                    $this->config->item('failure_url') . '?tx=' . $response->getData()['VendorTxCode'] . '&type=CC',
                    'Transaction not accepted - Reason: ' . $response->getData()['Status']
                );
            }

        } else {
            //Send back an error as we've failed the form validation.
            log_message('info', 'Transaction form validation failed: ' . json_encode($_POST));

            $request = $this->gateway->completePurchase([]);
            $response = new \Omnipay\SagePay\Message\ServerCompleteAuthorizeResponse($request, []);
            $response->error($this->config->item('failure_url'), 'No pending transaction'); // exits here
        }
    }