xsolla / xsolla-sdk-php
Xsolla SDK for PHP. Xsolla is the authorized reseller and merchant providing e-commerce services for online games.
Installs: 283 200
Dependents: 0
Suggesters: 0
Security: 0
Stars: 34
Watchers: 12
Forks: 53
Open Issues: 16
Requires
- php: ^7.3|~8.0
- ext-curl: *
- ext-json: *
- guzzlehttp/guzzle: ~6.0 || ~7.0
- symfony/http-foundation: ~2.3 || ~3.0 || ~4.0 || ~5.0 || ~6.0.0 || ~7.0
Requires (Dev)
- php: ^7.3|~8.0
- friendsofphp/php-cs-fixer: ~2.13
- mtdowling/burgomaster: ^0.0.3
- phpunit/phpunit: ^9.0
- symfony/process: ~4.1
- dev-master
- v4.3.2
- v4.3.1
- v4.3.0
- v4.2.0
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.2
- v4.0.1
- v4.0.0
- v3.3.0
- v3.2.0
- v3.1.0
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v2.0.0-BETA1
- v1.1.1
- v1.1.0
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-PAYMENTS-19117_increase_guzzle_version
- dev-PAYMENTS-17868_change_documentation
- dev-PAYMENTS-17730_fix_bugs_sdk
- dev-own3d
- dev-PAYMENTS-4030_Increase_PHP_version
- dev-LIVETEAM-19909
- dev-LIVETEAM-19892
- dev-LIVETEAM-19864
- dev-LIVETEAM-19840
This package is auto-updated.
Last update: 2024-05-23 11:11:26 UTC
README
An official PHP SDK for interacting with Xsolla API
This SDK can be used for:
- obtaining an authorization token
- processing of basic webhooks (user_validation, payment, refund, etc.)
Features
- Full customisation of Payment UI with the help of different methods of getting token.
- Client for all API methods, making your integration easy and convenient. You can use it for setting up and updating virtual currency, items and subscription plans, for managing the users balance, for checking the finance information with the help of Report API and so on.
- Convenient webhook server:
- To start you need only one callback function.
- All security checking already implemented: signature authentication and IP whitelisting.
- Full customisation of notification processing logic, if standard server class doesn’t suit you.
- SDK is built on Guzzle v3, and utilizes many of its features, including persistent connections, parallel requests, events and plugins (via Symfony2 EventDispatcher), service descriptions, over-the-wire logging, caching, flexible batching, and request retrying with truncated exponential back off.
Requirements
- PHP ^7.3 or ^8.0
- The following PHP extensions are required:
- curl
- json
Getting Started
Please register your Publisher Account and create the project. In order to use the PHP SDK Library you'll need:
- MERCHANT_ID
- API_KEY
- PROJECT_ID
- PROJECT_KEY
You can obtain these parameters using the information in your Company Profile and Project Settings.
Installation
Installing via Composer
The recommended way to install Xsolla SDK for PHP is through Composer.
$ cd /path/to/your/project
$ composer require xsolla/xsolla-sdk-php
After installing, you need to require Composer's autoloader:
require '/path/to/vendor/autoload.php';
Installing via Phar
You can download the packaged phar and include it in your scripts to get started:
require '/path/to/xsolla.phar';
Installing via Zip
You can download the zip file, unzip it into your project to a location of your choosing, and include the autoloader:
require '/path/to/xsolla-autoloader.php';
Quick Examples
Receive webhooks
There is a build in server class to help you to handle the webhooks.
Solution with webhook server:
<?php use Xsolla\SDK\Webhook\WebhookServer; use Xsolla\SDK\Webhook\Message\Message; use Xsolla\SDK\Webhook\Message\NotificationTypeDictionary; use Xsolla\SDK\Exception\Webhook\XsollaWebhookException; $callback = function (Message $message) { switch ($message->getNotificationType()) { case NotificationTypeDictionary::USER_VALIDATION: /** @var Xsolla\SDK\Webhook\Message\UserValidationMessage $message */ // TODO if user not found, you should throw Xsolla\SDK\Exception\Webhook\InvalidUserException break; case NotificationTypeDictionary::PAYMENT: /** @var Xsolla\SDK\Webhook\Message\PaymentMessage $message */ // TODO if the payment delivery fails for some reason, you should throw Xsolla\SDK\Exception\Webhook\XsollaWebhookException break; case NotificationTypeDictionary::REFUND: /** @var Xsolla\SDK\Webhook\Message\RefundMessage $message */ // TODO if you cannot handle the refund, you should throw Xsolla\SDK\Exception\Webhook\XsollaWebhookException break; default: throw new XsollaWebhookException('Notification type not implemented'); } }; $webhookServer = WebhookServer::create($callback, PROJECT_KEY); $webhookServer->start();
Solution with just helper classes in some php function:
public function handleRequest() { $request = Request::createFromGlobals(); $message = Message::fromArray($request->toArray()); switch ($message->getNotificationType()) { case NotificationTypeDictionary::USER_VALIDATION: /** * https://developers.xsolla.com/webhooks/operation/user-validation/ * @var Xsolla\SDK\Webhook\Message\UserValidationMessage $message */ if ($message->getUserId() !== 'our_user_id') { return YourResponseClass(json_encode(['error' => ['code' => 'INVALID_USER', 'message' => 'Invalid user']]), 400); } break; case NotificationTypeDictionary::PAYMENT: /** @var Xsolla\SDK\Webhook\Message\PaymentMessage $message */ break; case NotificationTypeDictionary::REFUND: /** @var Xsolla\SDK\Webhook\Message\RefundMessage $message */ break; default: throw new \Exception('Notification type not implemented'); } return YourResponseClass('', 200); }
Once you've finished the handling of notifications on your server, please set up the URL that will receive all webhook notifications on the Settings page for your project.
Troubleshooting
You can find solutions for the most frequently encountered errors in our documentation.
Contributing
Please take a look at the CONTRIBUTING.md to see how to get your changes merged in.