Question

omnipay doesn't have a complete documentation! I am trying to do a capture after an authorize but i cant seem to get it right.

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

use Omnipay\Common\GatewayFactory;

class Welcome extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }    
    public function authorize() {

        $gateway = GatewayFactory::create('PayPal_Express');
        $gateway->setUsername('***');
        $gateway->setPassword('***');
        $gateway->setSignature('***');
        $gateway->setTestMode(true);

        $response = $gateway->authorize(
                        array(
                            'cancelUrl' => base_url('welcome/authorize_return'),
                            'returnUrl' => base_url('welcome/authorize_return'),
                            'amount' => '1.99',
                            'currency' => 'USD'
                        )
                )->send();

        $response->redirect();
    }

    public function authorize_return() {
        $gateway = GatewayFactory::create('PayPal_Express');
        $gateway->setUsername('***');
        $gateway->setPassword('***');
        $gateway->setSignature('***');
        $gateway->setTestMode(true);

        $response = $gateway->completeAuthorize(
                        array(
                            'cancelUrl' => base_url('welcome/authorize_return'),
                            'returnUrl' => base_url('welcome/authorize_return'),
                            'amount' => '1.99',
                            'currency' => 'USD'
                        )
                )->send();

        echo $responsemsg = $response->getMessage();

        $data = $response->getData();
        $ref = $response->getTransactionReference();
        $response2 = $gateway->capture($data)->send();
        print_r($response2);
    }    
}

I need to change the status from "Pending" to "Completed" (ex: after I ship the product.)

enter image description here

also how can I can do a refund and when? Can I do a refund if the transaction is status is completed? or only on specific statuses, What are they?

I am getting "You can not refund this type of transaction" when the "Payment status" is "Pending" and when it is "Completed":

    function __construct() {
        parent::__construct();
        $this->load->helper('url');

        $this->gateway = GatewayFactory::create('PayPal_Express');
        $this->gateway->setUsername('***');
        $this->gateway->setPassword('***');
        $this->gateway->setSignature('***');
        $this->gateway->setTestMode(true);
    }
public function refund($transactionReference, $amount) {

            $ref = $transactionReference;

            $data = array(
                'transactionReference' => $ref,
                'amount' => $amount,
            );
            $response = $this->gateway->refund($data)->send();
            if ($response->isSuccessful()) {
                // success
                return 'done';
            } else {
                return $response->getMessage();
            }
        }
Was it helpful?

Solution

If you want to immediately capture the payment, just call purchase() and completePurchase() instead of authorize() and completeAuthorize() in your initial request (purchase does a combined authorize and capture).

If you want to capture the payment later (say, when the items are shipped), then you need to do the following.

// after initial completeAuthorize()
// store $ref in your database with the payment
$ref = $response->getTransactionReference();

// then later, when you want to capture it
$data = array(
    'transactionReference' => $ref,
    'amount' => '10.00', // pass original amount, or can be less
);
$response = $gateway->capture($data)->send();
if ($response->isSuccessful()) {
    // success
} else {
    // error, maybe you took too long to capture the transaction
    echo $response->getMessage();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top