How to add multiple items in $paymentRequest->addItem?

Asked

Viewed 495 times

1

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

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;
}

2 answers

1

You can’t add paymentRequest to a variable and then increment it?

Using moip I had a similar situation and solved as follows:

 $order = $moip->orders()->setOwnId(uniqid())->setCustomerId($idcliente_moip);
      for($x=0; $x < count($produtos); $x++){
           $order->addItem($produtos[$x], 1, $produtos[$x], $totalEmCentavos);
      }

 $order->create();

Note that in this case $products is an array with the list products.

As I never used the pagseguro, I’m not sure if this way works, however worth the try.

0

The only way I see is to have a class for Pay Insurance, because each item must have its own addItem.

I will illustrate with a generic class, other than the Insurance Payment:

<?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 exit would be:

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

Thus, in the foreach of make(), you could call the $paymentRequest->addItem($item->id, $item->nome, $item->quantidade, $item->preco);, because according to the API documentation, the addItem would be so:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.