How to add multiple items in the $ paymentRequest-addItem of the Pagseguro?

0

Well, I added 1 item to the Pagseguro checkout but I would like to insert several items. How can I enter multiple items by passing these items to $ data ?

public function pagar(){

    // Pega os itens enviados para o método produtos + adiciona os itens ao checkout do Pagseguro
    $items = self::produtos();
    $produtos = new PagSeguroItem($items);
    $paymentRequest->addItem($produtos);
...
}

public static function produtos()
{
    $data = array(       
        'id'            => '0001', 
        'description'   => 'Notebook Dell', 
        'quantity'      => 2, 
        'amount'        => 2150.00
    );
    return $data;
}
    
asked by anonymous 19.10.2016 / 02:09

1 answer

0

The only way I see it is to have a class for Secure Pag, since each item should have its addItem .

I'll illustrate with a generic class, other than the SafePage class:

<?php

class Pagseguro
{
    private $itens = array();

    public function addItem(PagSeguroItem $item)
    {
        array_push($this->itens, $item);
    }

    public function make()
    {
        foreach($this->itens as $item) {
            //Aqui iria o real add item
            echo $item->nome . " | " . $item->preco . "<br>";
        }

    }

}

class PagSeguroItem
{
    public $nome, $preco;

    public function __construct($nome, $preco) 
    {
        $this->nome = $nome;
        $this->preco = $preco;
    }
}

$ps = new PagSeguro();

$ps->addItem(new PagSeguroItem('Notebook', 1800.80));
$ps->addItem(new PagSeguroItem('Mouse sem fio', 50.80));
$ps->addItem(new PagSeguroItem('Case notebook', 100.80));

$ps->make();

The output would be:

Notebook | 1800.8
Mouse sem fio | 50.8
Case notebook | 100.8

So, in foreach of make() , you could call $paymentRequest->addItem($item->id, $item->nome, $item->quantidade, $item->preco); , because according to API documentation , addItem would look like this:

$paymentRequest = new PagSeguroPaymentRequest();  
$paymentRequest->addItem('0001', 'Notebook', 1, 2430.00);  
$paymentRequest->addItem('0002', 'Mochila',  1, 150.99); 
    
19.10.2016 / 16:47