Update status invoice sdk

Funky89
Contributor
Contributor

Hello everyone,

 

I want to update or change my status to my invoice

 

I follow this step:

 

For create my invoice : https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/invoice/CreateInvoice.php

For update my invoice : https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/invoice/UpdateInvoice.php

 

But my status is always in DRAFT

I don't know where is my mistake, can you help me

This is my code

 

<?php
include('../../includes/head.php');	

include('./bootstrap.php');
use PayPal\Api\Address;
use PayPal\Api\BillingInfo;
use PayPal\Api\Cost;
use PayPal\Api\Currency;
use PayPal\Api\Invoice;
use PayPal\Api\InvoiceAddress;
use PayPal\Api\InvoiceItem;
use PayPal\Api\MerchantInfo;
use PayPal\Api\PaymentTerm;
use PayPal\Api\Phone;
use PayPal\Api\ShippingInfo;

include_once('../../includes/class/class_commerce.php');
$commerce = new Commerce();

$invoice = new Invoice();

include_once('../../includes/class/class_bdd.php');

class InfoClient extends BDD {

    public function getInfo($idTransaction)
    {
        $data = array('ID_Transaction' => $idTransaction);

        $result = $this->sendJsonToNodeJS('Get_Info_Customer', null, $data);
            
        if( !isset($result->Erreur) || $result->Erreur == 0 ){
                return $result;
        }else{

            return false;
        }
    }
}

$bdd = new InfoClient();


$idTransaction = "I-G6YMC7KHFB4N" ;
$detail = $bdd->getInfo($idTransaction) ;
$jsonPayment = $detail->resource;

// ### Invoice Info
// Fill in all the information that is
// required for invoice APIs
$invoice
    ->setMerchantInfo(new MerchantInfo())
    ->setBillingInfo(array(new BillingInfo()))
    ->setNote("") // Note en bas de la facture
    ->setPaymentTerm(new PaymentTerm())
    ->setShippingInfo(new ShippingInfo());

// ### Merchant Info
// A resource representing merchant information that can be
// used to identify merchant
$invoice->getMerchantInfo()
    ->setEmail("myAdressemail") // Modifier l'adresse par le compte qui à paypal
    ->setFirstName("") // Définire qqch ?
    ->setLastName("") // Définire qqch ?
    ->setbusinessName("*******")
    ->setPhone(new Phone())
    ->setAddress(new Address()); // Modifier adresse

$invoice->getMerchantInfo()->getPhone()
    ->setCountryCode("001") // ???
    ->setNationalNumber[Removed. Phone #s not permitted]); // ???

// ### Address Information
// The address used for creating the invoice
$invoice->getMerchantInfo()->getAddress()
    ->setLine1("test")
    ->setCity("test")
    ->setState("")
    ->setPostalCode("71100")
    ->setCountryCode("FR");

// ### Billing Information
// Set the email address for each billing
$billing = $invoice->getBillingInfo();
$billing[0]
    ->setEmail($jsonPayment->payer->payer_info->email);

$billing[0]->setBusinessName($jsonPayment->payer->payer_info->shipping_address->recipient_name) // Info qui à sur la page de paypal à modifier mais mettre quoi ?
    ->setAdditionalInfo("") // Info qui à sur la page de paypal facture à modifier mais mettre quoi ?
    ->setAddress(new InvoiceAddress());

$billing[0]->getAddress()
    ->setLine1($jsonPayment->payer->payer_info->shipping_address->line1)
    ->setCity($jsonPayment->payer->payer_info->shipping_address->city)
    ->setState($jsonPayment->payer->payer_info->shipping_address->state)
    ->setPostalCode($jsonPayment->payer->payer_info->shipping_address->postal_code)
    ->setCountryCode($jsonPayment->payer->payer_info->shipping_address->country_code);

// ### Items List
// You could provide the list of all items for
// detailed breakdown of invoice
$items = array();
$items[0] = new InvoiceItem();
$items[0]
    ->setName($jsonPayment->description)
    ->setQuantity(1) // TODO RECUPERER la quantité !!!!!!
    ->setUnitPrice(new Currency());

$items[0]->getUnitPrice()
    ->setCurrency($jsonPayment->plan->curr_code)
    ->setValue($jsonPayment->plan->payment_definitions[0]->amount->value);

// #### Tax Item
// You could provide Tax information to each item.
$tax = new \PayPal\Api\Tax();
$tax->setPercent($commerce->TVA[$_SESSION['CPays']]['TVA'])->setName("TAXE 20 %"); // TODO RECUPERER LA VALEUR DE LA TAX
$items[0]->setTax($tax);

// echo $jsonPayment->plan->payment_definitions[0]->charge_models[0]->amount->value;
// Si achate de plusieurs éléments prendre exemple du code en bas

// // Second Item
// $items[1] = new InvoiceItem();
// // Lets add some discount to this item.
// $item1discount = new Cost();
// $item1discount->setPercent("3");
// $items[1]
//     ->setName("Injection")
//     ->setQuantity(5)
//     ->setDiscount($item1discount)
//     ->setUnitPrice(new Currency());

// $items[1]->getUnitPrice()
//     ->setCurrency("USD")
//     ->setValue(5);

// // #### Tax Item
// // You could provide Tax information to each item.
// $tax2 = new \PayPal\Api\Tax();
// $tax2->setPercent(3)->setName("Local Tax on Injection");
// $items[1]->setTax($tax2);

$invoice->setItems($items);


// Si on veut mettre une remise 
// #### Final Discount
// You can add final discount to the invoice as shown below. You could either use "percent" or "value" when providing the discount
// $cost = new Cost();
// $cost->setPercent("0");
// $invoice->setDiscount($cost);

$invoice->getPaymentTerm()
    ->setTermType("NET_45"); //The terms by which the invoice payment is due. Valid Values: ["DUE_ON_RECEIPT", "DUE_ON_DATE_SPECIFIED", "NET_10", "NET_15", "NET_30", "NET_45", "NET_60", "NET_90", "NO_DUE_DATE"]



// ### Shipping Information
$invoice->getShippingInfo()
    ->setFirstName($jsonPayment->payer->payer_info->first_name)
    ->setLastName($jsonPayment->payer->payer_info->last_name)
    ->setBusinessName("") // ??? par défaut c'était "Not applicable"
    ->setPhone(new Phone())
    ->setAddress(new InvoiceAddress());

$invoice->getShippingInfo()->getPhone()
    ->setCountryCode("001") // Mettre le +33 ??
    ->setNationalNumber("12"); // Mettre le num du client ??

$invoice->getShippingInfo()->getAddress()
    ->setLine1($jsonPayment->payer->payer_info->shipping_address->line1)
    ->setCity($jsonPayment->payer->payer_info->shipping_address->city)
    ->setState($jsonPayment->payer->payer_info->shipping_address->state)
    ->setPostalCode($jsonPayment->payer->payer_info->shipping_address->postal_code)
    ->setCountryCode($jsonPayment->payer->payer_info->shipping_address->country_code);

// ### Logo
// You can set the logo in the invoice by providing the external URL pointing to a logo
// $invoice->setLogoUrl('https://www.paypalobjects.com/webstatic/i/logo/rebrand/ppcom.svg');

// For Sample Purposes Only.
//$invoice->setStatus('PAID'); //Status of the invoice. Valid Values: ["DRAFT", "SENT", "PAID", "MARKED_AS_PAID", "CANCELLED", "REFUNDED", "PARTIALLY_REFUNDED", "MARKED_AS_REFUNDED", "UNPAID", "PAYMENT_PENDING"]
$request = clone $invoice;

try {

    $invoice1 = $invoice->create($apiContext);

    try {
        $invoice1->setStatus('PAID');
            
        $result = $invoice1->update($apiContext);
        print_r( $result->getStatus());
        try {
            $invoiceGood = Invoice::get($invoice1->getId(), $apiContext);
        } catch (Exception $ex) {

            exit(1);
        }

    } catch (Exception $ex) {
    
        exit(1);
    }




} catch (Exception $ex) {
    print_r($ex);
    echo "ERREUR FACTURE";

    exit(1);
}

 

Login to Me Too
1 ACCEPTED SOLUTION
3 REPLIES 3

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.