• Version: 1.2.0

Quickstart

Using an SDK

We provide many free SDK’s for your usage. Check out our SDK’s on our github page https://github.com/moltin

Here are some examples of using some SDK’s to get up and running quickly.

Installation

The recommended way to install the SDK is to use Composer to manage installation and creation of the autoload script.

https://getcomposer.org/download/

Add to, or create, composer.json in your project folder.

{
	"require" : {
		"moltin/php-sdk" : "dev-version1"
	}
}

Execute composer with the install parameter (i.e. composer install). This will download the Moltin PHP SDK and create autoload.php in the vendor directory.

Authenticate

Require the autoload.php file if needed, and initialize Moltin:

require 'vendor/autoload.php';

use Moltin\SDK\Facade\Moltin as Moltin;

Moltin::Authenticate('ClientCredentials', [
    'client_id'     => '<CLIENT_ID>',
    'client_secret' => '<CLIENT_SECRET>'
]);

Get a product

Now that we’ve authenticated with the Moltin API lets get one of our sample products.

$product = \Product::Find(['slug' => 'decorative-hedgehogs']);

“product” will contain an array of the products data. A good place to display this data is on a product or listing page.

Add product to cart

Using a product ID we can insert a quantity of this product into the cart with one simple request. The first parameter passed is the product ID, the second the quantity to be added to the cart.

$item = \Cart::Insert($product['id'], 1);

“item” will contain an array of the individual cart item data e.g. title, quantity, price. A good example to integrate this would be to add a simple button or form to the product page that posts the product ID and quantity to this endpoint.

Get cart contents

Now that there is at least one product in the cart lets get the full cart contents.

$cart = \Cart::Contents();

“cart” will contain an array of all cart items and cart totals. A good place to show this data would be on the cart or orders page, or even a widget in the header of your website.

Convert cart to order

When the customer is ready to checkout we need to convert the cart to an order. This call lets you define the payment gateway and conditional order parameters such as customer, billing and shipping addresses.

$order = \Cart::Order(['gateway' => 'dummy']);

This call will return an object containing data for the new order created.

Process payment

Once we’ve converted a cart into an order we’re now ready to process a payment. In this example we’ve used the dummy gateway so we just need to provide some card details. The data you need to provide in this step depends on your chosen gateway.

$checkout = \Checkout::Payment('purchase', $order['id'], [
	'number'       => '4242424242424242',
	'expiry_month' => '02',
	'expiry_year'  => '2017',
	'cvv'          => '123'
]);

Congratulations, if you made it this far you’ve implemented a simple step by step PHP checkout!

Jump start your own project

Grab our example project and see all of this working together in a simple application! Customise or use as inpiration for your own projects.

PHP example framework

View Project on Github