maree/tamara

将 tamara 集成到您的 PHP Laravel 项目的包

dev-main 2023-02-27 23:03 UTC

This package is auto-updated.

Last update: 2024-09-28 02:19:14 UTC


README

Tamara 是一种免息且无额外费用的账单服务。从我们的合作伙伴处购买您感兴趣的商品,并将金额分成3或4次轻松的月付款,首付款在购买时支付。

安装

您可以通过 Composer 安装此包。

composer require maree/tamara

使用以下命令发布您的 tamara 配置文件

php artisan vendor:publish --provider="Maree\Tamara\TamaraServiceProvider" --tag="tamara"

然后,从 config/tamara.php 文件更改您的 tamara 配置

    "token"        => "" , //from your tamara account 
    "mode"         => "test", //test or live
    'country_code' => "SA",
    'currency'     => "SAR",

用法

检查可用的支付选项

use Maree\Tamara\Tamara;

        $order = ['phone' => '00966511111110', 'total' => 600];
        $response = (new Tamara())->checkPaymentOptionsAvailability($order);

创建结账订单

use Maree\Tamara\Tamara;

        $order       = ['order_num' => '123', 'total' => 500,'notes' => 'notes ', 'discount_name' => 'discount coupon','discount_amount' => 50,'vat_amount' => 50,'shipping_amount' => 20];
        $products[0] = ['id' => '123','type' => 'mobiles' ,'name' => 'iphone','sku' => 'SA-12436','image_url' => 'https://example.com/image.png','quantity' => 1,'unit_price'=>50,'discount_amount' => 5,'tax_amount'=>10,'total' => 70];
        $products[1] = ['id' => '345','type' => 'labtops' ,'name' => 'macbook air','sku' => 'SA-789','image_url' => 'https://example.com/image.png','quantity' => 1,'unit_price'=>200,'discount_amount' => 50,'tax_amount'=>100,'total' => 300];
        $consumer    = ['first_name' => 'mohamed','last_name' => 'maree' ,'phone' => '01234567890','email' => 'm7mdmaree26@gmail.com'];
        $billing_address  = ['first_name' => 'mohamed','last_name' => 'maree','line1' => 'mehalla' ,'city' => 'mehalla','phone' => '01234567890'];
        $shipping_address = ['first_name' => 'mohamed','last_name' => 'maree','line1' => 'mehalla' ,'city' => 'mehalla','phone' => '01234567890'];
        $urls = ['success' => 'http://yoursite/success','failure' => 'http://yoursite/failure','cancel' => 'http://yoursite/cancel','notification' => 'http://yoursite/notification'];
        $response = (new Tamara())->createCheckoutSession($order,$products,$consumer,$billing_address,$shipping_address,$urls);
        return redirect()->to($response['checkout_url']);
  • urls 数组包含每个状态的回调 URL('success'、'failure'、'cancel'、'notification'),以便为每个状态创建路由
  • 如果您在 urls 数组的 'success' 键中传递了 route('tamara.result'),则响应将在 'PaymentController@tamaraResult' 函数中返回
  • 在 routes.php 中添加以下内容
    Route::get('tamara-response', 'PaymentController@tamaraResult')->name('tamara.result');
  • 在 controllers/PaymentController 中创建回调函数以检查响应
    public function tamaraResult(Request $request)
    {
        if ($request->paymentStatus == 'approved') {
        	//update order payment status 
            return view('success_payment');
        } else {
            return view('fail_payment');
        }
    }

获取订单详情

use Maree\Tamara\Tamara;

        //use id that you used in createCheckoutSession function $order['order_num']
        $response = (new Tamara())->getOrderDetails($orderId = '123');

取消订单

use Maree\Tamara\Tamara;

        //get id from createCheckoutSession function response
        $order = ['id' => '9d7546e6-59e5-46ab-884c-9dbf95e2877c' ,'amount' => 100];
        $response = (new Tamara())->cancelOrder($order);

文档