INITIAL GUIDE
Libraries
COLLECTING PAYMENTS
CHARGES

PHP

Important to consider

This library is deprecated. We recomend to use the Kushki API for your backend integration.

Once you have tokenized your client’s credit card using one of our collecting payments you can begin to charge the card.

Kushki has created PHP library for your backend development, allowing you to easily submit payments.

Install and configure

  • Option 1 - SOURCE CODE

Download our kushki.zip, unzip the file and place it in the project folder

Reference the library

<?php
require_once "ruta_a_kushki/autoload.php";
  • Option 2 - Composer

Add the dependency as shown "kushki/kushki-php": "^1.2.3" or correcting the following command composer require kushki/kushki-php:^1.2.3

Reference the library

<?php
require_once "vendor/autoload.php";

Import

Import Kushki to your project

<?php
namespace Kushki;
use My\Full\Path\Amount;
use My\Full\Path\Kushki;
use My\Full\Path\KushkiEnvironment;
use My\Full\Path\Transaction;
use My\Full\Path\ExtraTaxes;

Usage

Begin creating your Kushki object, it will allow you to perform all the methods available in our PHP Library.

<?php
$merchantId = "<private-merchant-id>";
$language = KushkiLanguage::ES;
$currency = KushkiCurrency::USD; // KushkiCurrency::COP; for Colombia
$environment = KushkiEnvironment::PRODUCTION;

$kushki = new Kushki($merchantId, $language, $currency, $environment);

?>

Required

Property Type Description
$merchantId String Kushki private ID created for your merchant

Optional

Property Type Description Default Possible Values
$language String Defined language ES ES,EN
$currency String Code of currency used USD USD, COP
$environment boolean Value to define if you are in production or test environment KushkiEnvironment::PRODUCTION KushkiEnvironment::PRODUCTION , KushkiEnvironment::TESTING

Reference

Find methods available in the chart. They allow you to make this operations:

  • Create a payment transaction
  • Create a deffered payment transaction
  • Cancel a payment transaction
  • Create a subscription
  • Create a subscription payment transaction
  • Update a subscription
PHP Methods
Name Parameters Returns Description
charge() token, amount, metadata months Object Charge the card
deferredCharge() token, amount, metadata months Object Deferred Charge to the card
voidCharge() ticket , amount Object Cancel a charge transaction
createSubscription() planName, periodicity, contactDetails, amount, startDate, metadata Object Create a subscription
updateSubscription() subscriptionId, body Object Update a created subscription
chargeSubscription() subscriptionId, metadata Object Charge a subscription

charge()

Charge the card using the following code:

Request

<?php
  
$token = "<obtained-kushkiToken>"; 
$subtotalIva = 10.0;
$iva = 1.20;
$subtotalIva0 = 5.0;
$ice = 1.50;
$amount = new Amount($subtotalIva, $iva, $subtotalIva0, $ice);
$metadata = array("Key1"=>"value1", "Key2"=>"value2");

$transaccion = $kushki->charge($token, $amount, $metadata);

?>

Required

Property Type Description
$token String Value obtained in the front-end
$amount Object Value that is going to be charged

Check Amount in PHP

Optional

Property Type Description
$metadata Array Unlimited values of unlimited levels

deferredCharge()

Apply a deferred charge to a card using the following code

Request

<?php
  
$token = "< token enviado desde el navegador en el parametro kushkiToken >"; 
$months = 3; // Number of months sent from the browser in the kushkiDeferred parameter, converted to Integer
$subtotalIva = 10.0;
$iva = 1.20;
$subtotalIva0 = 5.0;
$ice = 1.50;
$amount = new Amount($subtotalIva, $iva, $subtotalIva0, $ice);
$metadata = array("Key1"=>"value1", "Key2"=>"value2");

$transaccion = $kushki->deferredCharge($token, $amount, $months, $metadata);

?>

Required

Property Type Description
$token String Value obtained in the front-end
$amount Object Values that are going to be charged.

Check Amount in PHP

Optional

Property Description
$months Number of months if selected deffered and it defines the use of charge
$metadata Unlimited values of unlimited levels

You can apply both methods by using the number of months, it will determine the use of charge() in case of being 1, or deferredCharge() in case it is 2 or higher.

<?php
  
$token = "< token enviado desde el navegador en el parametro kushkiToken >"; 
$months = 3; // Number of months sent from the browser in the kushkiDeferred parameter, converted to Integer
$subtotalIva = 10.0;
$iva = 1.20;
$subtotalIva0 = 5.0;
$ice = 1.50;
$amount = new Amount($subtotalIva, $iva, $subtotalIva0, $ice);
$metadata = array("Key1"=>"value1", "Key2"=>"value2");
if ($months > 0) { // Charge or Deffered
    $transaccion = $kushki->deferredCharge($token, $amount, $months, $metadata);
} else {
    $transaccion = $kushki->charge($token, $amount, $metadata);
}
?>

Response

Check if the transaction was successful or not, and retreive relevant data.

<?php
if ($transaccion->isSuccessful()) {
    echo "Número de ticket: " . $transaccion->getTicketNumber();
} else {
    echo "Error " . $transaccion->getResponseCode() . ": " . $transaccion->getResponseText();
}
?>

Check Transaction in PHP

voidCharge()

It allows you to cancel a previous payment

Request

$ticket = $chargeTransaction->getTicketNumber();
$subtotalIva = 10.0;
$iva = 0.12;
$subtotalIva0 = 5.0;
$ice = 3.0;
$monto = new Amount($subtotalIva, $iva, $sutotalIva0, $ice);

$voidChargeTransaction = $kushki->voidCharge($ticket, $monto);
if ($voidChargeTransaction->isSuccessful()) {
    $ticket = $voidChargeTransaction->getTicketNumber();
}

Required

Property Type Description
$ticket String Ticket number obtained in the charge method

Optional

Property Type Description
$amount Object Value that is going to be charged

Response

Check if subscription was successful or not, retrieve relevant data

<?php
if ($voidChargeTransaction->isSuccessful()) {
    echo "Id de subscripción: " . $$voidChargeTransaction->getTicketNumber();
} else {
    echo "Error " . $voidChargeTransaction->getResponseCode() . ": " . $subscription->getResponseText();
}
?>

createSubscription()

Create a subscription with an amount to charge

Request

<?php

$token = "<obtained-kushkiToken >";
$planName = "Premium";
$periodicity = "monthly";
$contactDetails = array(
            "firstName" => "Lisbeth",
            "lastName" => "Salander",
            "email" => "lisbeth@salander.com");
$startDate = "2017-01-18";
$subtotalIva = 10.0;
$iva = 1.20;
$subtotalIva0 = 5.0;
$ice = 1.50;
$amount = new Amount($subtotalIva, $iva, $subtotalIva0, $ice);
$metadata = array("Key1"=>"value1", "Key2"=>"value2");

$subscription = $kushki->createSubscription($token, $planName, $periodicity, $contactDetails, $amount, $startDate, $metadata);

?>

Required

Property Description
$token Token generated in front-end
$planName Name of the plan assigned to the subscription
$periodicity Intervals of payment
$contactDetails Contains subscription details
$amount Contain all values to charge
$startDate Subscription start date

Check Amount in PHP

Optional

Property Description
$metadata Unlimited values of unlimited levels

Response

Check if subscription was successful or not, retrieve relevant data

<?php
if ($subscription->isSuccessful()) {
    echo "Id de subscripción: " . $subscription->getSubscriptionId();
} else {
    echo "Error " . $subscription->getResponseCode() . ": " . $subscription->getResponseText();
}
?>

Check Transaction in PHP

chargeSubscription()

Use this kind of charge over created subscriptions

Request

<?php

$subscriptionId = "< id de subscripción que se obtiene de la creación de la subscripción con el método getSubscriptionId() >"
$metadata = array("Key1"=>"value1", "Key2"=>"value2");

$chargeSubscription = $kushki->chargeSubscription($subscriptionId, $metadata);

?>

Response

Check if subscription charge was successful or not, retrieve relevant data

<?php
if ($chargeSubscription->isSuccessful()) {
    echo "Número de ticket: ". $chargeSubscription->getTicketNumber();
} else {
    echo "Error " . $chargeSubscription->getResponseCode() . ": " . $chargeSubscription->getResponseText();
}

Check Transaction in PHP

updateSubscription()

Update any subscription parameter

Request

<?php
$subscriptionId = "<subscriptionId-created-use-getSubscriptionId()>"
$planName = "Premium";
$periodicity = "monthly";
$contactDetails = array(
            "firstName" => "Lisbeth",
            "lastName" => "Salander",
            "email" => "lisbeth@salander.com");
$startDate = "2017-01-18";
$subtotalIva = 10.0;
$iva = 1.20;
$subtotalIva0 = 5.0;
$ice = 1.50;
$monto = new Amount($subtotalIva, $iva, $subtotalIva0, $ice);
$currency = "USD";
$metadata = array("Key1"=>"value1", "Key2"=>"value2");
$body = array(
        "planName" => $planName,
        "periodicity" => $periodicity,
        "contactDetails" => $contactDetails,
        "startDate" => $startDate,
        "amount" => $monto,
        "currency" => $currency,
        "metadata" => $metadata);

$updateSubscription = $kushki->updateSubscription($subscriptionId, $body);

?>

Response

Check if subscription update was successful or not, retrieve relevant data

<?php
if ($updateSubscription->isSuccessful()) {
    echo "Se realizo la actualización con éxito ";
} else {
    echo "Error " . $updateSubscription->getResponseCode() . ": " . $updateSubscription->getResponseText();
}

Check Transaction in PHP

Full Examples

<?php

public function charge($token, $amount)
$token = $_POST["kushkiToken"]
$subtotalIva = 10.0;
$iva = 0.12;
$subtotalIva0 = 5.0;
$ice = 3.0;
$monto = new Amount($subtotalIva, $iva, $sutotalIva0, $ice);
$metadata = array("Key1"=>"value1", "Key2"=>"value2");

$chargeTransaction = $kushki->charge($token, $monto, $metadata);
if ($chargeTransaction->isSuccessful()) {
    $ticket = $chargeTransaction->getTicketNumber();
}
?>

Classes available

Transaction as a response code. In case of successful transactions, 000. In case of transactions with error, a numeric code.

Methods

Name Description
isSuccessful() Returns true when the transaction was successful, false otherwise
getTicketNumber() Returns the ticket number of a charge or deferred charge transaction in order to cancel or reimburse it
getResponseText() Returns the response text. In case of successful transactions, successful transaction. In case of transactions with error, this text describes the problem
getApprovedAmount() Returns the approved amount of the transaction for Ecuador
getApprovedAmountColombia() Returns the approved amount of the transaction for Colombia
getResponseCode() Returns the response code. In case of successful transactions, 000. In case of transactions with error, a numeric code