PHP code example of culqi / culqi-php

1. Go to this page and download the library: Download culqi/culqi-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

culqi / culqi-php example snippets





// Cargamos Requests y Culqi PHP
de_once dirname(__FILE__).'/libraries/culqi-php/lib/culqi.php';

// Configurar tu API Key y autenticación
$PUBLIC_KEY = "{PUBLIC KEY}";
$SECRET_KEY = "{SECRET KEY}";
$culqi = new Culqi\Culqi(array('api_key' => $SECRET_KEY));

$encryption_params = array(
  "rsa_public_key" => "la llave pública RSA",
  "rsa_id" => "el id de tu llave"
);
 $req_body = array(
    "card_number" => "4111111111111111",
    "cvv" => "123",
    "email" => "culqi".uniqid()."@culqi.com", //email must not repeated
    "expiration_month" => "7",
    "expiration_year" => $futureDate,
    "fingerprint" => uniqid(),
    "metadata" => array("dni" => "71702935")
);
$token = $culqi->Tokens->create(
    $req_body,
    $encryption_params
  );

$req_body = array(
  "card_number" => "4111111111111111",
  "cvv" => "123",
  "email" => "culqi".uniqid()."@culqi.com", //email must not repeated
  "expiration_month" => "7",
  "expiration_year" => $futureDate,
  "fingerprint" => uniqid(),
  "metadata" => array("dni" => "71702935")
);
  
// Creando token a una tarjeta sin encriptar
$token = $culqi->Tokens->create(
  $req_body
);

//Respuesta
print_r($charge);

$charge = $culqi->Charges->create(
    array(
      "amount" => 1000,
      "capture" => true,
      "currency_code" => "PEN",
      "description" => "Venta de prueba",
      "email" => "[email protected]",
      "installments" => 0,
      "antifraud_details" => array(
          "address" => "Av. Lima 123",
          "address_city" => "LIMA",
          "country_code" => "PE",
          "first_name" => "Test_Nombre",
          "last_name" => "Test_apellido",
          "phone_number" => "9889678986",
      ),
      "source_id" => "{token_id o card_id}"
    )
);

//Respuesta
print_r($charge);

$req_body =  array(
  "amount" => 1000,
  "capture" => true,
  "currency_code" => "PEN",
  "description" => "Venta de prueba",
  "email" => "[email protected]",
  "installments" => 0,
  "antifraud_details" => array(
      "address" => "Av. Lima 123",
      "address_city" => "LIMA",
      "country_code" => "PE",
      "first_name" => "Test_Nombre",
      "last_name" => "Test_apellido",
      "phone_number" => "9889678986",
  ),
  "source_id" => "{token_id o card_id}"
);

$custom_headers = array(
  "X-Charge-Channel" => 'recurrent'
);

$charge = $culqi->Charges->create($req_body, [], $custom_headers);
//Respuesta
print_r($charge);

$req_body = array(
  "amount" => 500,
  "charge_id" => "{charge_id}",
  "reason" => "bought an incorrect product"
);

// Creando una devolución sin encriptar
$refund = $culqi->Refunds->create(
  $req_body
);

//Respuesta
print_r($refund);

$customer = $culqi->Customers->create(
  array(
    "address" => "av lima 123",
    "address_city" => "lima",
    "country_code" => "PE",
    "email" => "www@".uniqid()."me.com",
    "first_name" => "Will",
    "last_name" => "Muro",
    "metadata" => array("test"=>"test"),
    "phone_number" => 899898999
  )
);
print_r($customer);

$card = $culqi->Cards->create(
  array(
    "customer_id" => "{customer_id}",
    "token_id" => "{token_id}"
  )
);
print_r($card);

$plan = $culqi->Plans->create(
  array(
    "interval_unit_time" => 1,
    "interval_count" => 1,
    "amount" => 300,
    "name" => "Plan mensual" . uniqid(),
    "description" => "Plan-mock" . uniqid(),
    "short_name" => "pln-" . uniqid(),
    "currency" => "PEN",
    "metadata" => json_decode('{}'),
    "initial_cycles" => array(
      "count" => 0,
      "amount" => 0,
      "has_initial_charge" => false,
      "interval_unit_time" => 1
    ),
  )
);

//Respuesta
print_r($plan);

// Creando Suscriptor a un plan
$subscription = $culqi->Subscriptions->create(
  array(
    "card_id" => "crd_live_tjHaW6x5Dj2oKhrS",
    "plan_id" => "pln_live_0HzG8Edqy0aUIusL",
    "tyc" => true,
    "metadata" => array("envtest" => "SDK-JAVA"),
  )
);

//Respuesta
print_r($subscription);

// Creando orden (con 1 dia de duracion)
$order = $culqi->Orders->create(
      array(
        "amount" => 1000,
        "currency_code" => "PEN",
        "description" => 'Venta de prueba',        
        "order_number" => 'pedido-9999',  
        "client_details" => array( 
            "first_name"=> "Brayan", 
            "last_name" => "Cruces",
            "email" => "[email protected]", 
            "phone_number" => "51945145222"
         ),
        "expiration_date" => time() + 24*60*60   // Orden con un dia de validez
      )
);
print_r($order);

$PUBLIC_KEY = "{PUBLIC KEY}";
$culqi = new Culqi\Culqi(array('api_key' => $PUBLIC_KEY));
$futureDate = date('Y', strtotime('+1 year'));
$encryption_params = array(
  "rsa_public_key" => "",
  "rsa_id" => ""
);

$req_body = array(
  "card_number" => "4111111111111111",
  "cvv" => "123",
  "email" => "culqi".uniqid()."@culqi.com", //email must not repeated
  "expiration_month" => "7",
  "expiration_year" => $futureDate,
  "fingerprint" => uniqid(),
  "metadata" => array("dni" => "71702935")
);
  
// Creando token a una tarjeta sin encriptar
$token = $culqi->Tokens->create(
  $req_body
);


$SECRET_KEY = "{SECRET KEY}";
$culqi = new Culqi\Culqi(array('api_key' => $SECRET_KEY));

//Datos para encriptar
$encryption_params = array(
  "rsa_public_key" => "",
  "rsa_id" => ""
);

//3ds object, la primera vez que se consume el servicio no se debe enviar los parámetros 3ds
$tds_xid = $_POST["xid"];
$tds = array("authentication_3DS" => array(
  "eci" => $_POST["eci"],
  "xid" => $tds_xid,
  "cavv" => $_POST["cavv"],
  "protocolVersion" => $_POST["protocolVersion"],
  "directoryServerTransactionId" => $_POST["directoryServerTransactionId"]
));

$req_body = array(
  "amount" => 10000,
  "capture" => true,
  "currency_code" => "PEN",
  "description" => "Venta de prueba",
  "installments" => 0,
  "email" => "[email protected]",
  "metadata" => array("test"=>"test"),
  "source_id" => "" // previamente generado usando create token
);


$with_tds = ($req_body) + (isset($tds_xid) ? $tds : array());

// Creando Cargo sin encriptar a una tarjeta
$charge = $culqi->Charges->create($with_tds);
// Respuesta
echo "<b>Cargo sin encriptar payload:</b> "."<br>".json_encode($charge)."<br>";

// Creando Cargo con encriptación a una tarjeta
$charge = $culqi->Charges->create($with_tds, $encryption_params);
bash
git clone [email protected]:culqi/culqi-php.git
bash
git clone https://github.com/culqi/culqi-php.git
composer install
cd culqi-php/examples
php -S 0.0.0.0:8000
bash
   # Ejecutar el ejemplo de creación de planes
   php examples/plan/02-create-plan.php

   # Ejecutar el ejemplo de creación de suscripciones
   php examples/subscription/01-create-subscription.php