sfolador/heidipay-saloon

Integrate HeidiPay API in PHP with Saloon

1.1 2023-05-13 17:02 UTC

README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f48656964695061792532302d25323053616c6f6f6e2e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d73666f6c61646f7225324668656964697061792d73616c6f6f6e267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d496e746567726174652b796f75722b6170706c69636174696f6e2b776974682b48656964695061792b7573696e672b53616c6f6f6e266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d73686f7070696e672d63617274

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.