Variable error within the function

Asked

Viewed 346 times

4

I’m having problems and for what I searched in the internet forums, others have the same doubt that I.

I cannot use PHP variables within the transaction function. How do I set a variable?

For example, this does not work:

$id = $_POST["id_saida"];
$nome = $_POST["nome_produto"];
$valor   = $_POST["valor_final"];


class CreatePaymentRequestLightbox
{
public static function main()
{

Original:

$paymentRequest->addItem('0001', 'Notebook prata', 1, 430.00); //original

How I wish it would work:

$paymentRequest->addItem($id, $nome, $n, $valor); // como gostaria

Only it’s wrong, it’s empty. What to do?

<?php

require_once "includes/ps/PagSeguroLibrary/PagSeguroLibrary.php";
$form = $_GET['form'];
if ($form == "pagseg"){
echo "ok";
$id_transfer = $_POST["id_transfer"];
$id = $_POST["id_saida"];
$nome = $_POST["nome_produto"];
$valor   = $_POST["valor_final"];

$v1 ="teste";

/**
* Class with a main method to illustrate the usage of the domain class     
PagSeguroPaymentRequest
*/
class CreatePaymentRequestLightbox
{
    public static function main()
    {
        // Instantiate a new payment request
        $paymentRequest = new PagSeguroPaymentRequest();

        // Set the currency
        $paymentRequest->setCurrency("BRL");

        // Add an item for this payment request
        $paymentRequest->addItem($v1, 'Notebook prata', 1, 430.00);

        // Set a reference code for this payment request, it is useful to identify this   
      payment
        // in future notifications.
        $paymentRequest->setReference("REF123");

        // Set shipping information for this payment request
        $sedexCode = PagSeguroShippingType::getCodeByType('SEDEX');
        $paymentRequest->setShippingType($sedexCode);
        $paymentRequest->setShippingAddress(
            '01452002',
            'Av. Brig. Faria Lima',
            '1384',
            'apto. 114',
            'Jardim Paulistano',
            'São Paulo',
            'SP',
            'BRA'
        );

        // Set your customer information.
        $paymentRequest->setSender(
            'João Comprador',
            '[email protected]',
            '11',
            '56273440',
            'CPF',
            '156.009.442-76'
        );

        // Set the url used by PagSeguro to redirect user after checkout process ends
        $paymentRequest->setRedirectUrl("http://www.lojamodelo.com.br");

        // Add checkout metadata information
        $paymentRequest->addMetadata('PASSENGER_CPF', '15600944276', 1);
        $paymentRequest->addMetadata('GAME_NAME', 'DOTA');
        $paymentRequest->addMetadata('PASSENGER_PASSPORT', '23456', 1);

        // Another way to set checkout parameters
        $paymentRequest->addParameter('notificationURL', 'http://www.lojamodelo.com.br/nas');
        $paymentRequest->addParameter('senderBornDate', '07/05/1981');
        $paymentRequest->addIndexedParameter('itemId', '0003', 3);
        $paymentRequest->addIndexedParameter('itemDescription', 'Notebook Preto', 3);
        $paymentRequest->addIndexedParameter('itemQuantity', '1', 3);
        $paymentRequest->addIndexedParameter('itemAmount', '200.00', 3);

        try {

            /*
             * #### Credentials #####
             * Replace the parameters below with your credentials
             * You can also get your credentials from a config file. See an example:
             * $credentials = PagSeguroConfig::getAccountCredentials();
             */

            // seller authentication
            $credentials = new PagSeguroAccountCredentials("[email protected]",
                "EDC2E4A8E6B246D29C713FB97C26808E");

            // application authentication
            //$credentials = PagSeguroConfig::getApplicationCredentials();

            //$credentials->setAuthorizationCode("E231B2C9BCC8474DA2E260B6C8CF60D3");

            // Register this payment request in PagSeguro to obtain the checkout code
            $onlyCheckoutCode = true;
            $code = $paymentRequest->register($credentials, $onlyCheckoutCode);

            self::printPaymentUrl($code);
        } catch (PagSeguroServiceException $e) {
            die($e->getMessage());
        }
      }

      public static function printPaymentUrl($code)
      {
        if ($code) {
            echo "<h2>Criando requisi&ccedil;&atilde;o de pagamento</h2>";
            echo "<p>Code: <strong>$code</strong></p>";
            echo "<script>
            PagSeguroLightbox('".$code."');
                  </script>";

        }
      }
     }
}
CreatePaymentRequestLightbox::main();
  • 1

    Which error is being returned?

  • You passed these variables as a parameter in the function?

  • Error [HTTP 400] - BAD_REQUEST 0 [11025] - Item Id is required.

3 answers

3


I do not know if you have put enough code and information to indicate clearly where the problem is but surely there is something that will help or solve the problem. Probably just transfer the declaration of variables that are "loose" on the page and put inside the method where you need them, in case the main class CreatePaymentRequestLightbox. I have my doubts if you need this class. You are probably copying a code that you do not understand.

<?php
require_once "includes/ps/PagSeguroLibrary/PagSeguroLibrary.php";
class CreatePaymentRequestLightbox {

public static function main() {
    $form = $_GET['form'];
    $id_transfer = $_POST["id_transfer"];
    $id = $_POST["id_saida"];
    $nome = $_POST["nome_produto"];
    $valor   = $_POST["valor_final"];
    $v1 ="teste";

    $paymentRequest = new PagSeguroPaymentRequest();
    $paymentRequest->setCurrency("BRL");
    $paymentRequest->addItem($v1, 'Notebook prata', 1, 430.00);
    $paymentRequest->setReference("REF123");
    $sedexCode = PagSeguroShippingType::getCodeByType('SEDEX');
    $paymentRequest->setShippingType($sedexCode);
    $paymentRequest->setShippingAddress(
        '01452002',
        'Av. Brig. Faria Lima',
        '1384',
        'apto. 114',
        'Jardim Paulistano',
        'São Paulo',
        'SP',
        'BRA'
    );
    $paymentRequest->setSender(
        'João Comprador',
        '[email protected]',
        '11',
        '56273440',
        'CPF',
        '156.009.442-76'
    );
    $paymentRequest->setRedirectUrl("http://www.lojamodelo.com.br");
    $paymentRequest->addMetadata('PASSENGER_CPF', '15600944276', 1);
    $paymentRequest->addMetadata('GAME_NAME', 'DOTA');
    $paymentRequest->addMetadata('PASSENGER_PASSPORT', '23456', 1);
    $paymentRequest->addParameter('notificationURL', 'http://www.lojamodelo.com.br/nas');
    $paymentRequest->addParameter('senderBornDate', '07/05/1981');
    $paymentRequest->addIndexedParameter('itemId', '0003', 3);
    $paymentRequest->addIndexedParameter('itemDescription', 'Notebook Preto', 3);
    $paymentRequest->addIndexedParameter('itemQuantity', '1', 3);
    $paymentRequest->addIndexedParameter('itemAmount', '200.00', 3);
    try {
        $credentials = new PagSeguroAccountCredentials("[email protected]",
            "EDC2E4A8E6B246D29C713FB97C26808E");
        $onlyCheckoutCode = true;
        $code = $paymentRequest->register($credentials, $onlyCheckoutCode);
        self::printPaymentUrl($code);
    } catch (PagSeguroServiceException $e) {
        die($e->getMessage());
    }
  }

  public static function printPaymentUrl($code) {
    if ($code) {
        echo "<h2>Criando requisi&ccedil;&atilde;o de pagamento</h2>";
        echo "<p>Code: <strong>$code</strong></p>";
        echo "<script>
        PagSeguroLightbox('".$code."');
              </script>";
    }
  }
 }
}
CreatePaymentRequestLightbox::main();

I put in the Github for future reference.

1

The variables you are trying to use in the object method $paymentRequest shall be initialised within the main (main), so they will be recognized in the scope.

0

For you to use these parameters throughout the application (or in more than one class), the best, in my opinion, is to create a constant. Example:

define("ID", $_POST["id_saida"]);
define("NOME", $_POST["nome_produto"]);
define("VALOR", $_POST["valor_final"])  ;

class CreatePaymentRequestLightbox
{
    public static function main()
    {
        $id = ID;
        $nome = NOME;
        $valor   = VALOR;
    }
}

Browser other questions tagged

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