function getInvoiceDetails($invoiceNumber, $apiContext, $paypal) { try { $invoice = Invoice::get($invoiceNumber, $apiContext); } catch (Exception $ex) { ResultPrinter::printError("Get Invoice", "Invoice", $invoice->getId(), $invoiceId, $ex); exit(1); } $taxSum = 0; foreach ($invoice->getItems() as $item) { if ($item->getTax()) { $taxSum += $item->getTax()->getAmount()->getValue(); } } $tID = 0; //This is the key value to hook up with the classis paypal API $tID = $paypal->request($invoice->getPayments()['0']->getTransactionId())['L_TRANSACTIONID0']; return array('date' => $invoice->getPayments()['0']->getDate(), 'memo' => $invoice->getMerchantMemo(), 'number' => $invoice->getNumber(), 'tax' => $taxSum, 'tID' => $tID); }
<?php require __DIR__ . '/../bootstrap.php'; use PayPal\Api\Invoice; try { $invoice = Invoice::get("INV2-9DRB-YTHU-2V9Q-7Q24", $apiContext); $sendStatus = $invoice->send($apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); exit(1); } ?> <html> <head> <title>Send Invoice</title> </head> <body> <div>Send Invoice:</div> <pre><?php var_dump($invoice); ?> </pre> <a href='../index.html'>Back</a> </body> </html>
<?php // # Get Invoice Sample // This sample code demonstrate how you can retrieve // an invoice. /** @var Invoice $invoice */ $invoice = (require 'CreateInvoice.php'); use PayPal\Api\Invoice; $invoiceId = $invoice->getId(); // ### Retrieve Invoice // Retrieve the invoice object by calling the // static `get` method // on the Invoice class by passing a valid // Invoice ID // (See bootstrap.php for more on `ApiContext`) try { $invoice = Invoice::get($invoiceId, $apiContext); } catch (Exception $ex) { ResultPrinter::printError("Get Invoice", "Invoice", $invoice->getId(), $invoiceId, $ex); exit(1); } ResultPrinter::printResult("Get Invoice", "Invoice", $invoice->getId(), $invoiceId, $invoice); return $invoice;
<?php // # Search Invoices Sample // This sample code demonstrate how you can // search invoices from history. /** @var Invoice $invoice */ $invoice = (require 'CreateInvoice.php'); use PayPal\Api\Invoice; use PayPal\Api\Search; try { // ### Search Object // Fill up your search criteria for Invoice search. // Using the new way to inject raw json string to constructor $search = new Search('{ "start_invoice_date" : "2010-05-10 PST", "end_invoice_date" : "2019-05-11 PST", "page" : 1, "page_size" : 20, "total_count_required" : true }'); // ### Search Invoices // Retrieve the Invoice History object by calling the // static `search` method on the Invoice class. // Refer the method doc for valid values for keys // (See bootstrap.php for more on `ApiContext`) $invoices = Invoice::search($search, $apiContext); } catch (Exception $ex) { ResultPrinter::printError("Search Invoice", "Invoice", null, null, $ex); exit(1); } ResultPrinter::printResult("Search Invoice", "Invoice", null, $search, $invoices);
<?php // # Get Next Invoice Number Sample // This sample code demonstrate how you can retrieve // the next invoice number. require '../bootstrap.php'; use PayPal\Api\Invoice; // ### Get Next Invoice Number // To generate the successive invoice number for the merchant, use below code. // (See bootstrap.php for more on `ApiContext`) try { $number = Invoice::generateNumber($apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Get Next Invoice Number", "InvoiceNumber", null, null, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Get Next Invoice Number", "InvoiceNumber", null, $number, $number); return $number;
// # Create Invoice Sample // This sample code demonstrate how you can create // an invoice. require __DIR__ . '/../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; $invoice = new Invoice(); // ### Invoice Info // Fill in all the information that is // required for invoice APIs $invoice->setMerchantInfo(new MerchantInfo())->setBillingInfo(array(new BillingInfo()))->setNote("Medical Invoice 16 Jul, 2013 PST")->setPaymentTerm(new PaymentTerm())->setShippingInfo(new ShippingInfo()); // ### Merchant Info // A resource representing merchant information that can be // used to identify merchant $invoice->getMerchantInfo()->setEmail("*****@*****.**")->setFirstName("Dennis")->setLastName("Doctor")->setbusinessName("Medical Professionals, LLC")->setPhone(new Phone())->setAddress(new Address()); $invoice->getMerchantInfo()->getPhone()->setCountryCode("001")->setNationalNumber("5032141716"); // ### Address Information // The address used for creating the invoice $invoice->getMerchantInfo()->getAddress()->setLine1("1234 Main St.")->setCity("Portland")->setState("OR")->setPostalCode("97217")->setCountryCode("US"); // ### Billing Information // Set the email address for each billing $billing = $invoice->getBillingInfo();
public static function get($invoiceId, $apiContext = null) { if ($invoiceId == null || strlen($invoiceId) <= 0) { throw new \InvalidArgumentException("invoiceId cannot be null or empty"); } $payLoad = ""; if ($apiContext == null) { $apiContext = new ApiContext(self::$credential); } $call = new PPRestCall($apiContext); $json = $call->execute(array('PayPal\\Rest\\RestHandler'), "/v1/invoicing/invoices/{$invoiceId}", "GET", $payLoad); $ret = new Invoice(); $ret->fromJson($json); return $ret; }
<?php // # List Invoices Sample // This sample code demonstrate how you can get // all invoice from history. /** @var Invoice $invoice */ $invoice = (require 'CreateInvoice.php'); use PayPal\Api\Invoice; try { // ### Retrieve Invoices // Retrieve the Invoice History object by calling the // static `get_all` method on the Invoice class. // Refer the method doc for valid values for keys // (See bootstrap.php for more on `ApiContext`) $invoices = Invoice::getAll(array('page' => 0, 'page_size' => 4, 'total_count_required' => "true"), $apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Lookup Invoice History", "Invoice", null, null, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Lookup Invoice History", "Invoice", null, null, $invoices);
<?php require __DIR__ . '/../bootstrap.php'; use PayPal\Api\Invoice; use PayPal\Api\Notification; try { $invoice = Invoice::get("INV2-9CAH-K5G7-2JPL-G4B4", $apiContext); $notify = new Notification(); $notify->setSubject("Past due")->setNote("Please pay soon")->setSendToMerchant(true); $remindStatus = $invoice->remind($notify, $apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); exit(1); } ?> <html> <head> <title>Remind Invoice</title> </head> <body> <div>Remind Invoice:</div> <pre><?php var_dump($invoice); ?> </pre> <a href='../index.html'>Back</a> </body> </html>
<?php require __DIR__ . '/../bootstrap.php'; use PayPal\Api\Invoice; try { $invoices = Invoice::get_all($apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception:" . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); exit(1); } ?> <html> <head> <title>Lookup invoice history</title> </head> <body> <div>Got invoices </div> <pre><?php var_dump($invoices->toArray()); ?> </pre> <a href='../index.html'>Back</a> </body> </html>
public function createInvoice($params = null) { if (!$params) { return false; } $invoice = new Invoice(); // ### Invoice Info // Fill in all the information that is // required for invoice APIs $invoice->setMerchantInfo(new MerchantInfo())->setBillingInfo(array(new BillingInfo())); // ### Merchant Info // A resource representing merchant information that can be // used to identify merchant $owner_email = $this->_credentials['business_owner']; $invoice->getMerchantInfo()->setEmail($owner_email); // ### Billing Information // Set the email address for each billing $billing = $invoice->getBillingInfo(); $billing[0]->setEmail($params['email']); $items = []; foreach ($params['items'] as $key => $item) { # code... $items[$key] = new InvoiceItem(); $items[$key]->setName($item['name'])->setQuantity($item['quantity'])->setUnitPrice(new Currency()); $items[$key]->getUnitPrice()->setCurrency($params['currency'])->setValue((double) $item['price']); } // ### Items List $invoice->setItems($items); $request = clone $invoice; try { $invoice->create($this->config); } catch (\Exception $ex) { return $ex; } return $invoice; }
<?php require __DIR__ . '/../bootstrap.php'; use PayPal\Api\Invoice; use PayPal\Api\CancelNotification; try { $invoice = Invoice::get("INV2-CJL7-PF4G-BLQF-5FWG", $apiContext); $notify = new CancelNotification(); $notify->setSubject("Past due")->setNote("Canceling invoice")->setSendToMerchant(true)->setSendToPayer(true); $cancelStatus = $invoice->cancel($notify, $apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); exit(1); } ?> <html> <head> <title>Cancel Invoice</title> </head> <body> <div>Cancel Invoice:</div> <pre><?php var_dump($invoice); ?> </pre> <a href='../index.html'>Back</a> </body> </html>
/** * @dataProvider mockProvider * @param Invoice $obj */ public function testGenerateNumber($obj, $mockApiContext) { $mockPPRestCall = $this->getMockBuilder('\\PayPal\\Transport\\PayPalRestCall')->disableOriginalConstructor()->getMock(); $mockPPRestCall->expects($this->any())->method('execute')->will($this->returnValue(InvoiceNumberTest::getJson())); $result = $obj->generateNumber($mockApiContext, $mockPPRestCall); $this->assertNotNull($result); }
/** * Gets the details for a specified invoice, by ID. * * @param string $invoiceId * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials. * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls * @return Invoice */ public static function get($invoiceId, $apiContext = null, $restCall = null) { ArgumentValidator::validate($invoiceId, 'invoiceId'); $payLoad = ""; $json = self::executeCall("/v1/invoicing/invoices/{$invoiceId}", "GET", $payLoad, null, $apiContext, $restCall); $ret = new Invoice(); $ret->fromJson($json); return $ret; }
/** * @depends testSend * @param $invoice Invoice * @return Invoice */ public function testQRCode($invoice) { $result = Invoice::qrCode($invoice->getId(), array(), null, $this->mockPayPalRestCall); $this->assertNotNull($result); $this->assertNotNull($result->getImage()); }
<?php // # Retrieve QR Code for Invoice Sample // Specify an invoice ID to get a QR code (image) that corresponds to the invoice ID. A QR code for an invoice can be added to a paper or PDF invoice. When a customer uses their mobile device to scan the QR code, the customer is redirected to the PayPal mobile payment flow, where they can pay online with PayPal or a credit card. /** @var Invoice $invoice */ $invoice = (require 'SendInvoice.php'); use PayPal\Api\Invoice; try { // ### Retrieve QR Code of Sent Invoice // Retrieve QR Code of Sent Invoice by calling the // `qrCode` method // on the Invoice class by passing a valid // notification object // (See bootstrap.php for more on `ApiContext`) $image = Invoice::qrCode($invoice->getId(), array('height' => '300', 'width' => '300'), $apiContext); // ### Optionally Save to File // This is not a required step. However, if you want to store this image as a file, you can use // 'saveToFile' method with proper file name. // This will save the image as /samples/invoice/images/sample.png $path = $image->saveToFile("images/sample.png"); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Retrieved QR Code for Invoice", "Invoice", $invoice->getId(), null, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Retrieved QR Code for Invoice", "Invoice", $invoice->getId(), null, $image); // ### Show the Image // In PHP, there are many ways to present an images. // One of the ways, you could directly inject the base64-encoded string // with proper image information in front of it.
/** * @dataProvider mockProvider * @param Invoice $obj */ public function testQrCode($obj, $mockApiContext) { $mockPayPalRestCall = $this->getMockBuilder('\\PayPal\\Transport\\PayPalRestCall')->disableOriginalConstructor()->getMock(); $mockPayPalRestCall->expects($this->any())->method('execute')->will($this->returnValue(ImageTest::getJson())); $result = $obj->qrCode("invoiceId", array(), $mockApiContext, $mockPayPalRestCall); $this->assertNotNull($result); }
$billingInfo = $invoice->getBillingInfo()[0]; $billingInfo->setAddress(null); $invoice->getPaymentTerm()->setDueDate(null); try { // ### Update Invoice // Update an invoice by calling the invoice->update() method // with a valid ApiContext (See bootstrap.php for more on `ApiContext`) $invoice->update($apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Invoice Updated", "Invoice", null, $request, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Invoice Updated", "Invoice", $invoice->getId(), $request, $invoice); // ### Retrieve Invoice // Retrieve the invoice object by calling the // static `get` method // on the Invoice class by passing a valid // Invoice ID // (See bootstrap.php for more on `ApiContext`) try { $invoice = Invoice::get($invoice->getId(), $apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Get Invoice (Not Required - For Sample Only)", "Invoice", $invoice->getId(), $invoice->getId(), $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Get Invoice (Not Required - For Sample Only)", "Invoice", $invoice->getId(), $invoice->getId(), $invoice); return $invoice;
<?php require __DIR__ . '/../bootstrap.php'; use PayPal\Api\Invoice; use PayPal\Api\MerchantInfo; use PayPal\Api\BillingInfo; use PayPal\Api\InvoiceItem; use PayPal\Api\Phone; use PayPal\Api\Address; use PayPal\Api\Currency; use PayPal\Api\PaymentTerm; use PayPal\Api\ShippingInfo; $invoice = new Invoice(); $invoice->setMerchantInfo(new MerchantInfo())->setBillingInfo(array(new BillingInfo()))->setItems(array(new InvoiceItem()))->setNote("Medical Invoice 16 Jul, 2013 PST")->setPaymentTerm(new PaymentTerm())->setShippingInfo(new ShippingInfo()); $invoice->getMerchantInfo()->setEmail("*****@*****.**")->setFirstName("Dennis")->setLastName("Doctor")->setbusinessName("Medical Professionals, LLC")->setPhone(new Phone())->setAddress(new Address()); $invoice->getMerchantInfo()->getPhone()->setCountryCode("001")->setNationalNumber("5032141716"); $invoice->getMerchantInfo()->getAddress()->setLine1("1234 Main St.")->setCity("Portland")->setState("OR")->setPostalCode("97217")->setCountryCode("US"); $billing = $invoice->getBillingInfo(); $billing[0]->setEmail("*****@*****.**"); $items = $invoice->getItems(); $items[0]->setName("Sutures")->setQuantity(100)->setUnitPrice(new Currency()); $items[0]->getUnitPrice()->setCurrency("USD")->setValue(5); $invoice->getPaymentTerm()->setTermType("NET_45"); $invoice->getShippingInfo()->setFirstName("Sally")->setLastName("Patient")->setBusinessName("Not applicable")->setPhone(new Phone())->setAddress(new Address()); $invoice->getShippingInfo()->getPhone()->setCountryCode("001")->setNationalNumber("5039871234"); $invoice->getShippingInfo()->getAddress()->setLine1("1234 Main St.")->setCity("Portland")->setState("OR")->setPostalCode("97217")->setCountryCode("US"); print var_dump($invoice->toArray()); try { $invoice->create($apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL;