Giter VIP home page Giter VIP logo

merchant-sdk's Introduction

Getting Started

Setup/Install

Install through Composer.

composer require apvalkov/merchant-sdk

Example redirect to pay page

$apiKey    = 'Your api key here';

$config       = new Config(Environment::Sandbox, $apiKey, ApiVersion::v1);
$inxyPayments = new INXYPayments($config);

$orderAmountInUSD = 20;
$orderName        = 'Order #1';
$customer         = new Customer('[email protected]', 'John', 'Doe');
$sessionRequest   = new SessionRequest($orderAmountInUSD, $orderName);

$sessionRequest->setOrderId('order_123');
$sessionRequest->setCryptocurrencies([CurrencyCode::USDT, CurrencyCode::BTC]);
$sessionRequest->setPostbackUrl('https://example.com/postback');
$sessionRequest->setCancelUrl('https://example.com/cancel');
$sessionRequest->setSuccessUrl('https://example.com/success');
$sessionRequest->setCustomer($customer);

$sessionResponse = $inxyPayments->createSession($sessionRequest);

header( 'Location: ' . $sessionResponse->getRedirectUri());

Example handle webhook

function handleWebhooks($request) {
    $secretKey  = 'Your secret key here';
    $validator  = new Validator($secretKey);
    $signedHash = $request->headers['X-INXY-Payments-Signature'];

    $payload = $request->getBody()->getContents(); // fetch json from your request

    if (!$validator->isValid($payload, $signedHash)) {
        throw new BadRequestException('No valid webhook');
    }

    $data = json_decode($payload, false);

    switch ($data->name) {
        case EventName::PaymentsInit:
            handlePaymentsInitWebhook($data);
            break;
        case EventName::PaymentsWaitingConfirmations:
            handlePaymentsWaitingConfirmationsWebhook($data);
            break;
        case EventName::PaymentsReceived:
            handlePaymentsReceivedWebhook($data);
            break;
        default:
            throw new InvalidArgumentException('Undefined webhook name');
    }
}

function handlePaymentsInitWebhook(stdClass $webhookData) {
    $webhook = PaymentsInitWebhookFactory::create($webhookData);

    if ($webhook->data->paymentIntent->status === PaymentIntentStatus::WaitingPayment) {
        /** Waiting first payment */
    }

    if ($webhook->data->paymentIntent->status === PaymentIntentStatus::WaitingPaymentPart) {
        /** Waiting part payment after partially paid */
    }

    /** Your code here */
}

function handlePaymentsWaitingConfirmationsWebhook(stdClass $webhookData) {
    $webhook = PaymentsWaitingConfirmationsWebhookFactory::create($webhookData);

    /** Your code here */
}

function handlePaymentsReceivedWebhook(stdClass $webhookData) {
    $webhook = PaymentsReceivedWebhookFactory::create($webhookData);

    if ($webhook->data->paymentIntent->status === PaymentIntentStatus::Paid) {
        /** Success payment code */
    }

    if ($webhook->data->paymentIntent->status === PaymentIntentStatus::PartiallyPaid) {
        /** Partially paid actions */
    }

    /** Your code here */
}

merchant-sdk's People

Contributors

apvalkov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.