Omise PHP Client
omise-php
is a library designed specifically to connect with Omise API written in PHP.
Please pop onto our community forum or contact support@omise.co if you have any question regarding this library and the functionality it provides.
Requirements
- PHP 5.3 and above.
- Built-in libcurl support.
Installation
Using Composer
You can install the library via Composer. If you don't already have Composer installed, first install it by following one of these instructions depends on your OS of choice:
- Composer installation instruction for Windows
- Composer installation instruction for Mac OS X and Linux
-
After composer is installed, you can declare Omise-PHP as a project dependency by creating a
composer.json
at the root of your project directory with the following content:{ "require": { "omise/omise-php": "dev-master" } }
-
Then run the following command to install the Omise-PHP library:
php composer.phar install
-
Once you run the
composer install
command, the Composer will generate avendor
folder at the same directory ascomposer.json
. Now you can then add the following line to PHP script to load the library:require_once dirname(__FILE__).'/vendor/autoload.php';
-
Now you are ready to start using the library, please see the configuration and quick start sections below for usage examples.
Manually
If you're not using Composer, you can also download the latest version of Omise-PHP. Then, follows the instruction below to install Omise-PHP to the project.
-
Extract the library to your project.
-
Then, include the following line into your PHP file,
require_once 'path-to-library/omise-php/lib/Omise.php';
-
Now you are ready to start using the library, please see the configuration and quick start sections below for usage examples.
However, using Composer is recommended as you can easily keep the library up-to-date.
Configuration
Config your public and secret keys
In order to make any request to Omise API through Omise-PHP library, you will need to config your public and secret key to the project. Type the following code in any place before executing the library.
define('OMISE_PUBLIC_KEY', 'pkey_can_find_at_omise_dashboard');
define('OMISE_SECRET_KEY', 'skey_can_find_at_omise_dashboard');
Note, you can get your public and secret keys at Omise Dashboard.
As for reference, you can check our document at https://www.omise.co/api-authentication.
API version
This parameter most uses specially for backward-compatible reason, it's not necessary to config when using the library.
It is highly recommended to set this version to the current version you're using.
(or you can just skip this section, Omise will then use your current API version as an default in any requests).
You can choose which API version to use with Omise. Each API version has new features and might not be compatible with previous versions. You can change the default version by visiting your Omise Dashboard.
To overwrite the API version to use, you can specify it by defining OMISE_API_VERSION
.
The version configured here will have higher priority than the version set in your Omise account.
This is useful if you have multiple environments with different API versions for testing.
(e.g. Development on the latest version but production is on an older version).
define('OMISE_API_VERSION', '2014-07-27');
You can check your current API version at Omise Dashboard.
Quick Start
The following code demonstrates how to make a charge with Omise-PHP library.
Now from the above sections, your code will looks similar like the below.
require_once dirname(__FILE__).'/vendor/autoload.php';
define('OMISE_PUBLIC_KEY', 'pkey_test_54ot96fkr3i2op60cng');
define('OMISE_SECRET_KEY', 'skey_test_54ot96fkr3i2op60cng');
Now, let's add the below code to create a charge through the library.
$charge = OmiseCharge::create(array(
'amount' => 100000,
'currency' => 'THB',
'card' => 'tokn_test_4xs9408a642a1htto8z'
));
To see a real implementation code, you can check from the link below. https://github.com/omise/examples/tree/master/php
After this, you can check the complete documentation at https://www.omise.co/docs.
Development and Testing
To run an automated test suite, make sure you already have a PHPUnit in your local machine. Then run the PHPUnit:
phpunit omise-php/tests
If you want to run with a specific test, let's try
phpunit omise-php/tests/omise/AccountTest
License
Omise-PHP is open-sourced software released under the MIT License.