sfolador/heidipay-saloon

使用 Saloon 集成 HeidiPay API 到 PHP

1.1 2023-05-13 17:02 UTC

README

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

使用 Saloon 将您的应用程序与 HeidiPay API 集成

此包提供了一种简单的方法,可以将您的应用程序与 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();

安装

您可以通过 composer 安装此包

composer require sfolador/heidipay-saloon

用法

认证

在调用任何 API 端点之前,必须使用 HeidiPay API 进行认证并获取令牌。有关更多信息,请参阅 HeidiPay API 的官方文档。

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);

合约初始化

为用户初始化一个新的合约。有关更多信息,请参阅 HeidiPay API 的官方文档。

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

贡献者

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件