/heidipay-saloon

Integrate HeidiPay API with Saloon

Primary LanguagePHPMIT LicenseMIT

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Integrate your application with HeidiPay API using Saloon

This package provides a simple way to integrate your application with the HeidiPay API.

use Sfolador\HeidiPaySaloon\Services\HeidiPay;
use Sfolador\HeidiPaySaloon\Dto\AuthDto;

$apiUrl = 'https://api.heidipay.com';

$heidipay = new HeidiPay(apiUrl: $apiUrl);

$authDto = AuthDto::from(merchantKey: "merchant-key");
$heidipay->auth($authDto);

//The next requests will use the token returned by the `auth` API


$contractResponse = $heidipay->contract($contractInitDto);

// the response is a `Response` object, you can get the DTO using the `dtoOrFail` method
$contractResponseDto = $contractResponse->dtoOrFail();

Installation

You can install the package via composer:

composer require sfolador/heidipay-saloon

Usage

Auth

Before calling any of the API endpoint it is necessary to authenticate with the HeidiPay API and obtain a token. Please read the official documentation at HeidiPay API for more information.

use Sfolador\HeidiPaySaloon\Services\HeidiPay;
use Sfolador\HeidiPaySaloon\Dto\AuthDto;

$apiUrl = 'https://sandbox-origination.heidipay.com';

//for production use $apiUrl = 'https://api.heidipay.com'; 

$heidipay = HeidiPay::init(apiUrl: $apiUrl);

$authDto = AuthDto::from(merchantKey: "merchant-key"); // the merchant key is provided by HeidiPay
$token = $heidipay->auth($authDto);

Contract init

Initialize a new contract for a user. Please read the official documentation at HeidiPay API for more information.

use Sfolador\HeidiPaySaloon\Services\HeidiPay;
use Sfolador\HeidiPaySaloon\Models\Amount;
use Sfolador\HeidiPaySaloon\Models\Customer;
use Sfolador\HeidiPaySaloon\Models\Webhooks;
use Sfolador\HeidiPaySaloon\Models\CreditInitProduct;
use Sfolador\HeidiPaySaloon\Dto\ContractInitDto;


$heidipay = HeidiPay::init(apiUrl: $apiUrl);

 $amount = new Amount( currency: 'BRL',amount: 100, amountFormat: AmountFormat::DECIMAL);
 $customer = new Customer(
        email: 'customer@example.com', title: '', firstname: 'Test', lastname: 'Test', dateOfBirth: '', contactNumber: '', companyName: '', residence: ''
 );

$webhooks = new Webhooks(
    success: 'https://www.example.com/success',
    failure: 'https://www.example.com/failure',
    cancel: 'https://www.example.com/cancel',
    status: 'https://www.example.com/status',
    mappingScheme: 'default'
);

// remember to set a token for the webhooks and save it in your database to verify the webhooks later
    $webhooks->setToken(str()->random(32));

$products = [new CreditInitProduct(
    sku: null,
    name: 'Test',
    quantity: 1,
    price: '100',
    imageThumbnailUrl: null,
    imageOriginalUrl: null,
    description: null
)];

$contractInitDto = new ContractInitDto($amount, $customer, $webhooks, $products);

$contractResponse = $heidipay->contract($contractInitDto);

// the response is a `Response` object, you can get the DTO using the `dtoOrFail` method
$contractResponseDto = $contractResponse->dtoOrFail();

// $contractResponseDto will be a ContractDto object and have these properties:

$contractResponseDto->action;
$contractResponseDto->redirectUrl; // the url to redirect the user to complete the contract
$contractResponseDto->external_contract_uuid // the contract uuid
$contractResponseDto->application_uuid // the application uuid

Credits

License

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