This is a PHP SDK to make easier to communicate with the Plug&Pay API.
Not all functionalities are present. If you're missing something, feel free to request it through our Discord channel.
- The Plug&Pay SDK works with PHP version >= 7.4.
//
//
Your application will be better and more stable if you take the exceptions into account. It is therefore good to catch the errors when they arise. Then you can decide for yourself how you want to deal with the error.
use PlugAndPay\Sdk\Exception\NotFoundException;
use PlugAndPay\Sdk\Exception\RelationNotLoadedException;
use PlugAndPay\Sdk\Exception\ValidationException;
use PlugAndPay\Sdk\Exception\UnauthenticatedException;
try {
// Place your code that uses the SDK inside this block.
} catch (ValidationException $exception) {
# The information that has been sent is incorrect. If
# you want to forward this in your response, this would
# return an HTTP status Unprocessable Entity (422).
// All messages separated by a space
$message = $exception->getMessage();
// First notification of the first error. There can be
// multiple errors with the same field
$exception->errors()[0]->message());
// The field that did not pass the validation successfully.
$exception->errors()[0]->field());
} catch (UnauthenticatedException $exception) {
# Unable to connect with Plug&Pay. Request is
# unauthenticated. Please check if the token
# is correct.
# Unauthorized Error (401).
} catch (NotFoundException $exception) {
# Entry not found. You probably gave the wrong id to
# the service. You have to solve this error in your
# application, otherwise you would expect an HTTP
# Internal Server Error (500).
} catch (RelationNotLoadedException $exception) {
# You are trying to extract a relation from an entity
# that has not yet been loaded. You can include this
# by using the `include()` method of the service. You
# have to solve this error in your application, otherwise
# you would expect an HTTP Internal Server Error (500).
} catch (BadFunctionCallException $exception) {
# It is not possible to use this function in this way.
# Unauthorized Error (500).
} catch (DecodeResponseException $exception) {
# Failed to decode the response. You would expect an
# HTTP Internal Server Error (500) and you have to
# contact Plug&Pay customer service.
}
Your test should always mock external services. For performance reasons, rate limit and for stability of your tests. Below you see an example where we mock the request of deleting an order. It is up to you which response you want to which request.
$guzzleClient = new \GuzzleHttp\Client([
'handler' => function (Request $request) {
$path = $request->getUri()->getPath();
$method = $request->getMethod();
if ($method === 'DELETE' && $path === '/v2/orders') {
return new \GuzzleHttp\Psr7\Response(204);
}
throw new Exception("No guzzle mock found for path $path and method $method");
},
]);
$client = new \PlugAndPay\Sdk\Service\Client(self::KEY, null, $guzzleClient);
$service = new \PlugAndPay\Sdk\Service\OrderService($client);
// ...
The SDK uses predefined responses in the tests. Feel free to use that instead of mocking the Guzzle Client.
use \PlugAndPay\Sdk\Tests\Feature\Order\Mock\OrderShowMockClient;
use \PlugAndPay\Sdk\Service\OrderService;
$client = new \PlugAndPay\Sdk\Tests\Feature\Order\Mock\OrderDestroyMockClient(Response::HTTP_NO_CONTENT, []);
(new \PlugAndPay\Sdk\Service\OrderService($client))->delete(1);
// ...
By using the methods on the client you can get additional responses:
use \PlugAndPay\Sdk\Tests\Feature\Order\Mock\OrderShowMockClient;
use \PlugAndPay\Sdk\Service\OrderService;
$client = (new OrderShowMockClient(['id' => 1]))->billing();
$order = (new \PlugAndPay\Sdk\Service\OrderService($client))->find(1);
$billing = $order->billing();
//