wdevkit/sdk

A sdk to wdevkit services.

v1.0.3 2021-01-26 17:23 UTC

This package is auto-updated.

Last update: 2024-05-27 01:13:36 UTC


README

A sdk to wdevkit services

Latest Version on Packagist GitHub Tests Action Status Total Downloads

A sdk that simplifies the usage of many wdevkit services, with a tested, clean and easy to use api.

Installation

You can install the package via composer:

composer require wdevkit/sdk

Usage

The wdevkit/sdk provides api to multiple wdevkit services. The services are provided by the \Wdevkit\Sdk\Api class, by calling the respective service name as a static method, as you can see below:

$accounts = \Wdevkit\Sdk\Api::auth($settings);
$payments = \Wdevkit\Sdk\Api::payments($settings);
$checkout = \Wdevkit\Sdk\Api::checkout($settings);
$orders = \Wdevkit\Sdk\Api::orders($settings);
$inventory = \Wdevkit\Sdk\Api::inventory($settings);

By calling the static method, a new instance of the service handler will be instantiated, with the given $settings array. In this settings, you must a base_uri and a token, on order to correctly make the requests to each service. You also can pass a client instance to the service. If not provided, a default client using Guzzle will be used, with the given request headers:

'headers' => [
    'User-Agent' => 'wdevkit/sdk:1.x',
    'Accept'     => 'application/json',
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer ' . <token>,
]

Each service handler instantiated may have its own methods, wich will be described in the respective service documentation.

Auth

Fetch User

In order to check if you are authenticated, you can make a request to a wdevkit auth service /sanctum/user endpoint by using the \Wdevkit\Sdk\Api::auth($settings)->fetchUser([]) method.

$settings = [
    'base_uri' => 'https://auth.your_domain.dev',
    'token' => 'some_token'
];

$checkout = \Wdevkit\Sdk\Api::auth($settings)->fetchUser([]);

// response

'data' => [
    'uuid' => 'a25d81de-8715-480e-a6cb-df24649e7479',
    'name' => 'John Doe',
    'email' => 'john@email.com',
]

Checkout

Create a Checkout

In order to create a checkout, you can use the \Wdevkit\Sdk\Api::checkout($settings)->create([]) method from the sdk, and you need to provide the required data.

$settings = [
    'base_uri' => 'https://checkout.your_domain.dev',
    'token' => 'some_token'
];

$checkout = \Wdevkit\Sdk\Api::checkout($settings)->create([
    'items' => [
        [
            'name' => 'Black shirt #1', // required
            'sku' => 'black_shirt_1', // required
            'qty' => 2, // required
            'price' => 15.10, // required
        ],
    ],
    'meta' => [
        'source_type' => 'acme_system', // required
        'source_id' => '123456', // required
    ],
]);

// response

'data' => [
    'uuid' => '2dcdf759-1ba1-4d25-aca9-9c0c11224cfd',
    'actions' => [
        'get' => 'https://checkout.your_domain.dev/checkouts/2dcdf759-1ba1-4d25-aca9-9c0c11224cfd/profiles',
    ]
]

Payments

Fetch Methods

To fetch payment methods options, you can use the \Wdevkit\Sdk\Api::payments($settings)->fetchMethods([]) method from the sdk.

$settings = [
    'base_uri' => 'https://payments.your_domain.dev',
    'token' => 'some_token'
];

$methods = \Wdevkit\Sdk\Api::payments($settings)->fetchMethods([]);

// response

'data' => [
    'methods' => [
        ['code' => 'credit_card', 'title' => 'Credit Card', 'driver' => 'stripe'],
        ['code' => 'transfer', 'title' => 'Transfer', 'driver' => 'bank_acme'],
    ]
]
Create a Payment

To create a payment, you can use the \Wdevkit\Sdk\Api::payments($settings)->create([]) method from the sdk, and you need to provide the required data.

$settings = [
    'base_uri' => 'https://payments.your_domain.dev',
    'token' => 'some_token'
];

$methods = \Wdevkit\Sdk\Api::payments($settings)->create([
    'customer' => [
        'name' => 'John Doe', // required
        'document' => '12345678909', // required
        'email' => 'john@test.dev', // required
    ],
    'payment' => [
        'method' => 'credit_card', // required
        'amount' => 125.35, // required,
        'installments' => 1, // required
        'method_data' => [], // required, and attributes required depending on method.
    ],
    'meta' => [
        'source_type' => 'acme_system', // required
        'source_id' => '123456', // required
    ],
]);

// response

'data' => [
    'payment_uuid' => 'fb624d85-5a13-47c7-8ea7-b917490d5e12',
    'payment_method' => 'credit_card',
    'amount' => '42',
    'state' => 'processed',
    'status' => 'success',
    'errors' => null,
    'actions' => [
        ['title' => 'Refund', 'code' => 'refund', 'url' => 'https://refund_route'],
        ['title' => 'Details', 'code' => 'details', 'url' => 'https://details_route'],
    ],
]
Fetch a Payment

To fetch a previously created payment, you can use the \Wdevkit\Sdk\Api::payments($settings)->fetch($uuid) method from the sdk. You need to provide a payment uuid on the fetch method.

$settings = [
    'base_uri' => 'https://payments.your_domain.dev',
    'token' => 'some_token'
];

$methods = \Wdevkit\Sdk\Api::payments($settings)->fetch('fb624d85-5a13-47c7-8ea7-b917490d5e12');

// response

'data' => [
    'id' => 50,
    'uuid' => 'fb624d85-5a13-47c7-8ea7-b917490d5e12',
    'driver' => 'acme',
    'method' => 'credit_card',
    'state' => 'processed',
    'status' => 'success',
    'amount' => '119.0000',
    'installments' => 1,
    'description' => 'test payment',
    'recurring' => null,
    'customer' => [
        'name' => 'John Doe',
        'email' => 'johndoe@email.com',
        'phone' => '999999999',
        'document' => '999999999999'
    ]
]

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.