payos/payos

A payOS package for PHP

1.0.5 2024-03-29 03:19 UTC

This package is not auto-updated.

Last update: 2024-06-07 04:05:55 UTC


README

Packagist Downloads

PayOS is a PHP library for the PayOS API.

Getting started

Installation

composer require payos/payos

Usage

You need to initialize the PayOS object with the Client ID, Api Key and Checksum Key of the payment channel you created.

<?php

require_once __DIR__ . '/vendor/autoload.php';

use PayOS\PayOS;

$payOS = new PayOS(CLIENT_ID, API_KEY, CHECKSUM_KEY);

Create payment link

Create a payment link for the order data passed in the parameter.

Parameter data type:

$data = [
    "orderCode" => "string",
    "amount" => "integer",
    "description" => "string",
    "returnUrl" => "string",
    "cancelUrl" => "string",
    "signature" => "string|null",
    "items" => "array|null",
    "buyerName" => "string|null",
    "buyerEmail" => "string|null",
    "buyerPhone" => "string|null",
    "buyerAddress" => "string|null",
    "expiredAt" => "integer|null",
];

Items data type:

$items = [
    [
        "name" => "string",
        "quantity" => "integer",
        "price" => "integer",
    ]
];

Return data type:

$response = [
    "bin" => "string",
    "accountNumber" => "string",
    "accountName" => "string",
    "amount" => "integer",
    "description" => "string",
    "orderCode" => "integer",
    "currency" => "string",
    "paymentLinkId" => "string",
    "status" => "string",
    "checkoutUrl" => "string",
    "qrCode" => "string",
];

Example:

$data = [
    "orderCode" => intval(substr(strval(microtime(true) * 10000), -6)),
    "amount" => 2000,
    "description" => "Create payment link",
    "returnUrl" => $YOUR_DOMAIN . "/success.html",
    "cancelUrl" => $YOUR_DOMAIN . "/cancel.html"
];

try {
    $response = $payOS->createPaymentLink($data);
    return redirect($response['checkoutUrl']);
} catch (\Throwable $th) {
    return $th->getMessage();
}

Get payment link information

Get payment information of an order that has created a payment link.

Parameter data type:

$orderCode = "integer";

Return data type:

$response = [
    "id" => "string",
    "orderCode" => "integer",
    "amount" => "integer",
    "amountPaid" => "integer",
    "amountRemaining" => "integer",
    "status" => "string",
    "createAt" => "string",
    "transactions" => "array",
    "cancellationReason" => "string|null",
    "canceledAt" => "string|null",
];

Transactions data type:

$transactions = [
    [
        "reference" => "string",
        "amount" => "integer",
        "accountNumber" => "string",
        "description" => "string",
        "transactionDateTime" => "string",
        "virtualAccountName" => "string|null",
        "virtualAccountNumber" => "string|null",
        "counterAccountBankId" => "string|null",
        "counterAccountBankName" => "string|null",
        "counterAccountName" => "string|null",
        "counterAccountNumber" => "string|null",
    ]
];

Example:

$orderCode = 123456;

try {
    $response = $payOS->getPaymentLinkInfomation($orderCode);
    return $response;
} catch (\Throwable $th) {
    return $th->getMessage();
}

Cancel payment link

Cancel the payment link of the order.

Parameter data type:

$orderCode = "integer";
$cancelBody = "string|null";

Return data type:

$response = [
    "id" => "string",
    "orderCode" => "integer",
    "amount" => "integer",
    "amountPaid" => "integer",
    "amountRemaining" => "integer",
    "status" => "string",
    "createAt" => "string",
    "transactions" => "array",
    "cancellationReason" => "string|null",
    "canceledAt" => "string|null",
];

Transactions data type:

$transactions = [
    [
        "reference" => "string",
        "amount" => "integer",
        "accountNumber" => "string",
        "description" => "string",
        "transactionDateTime" => "string",
        "virtualAccountName" => "string|null",
        "virtualAccountNumber" => "string|null",
        "counterAccountBankId" => "string|null",
        "counterAccountBankName" => "string|null",
        "counterAccountName" => "string|null",
        "counterAccountNumber" => "string|null",
    ]
];

Example:

$orderCode = 123456;
$reason = "Cancel payment link";

try {
    $response = $payOS->cancelPaymentLink($orderCode, $cancelBody);
    return $response;
} catch (\Throwable $th) {
    return $th->getMessage();
}

Confirm webhook

Validate the Webhook URL of a payment channel and add or update the Webhook URL for that Payment Channel if successful.

Example:

$webhookUrl = "https://your-webhook-url/";

try {
    $payOS->confirmWebhook($webhookUrl);
    return "Webhook confirmed";
} catch (\Throwable $th) {
    return $th->getMessage();
}

Verify webhook data

Verify data received via webhook after payment.

Parameter data type:

$webhook = [
    "code" => "string",
    "desc" => "string",
    "data" => "array",
    "signature" => "string",
];

$data = [
    "orderCode" => "integer",
    "amount" => "integer",
    "description" => "string",
    "accountNumber" => "string",
    "reference" => "string",
    "transactionDateTime" => "string",
    "currency" => "string",
    "paymentLinkId" => "string",
    "code" => "string",
    "desc" => "string",
    "counterAccountBankId" => "string",
    "counterAccountBankName" => "string",
    "counterAccountName" => "string",
    "counterAccountNumber" => "string",
    "virtualAccountName" => "string",
    "virtualAccountNumber" => "string",
];

Return data type:

$response = [
    "orderCode" => "integer",
    "amount" => "integer",
    "description" => "string",
    "accountNumber" => "string",
    "reference" => "string",
    "transactionDateTime" => "string",
    "currency" => "string",
    "paymentLinkId" => "string",
    "code" => "string",
    "desc" => "string",
    "counterAccountBankId" => "string",
    "counterAccountBankName" => "string",
    "counterAccountName" => "string",
    "counterAccountNumber" => "string",
    "virtualAccountName" => "string",
    "virtualAccountNumber" => "string",
];

Example:

$webhook = $request->body();

try {
    $response = $payOS->verifyWebhookData($webhook);
    return $response;
} catch (\Throwable $th) {
    return $th->getMessage();
}