This library provides integration access to DirectPay Api., (*1)
Using cli, (*2)
composer require digitalvirgo/directpay
or manualy add to "require" in composer.json, (*3)
"require": { "digitalvirgo/directpay":"^1.0.0" }
If you are using Composer use autoload functionality:, (*4)
include "vendor/autoload.php";
Basic setup for DirectPay version 2:, (*5)
use DigitalVirgo\DirectPay\Service\Client; $client = new Client();
First parameter is DirectPay url. Default is for version 2. These urls are in constants:
Client::DP_V1_BASE_URL
and Client::DP_V2_BASE_URL
You can pass options for Guzzle client as second parameter., (*6)
use DigitalVirgo\DirectPay\Service\Client; $options = [ 'timeout' => 10, ] $client = new Client(Client::DP_V2_BASE_URL, $options);
Set your credentials in client:, (*7)
$client->setAuth($login, $password);
Alternatively you can pass credentials directly on every request:, (*8)
$request = new \DigitalVirgo\DirectPay\Model\Request\PaymentPointInfoRequest([ 'product' => $product, 'login' => $login, 'password' => $password, ])
Before you will able to generate new order You have to get know your PaymentPoints, (*9)
$paymentPointResponse = $client->paymentPointInfo([ 'product' => [ 'name' => 'product name', 'price' => [ // net price and currency is mandatory 'net' => '1,00', 'gross' => '1,23', 'tax' => '0,23', 'taxRate' => '23', 'currency' => 'PLN', ] ] ]); if ($paymentPointResponse->getError()) { throw new \Exception("Unable to get paymentPoints: {$paymentPointResponse->getError()} {$paymentPointResponse->getErrorDescription()}"); }
$singlePaymentPoint = $paymentPointResponse->getProduct()->getPaymentPoints()->getPaymentPoint()[0]; // we use first given
$orderNewResponse = $client->orderNewRequest([ 'order' => [ 'paymentPointId' => $singlePaymentPoint->getPaymentPointId(), 'orderDescription' => 'order_description', 'product' => [ 'name' => 'product name', 'price' => $singlePaymentPoint->getPrice(), ], 'notifyUrl' => 'https://partnerhost.com/notify', //setup your url's 'orderFailureUrl' => 'https://partnerhost.com/failure', 'orderCompleteUrl' => 'https://partnerhost.com/complete', ], ]);
$orderGetResponse = $client->orderGetRequest([ 'orderId' => $orderNewResponse->getOrderId() ]); if ($orderGetResponse->getError()) { throw new \Exception("Unable to get order: {$orderGetResponse->getError()} {$orderGetResponse->getErrorDescription()}"); } var_dump($orderGetResponse->getOrder()->getOrderId()); var_dump($orderGetResponse->getOrder()->getOrderStatus());
When you set up notifyUrl
in orderNewReqest
you will be notified By DirectPay. You culd parse received xml to OrderNotifyRequest
:, (*10)
$body = file_get_contents('php://input'); //if you can't receive this notification throw some error. // if everything is ok return status 200; $orderNotifyRequest = OrderNotifyRequest::fromXml($body); $response = new OrderNotifyResponse([ 'order' => $orderNotifyRequest->getOrder(), 'updateDate' => new DateTimeImmutable(), ]); print ($response->toXml());
All steps are in example/index.php and example/notify.php;, (*11)