PHP is one of the broadly used platforms for e-commerce websites. PHP is a stable, fast, and trusted language. Today countless e-commerce websites are running on PHP.

A smoothly operating portal to payments is essential to running a business with a significant online presence.

You can easily integrate crypto payments into your own website or app with our UniPayment PHP SDK, after installation you can start accepting crypto-payments.

UniPayment PHP Client

GitHub license Packagist

A PHP client for the UniPayment Client API.

This SDK provides a convenient abstraction of UniPayment's Gateway API and allows developers to focus on payment flow/e-commerce integration rather than on the specific details of client-server interaction using the raw API.

Getting Started

Before using the UniPayment API, sign up for your API key.

If you want to use the Sandbox, sign up here.

Installation

Install Composer

curl -sS https://getcomposer.org/installer | php

Install via composer

Add unipayment/client into require section of composer.json

{
  "require": {
    "unipayment/client": "1.*"
  }
}

Initializing UniPayment client

client = new \UniPayment\Client\UniPaymentClient();
client->getConfig()->setAppId("your app id");
client->getConfig()->setApiKey("you api key");

Sandbox is used in the same way with is_sandbox as true.

$client = new \UniPayment\Client\UniPaymentClient();
$client->getConfig()->setAppId("your app id");
$client->getConfig()->setApiKey("you api key");
$client->getConfig()->setIsSandbox(true)

Create an invoice

Reference:https://unipayment.readme.io/reference/create_invoice

$app_id='your app id'
$api_key='your api key'

$createInvoiceRequest = new \UniPayment\Client\Model\CreateInvoiceRequest();
$createInvoiceRequest->setPriceAmount("10.05");
$createInvoiceRequest->setPriceCurrency("USD");
$createInvoiceRequest->setNotifyUrl("https://example.com/notify");
$createInvoiceRequest->setRedirectUrl("https://example.com/redirect");
$createInvoiceRequest->setOrderId("#123456");
$createInvoiceRequest->setTitle("MacBook Air");
$createInvoiceRequest->setDescription("MacBookAir (256#)");


$client = new \UniPayment\Client\UniPaymentClient();
$client->getConfig()->setAppId($app_id);
$client->getConfig()->setApiKey($api_key);

create_invoice_response = $this->uniPaymentClient->createInvoice($createInvoiceRequest);

CreateInvoiceResponse

{
  "code": "OK",
  "msg": "",
  "data": {
    "app_id": "cee1b9e2-d90c-4b63-9824-d621edb38012",
    "invoice_id": "Dj2mNCXXWCGKT89kcU8NJn",
    "order_id": "ORDER_123456",
    "price_amount": 2.0,
    "price_currency": "USD",
    "network": null,
    "address": null,
    "pay_amount": 0.0,
    "pay_currency": null,
    "exchange_rate": 0.0,
    "paid_amount": 0.0,
    "create_time": "2022-09-14T06:31:57",
    "expiration_time": "2022-09-14T06:36:57",
    "confirm_speed": 2,
    "status": 1,
    "error_status": 0,
    "invoice_url": "https://sandbox-app.unipayment.io/i/Dj2mNCXXWCGKT89kcU8NJn"
  }
}

Handle IPN

Reference:https://unipayment.readme.io/reference/ipn-check

Invoice Status: https://unipayment.readme.io/reference/invoice-status

IPNs (Instant Payment Notifications) are sent to the notify_url when order status is changed to paid, confirmed and complete.

$app_id='your app id'
$api_key='your api key'
$notify='{"ipn_type":"invoice","event":"invoice_created","app_id":"cee1b9e2-d90c-4b63-9824-d621edb38012","invoice_id":"12wQquUmeCPUx3qmp3aHnd","order_id":"ORDER_123456","price_amount":2.0,"price_currency":"USD","network":null,"address":null,"pay_currency":null,"pay_amount":0.0,"exchange_rate":0.0,"paid_amount":0.0,"confirmed_amount":0.0,"refunded_price_amount":0.0,"create_time":"2022-09-14T04:57:54.5599307Z","expiration_time":"2022-09-14T05:02:54.559933Z","status":"New","error_status":"None","ext_args":"Merchant Pass Through Data","transactions":null,"notify_id":"fd58cedd-67c6-4053-ae65-2f6fb09a7d2c","notify_time":"0001-01-01T00:00:00"}';

$client = new \UniPayment\Client\UniPaymentClient();
$client->getConfig()->setAppId($app_id);
$client->getConfig()->setApiKey($api_key);

$response = $$client->checkIpn($notify);

IPN notify

{
    "ipn_type": "invoice",
    "event": "invoice_expired",
    "app_id": "cee1b9e2-d90c-4b63-9824-d621edb38012",
    "invoice_id": "3Q7fyLnB2YNhUDW1fFNyEz",
    "order_id": "20",
    "price_amount": 6.0,
    "price_currency": "SGD",
    "network": null,
    "address": null,
    "pay_currency": null,
    "pay_amount": 0.0,
    "exchange_rate": 0.0,
    "paid_amount": 0.0,
    "confirmed_amount": 0.0,
    "refunded_price_amount": 0.0,
    "create_time": "2022-09-12T03:36:03",
    "expiration_time": "2022-09-12T03:41:03",
    "status": "Expired",
    "error_status": "None",
    "ext_args": null,
    "transactions": null,
    "notify_id": "8ccd2b61-226b-48e5-99b8-acb1f350313e",
    "notify_time": "2022-09-12T03:56:10.5852752Z"
}

Run Example

1.Get source code form GitHub

git clone https://github.com/UniCryptoLab/UniPaymentClient.PHP.git

2.Run project in PHPStorm

Resource