<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;

/**
 * @author Igor Ratsun
 */
class Paypal_transaction
{

    public function prepare($data = [])
    {
        $sub_total = floatval($data['total']);
        $tax = (!empty($data['tax'])) ? round($sub_total * floatval($data['tax']), 2) : 0;

        $item = new Item();
        $item->setName($data['description'])
                ->setCurrency($data['currency'])
                ->setQuantity(1)
                ->setPrice($sub_total);

        $itemList = new ItemList();
        $itemList->setItems([$item]);

        $details = new Details();
        $details->setTax($tax)
                ->setSubtotal($sub_total);

        $amount = new Amount();
        $amount->setCurrency($data['currency'])
                ->setTotal($sub_total + $tax)
                ->setDetails($details);

        $transaction = new Transaction();
        $transaction->setAmount($amount)
                ->setItemList($itemList)
                ->setDescription($data['description'])
                ->setInvoiceNumber($data['invoice']);

        return $transaction;
    }

}
