PHP Composer Guide

Welcome! This article will assist you to understand better about getting to know Composer!

In this article, we’ll cover what Composer is, what Composer does, how to use a composer, when to use Composer and how Composer is used in Xendit PHP SDK, and how you can easily integrate Xendit with your PHP application via composer.

We will use Invoice / Checkout API as a tutorial example, but you can follow the same steps to set up for other Xendit APIs.

What is a composer? What does a composer do?

If you’ve been coding in PHP for some time, you'll be aware of how PHP libraries can help save work and make code reusable. In the past, it was harder to add libraries to PHP, which led to a lot of reinventing the wheel for common features. For example, when you want to integrate with a new library, you have to download the library and call include native PHP function and you’re able to execute your desired function in that library.

In a nutshell, we need a tool which can be used to install libraries and manage application dependencies. Composer does a great job of this. It is an application-level package manager for PHP that has gained immense popularity and become the standard for managing dependencies in PHP applications.

INFO

"Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you" — Composer Introduction

It’s important to note that Composer allows you to install the necessary libraries on a per-project basis. It allows you to use different versions of the same library across different PHP projects. Of course, there’s an option to install a library globally, but it’s not recommended. If you’ve heard of npm for Node.js, or Bundler for Ruby, that's what Composer is for PHP.

To install and use libraries that are managed by Composer, you just need to declare them in your project in a standard format, and Composer will manage the rest. For example, if you want to install the phpmailer library by using Composer, you just need to run the following command in the root of your project.

$composer require phpmailer/phpmailer 

When to use Composer

Now you should have understood the composer from the previous section. And you might be wondering when is the right time to use a composer. If your PHP project uses multiple external libraries and various packages as helpers within your project, composer is the right tool for you to consider and use.

Composer allows you to very easily install a multitude of libraries for your project without dealing with the details. It allows the authors to use another library themselves without you having to deal with all the details.

For example, if you would want to manually install a library that requires to install two additional libraries in the correct version, with these libraries requiring one additional library each, and additionally you'd have to initialize the autoloading of all five libraries, this might be some task to tackle.

With Composer, you only require one library, and after that everything is done for you.

Additionally, it makes updating way easier for you. If your library has a bug that got fixed in a newer version, you simply update and see if your application still runs. You would spend most of the time checking everything still runs - and barely any time updating.

How to use Composer

There are two ways when it comes to installing libraries with Composer. Let’s quickly go through it to understand how it works.

The install Command

To use install, you need to create a composer.json file in your project first. In the composer.json file, you just need to declare your project dependencies, as shown in the following snippet.

{
    "require": {
        "phpmailer/phpmailer": "~6.1"
    }
}

Next, when you run the composer install command from that folder,

$composer install 

Composer installs the phpmailer package and its dependencies in the vendor directory. More importantly, it also creates the composer.lock file, which maintains a list of all of the packages and the exact versions of them that are installed.

The require Command

We can say that the composer require command is a sort of shortcut for the previous process of creating a composer.json file. require will add a package to your composer.json file automatically. The following command shows how to install the phpmailer package with the help of require.

$composer require phpmailer/phpmailer 

After installing the phpmailer package and its dependencies, require also adds an entry of the package which is installed in the composer.json file. If the composer.json file doesn’t exist, it’ll be created on the fly. Of course, it also updates the composer.lock file to write package information, along with the exact versions.

So that’s how you can install the necessary dependencies in your PHP projects by using Composer. And with that, we’ve reached the end of this article as well.

Common Issue

  1. Permission Issue

Please make sure you have granted sufficient permission to the project folder so that the composer could do its magic to install all dependencies.

  • Packages will not be available. You should upgrade to Composer 2 error. In case you are using PHP 8.0, any old composer commands might not work. Instead, you need to use composer2. For example command for installing gumlet/php-image-resize should look like this:
composer2 require gumlet 

As an alternative, you can lower your PHP version.

  • Composer dependency versioning issue

If you have been using composer and trying to install a new package library and getting an error with dependency versioning, there are two options you can do:

  • Fix by upgrading / downgrading the dependencies
  • Install by ignoring the platform requirement composer update --ignore-platform-reqs or composer install --ignore-platform-reqs (Not Recommended)

How composer is used in Xendit PHP SDK

Installation

Install xendit-php-clients with composer by following command:

composer require xendit/xendit-php 

You will see the following outputs once you have run the command.

root@57ea4162a1e8:/var/www/html# composer require xendit/xendit-php
Info from https://repo.packagist.org: #StandWithUkraine
Using version ^2.17 for xendit/xendit-php
./composer.json has been updated
Running composer update xendit/xendit-php
Loading composer repositories with package information
Updating dependencies
Lock file operations: 9 installs, 0 updates, 0 removals
  - Locking guzzlehttp/guzzle (7.4.2)
  - Locking guzzlehttp/promises (1.5.1)
  - Locking guzzlehttp/psr7 (2.2.1)
  - Locking psr/http-client (1.0.1)
  - Locking psr/http-factory (1.0.1)
  - Locking psr/http-message (1.0.1)
  - Locking ralouphie/getallheaders (3.0.3)
  - Locking symfony/deprecation-contracts (v2.5.1)
  - Locking xendit/xendit-php (2.17.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 9 installs, 0 updates, 0 removals
  - Downloading guzzlehttp/promises (1.5.1)
  - Downloading ralouphie/getallheaders (3.0.3)
  - Downloading psr/http-message (1.0.1)
  - Downloading psr/http-factory (1.0.1)
  - Downloading guzzlehttp/psr7 (2.2.1)
  - Downloading psr/http-client (1.0.1)
  - Downloading symfony/deprecation-contracts (v2.5.1)
  - Downloading guzzlehttp/guzzle (7.4.2)
  - Downloading xendit/xendit-php (2.17.1)
  - Installing guzzlehttp/promises (1.5.1): Extracting archive
  - Installing ralouphie/getallheaders (3.0.3): Extracting archive
  - Installing psr/http-message (1.0.1): Extracting archive
  - Installing psr/http-factory (1.0.1): Extracting archive
  - Installing guzzlehttp/psr7 (2.2.1): Extracting archive
  - Installing psr/http-client (1.0.1): Extracting archive
  - Installing symfony/deprecation-contracts (v2.5.1): Extracting archive
  - Installing guzzlehttp/guzzle (7.4.2): Extracting archive
  - Installing xendit/xendit-php (2.17.1): Extracting archive
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
4 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

Your composer.json should have been appended in the require section with new package called xendit/xendit-php

{
    "name": "xendit/xendit-sdk-sample",
    "description": "XENDIT PHP SDK example ",
    "version": "1.0.0",
    "license": "MIT",
    "keywords": [
        "xendit"
    ],
    "require": {
        "xendit/xendit-php": "^2.17"
    }
}

Your project folder now should look like this:

Alternatively if you have already had xendit-php before, you may opt to update the SDK to the latest version with the following command:

composer update xendit/xendit-php 

SDK Usage on Invoice Creation

Now we are going to demonstrate by using xendit php sdk to create an invoice. For now, we will use index.php as a single file and invoking xendit php sdk by creating an invoice. Sample code below with name index.php:

<?php

/**
 * InvoiceExample.php
 * php version 7.2.0
 *
 * @category Example
 * @package  Xendit/Examples
 * @author   Hendry <hendry@xendit.co>
 * @license  https://opensource.org/licenses/MIT MIT License
 * @link     https://api.xendit.co
 */

use Xendit\Xendit;

require "vendor/autoload.php";

Xendit::setApiKey("INSERT YOUR API SECRET KEY HERE");

$params = [
    "external_id" => "demo_147580196270",
    "payer_email" => "sample_email@xendit.co",
    "description" => "Trip to Bali",
    "amount" => 32000,
];

$createInvoice = \Xendit\Invoice::create($params);
print_r($createInvoice);

$id = $createInvoice["id"];

$getInvoice = \Xendit\Invoice::retrieve($id);
print_r($getInvoice);

And now we will run above snippet code by calling index.php directly.

root@57ea4162a1e8:/var/www/html# php index.php
Array
(
    [id] => 6274cc43d4846e37284e57a2
    [external_id] => demo_147580196270
    [user_id] => 599bd7f1ccab55b020bb1147
    [status] => PENDING
    [merchant_name] => Xendit
    [merchant_profile_picture_url] => https://xnd-companies.s3.amazonaws.com/prod/1538466380522_868.png
    [amount] => 32000
    [payer_email] => sample_email@xendit.co
    [description] => Trip to Bali
    [expiry_date] => 2022-05-07T07:20:35.103Z
    [invoice_url] => https://checkout-staging.xendit.co/web/6274cc43d4846e37284e57a2
    [available_banks] => Array
        (
            [0] => Array
                (
                    [bank_code] => MANDIRI
                    [collection_type] => POOL
                    [transfer_amount] => 32000
                    [bank_branch] => Virtual Account
                    [account_holder_name] => XENDIT
                    [identity_amount] => 0
                )

            [1] => Array
                (
                    [bank_code] => BRI
                    [collection_type] => POOL
                    [transfer_amount] => 32000
                    [bank_branch] => Virtual Account
                    [account_holder_name] => XENDIT
                    [identity_amount] => 0
                )

            [2] => Array
                (
                    [bank_code] => BNI
                    [collection_type] => POOL
                    [transfer_amount] => 32000
                    [bank_branch] => Virtual Account
                    [account_holder_name] => XENDIT
                    [identity_amount] => 0
                )

            [3] => Array
                (
                    [bank_code] => PERMATA
                    [collection_type] => POOL
                    [transfer_amount] => 32000
                    [bank_branch] => Virtual Account
                    [account_holder_name] => XENDIT
                    [identity_amount] => 0
                )

        )

    [available_ewallets] => Array
        (
        )

    [available_direct_debits] => Array
        (
        )

    [available_paylaters] => Array
        (
        )

    [should_exclude_credit_card] => 
    [should_send_email] => 
    [created] => 2022-05-06T07:20:36.033Z
    [updated] => 2022-05-06T07:20:36.033Z
    [currency] => IDR
)
Array
(
    [id] => 6274cc43d4846e37284e57a2
    [external_id] => demo_147580196270
    [user_id] => 599bd7f1ccab55b020bb1147
    [status] => PENDING
    [merchant_name] => Xendit
    [merchant_profile_picture_url] => https://xnd-companies.s3.amazonaws.com/prod/1538466380522_868.png
    [amount] => 32000
    [payer_email] => sample_email@xendit.co
    [description] => Trip to Bali
    [expiry_date] => 2022-05-07T07:20:35.103Z
    [invoice_url] => https://checkout-staging.xendit.co/web/6274cc43d4846e37284e57a2
    [available_banks] => Array
        (
            [0] => Array
                (
                    [bank_code] => MANDIRI
                    [collection_type] => POOL
                    [transfer_amount] => 32000
                    [bank_branch] => Virtual Account
                    [account_holder_name] => XENDIT
                    [identity_amount] => 0
                )

            [1] => Array
                (
                    [bank_code] => BRI
                    [collection_type] => POOL
                    [transfer_amount] => 32000
                    [bank_branch] => Virtual Account
                    [account_holder_name] => XENDIT
                    [identity_amount] => 0
                )

            [2] => Array
                (
                    [bank_code] => BNI
                    [collection_type] => POOL
                    [transfer_amount] => 32000
                    [bank_branch] => Virtual Account
                    [account_holder_name] => XENDIT
                    [identity_amount] => 0
                )

            [3] => Array
                (
                    [bank_code] => PERMATA
                    [collection_type] => POOL
                    [transfer_amount] => 32000
                    [bank_branch] => Virtual Account
                    [account_holder_name] => XENDIT
                    [identity_amount] => 0
                )

        )

    [available_ewallets] => Array
        (
        )

    [available_direct_debits] => Array
        (
        )

    [available_paylaters] => Array
        (
        )

    [should_exclude_credit_card] => 
    [should_send_email] => 
    [created] => 2022-05-06T07:20:36.033Z
    [updated] => 2022-05-06T07:20:36.033Z
    [currency] => IDR
)

Voila!! You are now making your first API Call with Xendit PHP SDK!

Last Updated on 2023-06-02