Skip to content

10quality/bac-php-cardinal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BAC (PHP via Cardinal)

This PHP library will allow make payments using "BAC Credomatic"'s credit card service via Cardinal.

Requirements

  • PHP >= 5.6
  • BAC access

Usage

Init API

Before implementing cardinal, you will need the public and private keys that are provided by Bac (read their documentation).

Init your API access using the Api class like this:

use Bac\Cardinal\Api;

/**
 * @param mixed  $keyId    Public key.
 * @param string $key      Private key.
 * @param string $redirect Redirect servlet URL, who will process reponses from the server.
 */
$api = new Api($keyId, $key, $redirect);

Init Service

Instantiate the payment service like this:

use Bac\Cardinal\BacPayment;

/**
 * @param Bac\Cardinal\Api $api Api instance.
 */
$service = new BacPayment($api);

Payment Object

You will need to generate and fill a payment object if you want to make any request to the service.

Create payment object like this:

use Bac\Cardinal\Data\Payment;

$payment = new Payment;

// --- Fill minimum data
$payment->orderId = 304;
$payment->amount = 19.00;

// --- Fill credit card data
$payment->ccNumber = 410000000000000;
$payment->ccExpiration = '0120';
$payment->cvv = 123;
$payment->address = 'Billing address in one line';

// --- Fill transaction (for certain requests)
$payment->transactionId = 457464;

// --- Fill processor_id (optional)
$payment->processor_id = 1;

Request

This package supports authorize, capture, sale and cancel. These methods may throw exceptions before they attempt a request if some validations are not met, use try/catch to handle them.

use Bac\Cardinal\Exceptions\ApiException;
use Bac\Cardinal\Exceptions\PaymentException;

try {

    $service->authorize($payment);
    $service->capture($payment);
    $service->sale($payment);
    $service->cancel($payment);

} catch (ApiException $e) {
    // API exceptions
} catch (PaymentException $e) {
    // Payment exceptions
} catch (Exception $e) {
    // Unknown exceptions
}

Servlet (Response handling)

This package provides all the functionality needed to read the upcoming response and prepare your servlet. Use getResponse() method in service to obtain the response, then do your code based on the results. Same as the example above, use try/catch to handle exceptions

This is a servlet example:

use Bac\Cardinal\Api;
use Bac\Cardinal\BacPayment;
use Bac\Cardinal\Exceptions\ApiException;
use Bac\Cardinal\Exceptions\PaymentException;
use Bac\Cardinal\Exceptions\ResponseException;

try {

    // Prepare service
    $service = new BacPayment(new Api('key', 'key'));

    // Get response
    $response = $service->getResponse();

    // ------------- your code -----------
    // GET PAYMENT info
    // -----------------------------------
    $payment = custom_get_payment_function($response->orderId);

    // Update the `time` property if you want to compare and validate hashes
    $response->time = $payment->time;

    // Validate and check if approved
    if ($service->isValid($response) && $service->isApproved($response)) {

        // TODO
        // Your code if valid and approved

        // ------------- your code -----------
        // EXAMPLE IF WE WANT TO CAPTURE AN AUTHORIZED REQUEST
        // -----------------------------------
        $payment->transactionId = $response->transactionId;
        $service->capture($payment);

    } else {

        // TODO
        // Your code if denied or with data error

        // NOTE: Unmatching hashes will throw ResponseException
    }

} catch (ApiException $e) {
    // API exceptions
} catch (ResponseException $e) {
    // Response exceptions
} catch (PaymentException $e) {
    // Payment exceptions
} catch (Exception $e) {
    // Unknown exceptions
}

Response Object

Response object may containt:

// --- Response data (base response from service)
$response->response; // 1,2,3
$response->responseText;
$response->responseCode;
$response->avsResponse;
$response->cvvResponse;
$response->rawHash;

// --- Auth data
$response->authCode;

// --- Transaction data
$response->transactionId;
$response->orderId;
$response->amount;

// --- Payment data
$response->time;

// --- Generated data
$response->hash;
$response->isValidHash;

// --- Other BAC data
$response->points;

Copyright

(c) 2018 - 10 Quality.

About

BAC Credomatic PHP Cardinal Payment API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages