xsolla/xsolla-sdk-php

Xsolla SDK for PHP. Xsolla is the authorized reseller and merchant providing e-commerce services for online games.

Installs: 3 445

Dependents: 0

Stars: 9

Watches: 7

Forks: 5

Open Issues: 1

Language: PHP

v1.1.1 2014-08-05 09:58 UTC

README

Latest Stable Version Build Status Code Coverage Scrutinizer Quality Score SensioLabsInsight

An official PHP SDK for interacting with Xsolla API

Requirements

  • PHP 5.3.3+
  • Your php.ini needs to have the date.timezone setting

Installation

The recommended way to install Xsolla SDK for PHP is through Composer.

$ cd /path/to/your/project
$ composer require xsolla/xsolla-sdk-php:~1.0

Usage

Generate URL to Payment Page

<?php

require_once 'vendor/autoload.php';

use Xsolla\SDK\Project;
use Xsolla\SDK\User;
use Xsolla\SDK\Invoice;
use Xsolla\SDK\PaymentPage\UrlBuilderFactory;

$project = new Project(
    '4783',//demo project id
    'key'//demo project secret key
 );
$urlBuilderFactory = new UrlBuilderFactory($project);

$user = new User('username');
$user->setEmail('example@example.com')
    ->setPhone('79090000000');

$invoice = new Invoice;
$invoice->setVirtualCurrencyAmount(5);

$url = $urlBuilderFactory->getPayStation()
    ->setInvoice($invoice)
    ->setUser($user)
    ->unlockParameterForUser('email')
    ->setCountry('US')
    ->setLocale('en')
    ->setParameter('theme', 115)
    ->setParameter('description', 'Purchase description')
    ->getUrl();

echo $url.PHP_EOL;

?>

Receive Instant Payment Notification

For receiving IPN requests you should implement \Xsolla\SDK\Protocol\Storage interfaces. Also you can setup sql tables for your protocol from resources/mysql and use \Xsolla\SDK\Protocol\Storage\Pdo classes directly or extend it.

<?php
$demoProject = new \Xsolla\SDK\Project(
    '4783',//demo project id
    'key'//demo project secret key
);

$dsn = sprintf('mysql:dbname=%s;host=%s;charset=utf8', 'YOUR_DB_NAME', 'YOUR_DB_HOST');
$pdo = new \PDO($dsn, 'YOUR_DB_USER', 'YOUR_DB_PASSWORD');
$usersStorage = new \Xsolla\SDK\Protocol\Storage\Pdo\UserStorage($pdo);
$paymentsStorage = new \Xsolla\SDK\Protocol\Storage\Pdo\PaymentStandardStorage($pdo);
$ipChecker = new \Xsolla\SDK\Validator\IpChecker;
$protocolBuilder = new \Xsolla\SDK\Protocol\ProtocolFactory($demoProject, $ipChecker);
$protocol = $protocolBuilder->getStandardProtocol($usersStorage, $paymentsStorage);

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$response = $protocol->run($request);
$response->send();

IpChecker - additional security level for situations when your secret key is compromised. It's a optional parameter for ProtocolFactory and you can skip it for development and testing environment. If you use reverse proxy, you should set a list of trusted proxies via Request::setTrustedProxies()

IPN demo

You can run IPN demo with the following commands(required php 5.4+ with built-in server):

$ cd /path/to/xsolla/xsolla-sdk-php
$ composer install
$ php -S localhost:9000 -t example example/ipn_standard.php > /dev/null 2>&1 &
$ # no command
$ curl 'localhost:9000'
$ # user found
$ curl 'http://localhost:9000/?command=check&v1=demo&v2=&v3=&md5=a3561b90df78828133eb285e36965419'
$ # user not found or disabled
$ curl 'http://localhost:9000/?command=check&v1=not_exist&v2=&v3=&md5=5f67cabd3cf27cac2944e7f9f762a42a'
$ # success IPN handling. Response contain payment ID
$ curl 'http://localhost:9000/?command=pay&id=1&v1=demo&v2=&v3=&date=2014-02-19+13%3A03%3A52&sum=1&md5=eae3e95e93ff64f72aeb9fadfd8f0d66'
$ # failed IPN handling. Unprocessable request error
$ curl 'http://localhost:9000/?command=pay&id=2&v1=demo&v2=&v3=&date=2014-02-19+13%3A04%3A30&sum=5&md5=3067aeb81faa883f36d27acc9d808abb'
$ # success payment cancel
$ curl 'http://localhost:9000/?command=cancel&id=3&md5=9ac4f238314b0a0dae5be98151d19f33'

Examples

More examples you can find in example folder. All examples are executable and you can run them with the following commands.

$ cd /path/to/xsolla/xsolla-sdk-php
$ composer install
$ php example/calculator.php

Additional resources