rixtrayker / laravel-tamara
Laravel wrapper for Tamara SDK
1.2.4
2024-09-04 17:19 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.4
- illuminate/contracts: ^9|^10|^11
- psr/http-client: ^1.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
README
Laravel wrapper for Tamara SDK with small amount of changes to make it work with Laravel 9.x.
Installation
You can install the package via composer:
composer require alazzi-az/laravel-tamara
You can publish the config file with:
php artisan vendor:publish --tag="laravel-tamara-config"
This is the contents of the published config file:
return [ 'uri' => env('TAMARA_URI', 'https://api-sandbox.tamara.co'), 'token' => env('TAMARA_TOKEN', ''), 'notification_token' => env('TAMARA_NOTIFICATION_TOKEN', ''), 'request_timeout' => env('TAMARA_REQUEST_TIMEOUT', 1000), 'transport' => null, 'success_url' => env('APP_URL').'/tamara_handle_success_redirect', 'failure_url' => env('APP_URL').'/tamara_handle_failure_redirect', 'cancel_url' => env('APP_URL').'/tamara_handle_cancel_redirect', 'notification_url' => env('APP_URL').'/tamara_notification_redirect', ];
Usage For Client Instance
use \AlazziAz\Tamara\Facades\Tamara; $client = \AlazziAz\Tamara\Facades\Tamara::client(); $response = $client->getPaymentTypes('SA');
Create Order Checkout
use \AlazziAz\Tamara\Facades\Tamara; use \AlazziAz\Tamara\Tamara\Model\Order\MerchantUrl; use \AlazziAz\Tamara\Tamara\Model\Order\OrderItemCollection; use \AlazziAz\Tamara\Tamara\Model\Order\Order; use \AlazziAz\Tamara\Tamara\Model\Order\OrderItem; use AlazziAz\Tamara\Tamara\Request\Checkout\CreateCheckoutRequest; use \AlazziAz\Tamara\Tamara\Model\Order\Consumer; use AlazziAz\Tamara\Tamara\Model\Money; use AlazziAz\Tamara\Tamara\Model\Order\Address; $merchantUrl = app()->make(MerchantUrl::class); $order = new Order(); $order->setMerchantUrl($merchantUrl); $orderItemCollection = new OrderItemCollection(); $client = Tamara::client(); // firs set Order Details $order->setOrderReferenceId($orderReferenceId); $order->setLocale($local); $order->setCurrency($orderCurrency); $order->setTotalAmount(new Money($totalAmount, $orderCurrency)); $order->setCountryCode($countryCode); $order->setPaymentType($paymentType); $order->setDescription($orderDescription); $order->setTaxAmount(new Money($taxAmount, $orderCurrency)); $order->setDiscount(new Discount($discountType, new Money($discountAmount, $orderCurrency))); $order->setShippingAmount(new Money($shippingAmount, $orderCurrency)); // second set Consumer data $consumer = new Consumer(); $consumer->setFirstName($firstName); $consumer->setLastName($lastName); $consumer->setEmail($email); $consumer->setPhoneNumber($phoneNumber); $order->setConsumer($consumer); // third sett Billing Address $address = new Address(); $address->setFirstName($firstName); $address->setLastName($lastName); $address->setLine1($line1); $address->setLine2($line2); $address->setRegion($region); $address->setCity($city); $address->setPhoneNumber($phoneNumber); $address->setCountryCode($countryCode); $order->setBillingAddress($address); // forth set Shipping Address $address = new Address(); $address->setFirstName($firstName); $address->setLastName($lastName); $address->setLine1($line1); $address->setLine2($line2); $address->setRegion($region); $address->setCity($city); $address->setPhoneNumber($phoneNumber); $address->setCountryCode($countryCode); $order->setShippingAddress($address); // fifth we add items to items collection $orderItem = new OrderItem(); $orderItem->setName($name); $orderItem->setQuantity($quantity); $orderItem->setUnitPrice(new Money($unitPrice, $currency)); $orderItem->setType($type); $orderItem->setTotalAmount(new Money($totalPrice, $currency)); $orderItem->setTaxAmount(new Money($taxAmount, $currency)); $orderItem->setDiscountAmount(new Money($discount, $currency)); $orderItem->setReferenceId($uniqueId); $orderItem->setSku($uniqueId); if ($imageUrl) { $orderItem->setImageUrl($imageUrl); } // append items can be in iterator $orderItemCollection->append($orderItem); // sixth set items collection to order $order->setItems($this->orderItemCollection); // seventh create request object $request = new CreateCheckoutRequest($this->order); // now we can make request $response = $client->createCheckout($request); // get data from response $checkoutUrl=$response->getCheckoutResponse()->getCheckoutUrl(); // redirect customer to complete payment $orderID=$response->getCheckoutResponse()->getOrderId(); $heckOutID=$response->getCheckoutResponse()->getCheckoutId();
After Customer complete payment we need to Authorise Order Transaction
in controller that handle success_url endpoint
use AlazziAz\Tamara\Tamara\Request\Order\AuthoriseOrderRequest; use AlazziAz\Tamara\Facades\Tamara; $authOrder = new AuthoriseOrderRequest($request->get('orderId')); $authedResponse = Tamara::client()->authoriseOrder($authOrder); $orderStatus=$authedResponse->getOrderStatus(); // authorised, approved, captured, fully_captured, declined, refunded, failed, expired // then check the status of the order and update your app state
you can Capture payment by using AlazziAz\Tamara\Tamara\Request\Payment\CaptureRequest
Usage For Notification Service Instance
use AlazziAz\Tamara\Facades\Tamara; $notificationService = Tamara::notificationService(); $message = $notificationService->processAuthoriseNotification(); // then you can update state of the app e.g. $orderId=$message->getOrderReferenceId(); $orderStatus=$message->getEventType(); //...
Usage Some Instance by Laravel Container to get the instance injected config values
use Illuminate\Support\Facades\App; use AlazziAz\Tamara\Tamara\Model\Order\MerchantUrl; $merchantUrl = App::make(MerchantUrl::class); // app()->make(MerchantUrl::class);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.