forked from LiveCarta/PayPal-PHP-SDK
Add samples for Invoice Template API
This commit is contained in:
72
sample/invoice-templates/CreateInvoiceTemplate.php
Normal file
72
sample/invoice-templates/CreateInvoiceTemplate.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
// # Create Invoice Template Sample
|
||||
// This sample code demonstrate how you can create
|
||||
// an invoice template.
|
||||
|
||||
use PayPal\Api\Currency;
|
||||
use PayPal\Api\InvoiceItem;
|
||||
use PayPal\Api\MerchantInfo;
|
||||
use PayPal\Api\Template;
|
||||
use PayPal\Api\TemplateData;
|
||||
use PayPal\Api\TemplateSettings;
|
||||
use PayPal\Api\TemplateSettingsMetadata;
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
// ### Invoice Template Item
|
||||
$invoiceTemplateDataItem = new InvoiceItem();
|
||||
$invoiceTemplateDataItem
|
||||
->setName("Nutri Bullet")
|
||||
->setQuantity(1)
|
||||
->setUnitPrice(new Currency('{ "currency": "USD", "value": "50.00" }'));
|
||||
|
||||
// ### Invoice Template Data
|
||||
$invoiceTemplateData = new TemplateData();
|
||||
$invoiceTemplateData
|
||||
->setTaxCalculatedAfterDiscount(false)
|
||||
->setTaxInclusive(false)
|
||||
->setNote("Thank you for your business")
|
||||
->setLogoUrl("https://pics.paypal.com/v1/images/redDot.jpeg")
|
||||
->addItem($invoiceTemplateDataItem)
|
||||
->setMerchantInfo(new MerchantInfo('{ "email": "jaypatel512-facilitator@hotmail.com" }'));
|
||||
|
||||
// ### Template Settings
|
||||
$displayPreferences = new TemplateSettingsMetadata();
|
||||
$displayPreferences->setHidden(true);
|
||||
|
||||
$settingDate = new TemplateSettings();
|
||||
$settingDate
|
||||
->setFieldName("items.date")
|
||||
->setDisplayPreference($displayPreferences);
|
||||
|
||||
|
||||
// ### Template
|
||||
$invoiceTemplate = new Template();
|
||||
$invoiceTemplate
|
||||
->setName("Hours Template" . rand())
|
||||
->setDefault(true)
|
||||
->setUnitOfMeasure("HOURS")
|
||||
->setTemplateData($invoiceTemplateData)
|
||||
// This is another way of initializing the object.
|
||||
->addSetting(new TemplateSettings('{ "field_name": "custom", "display_preference": { "hidden": true } }'))
|
||||
->addSetting($settingDate);
|
||||
|
||||
// For Sample Purposes Only.
|
||||
$request = clone $invoiceTemplate;
|
||||
|
||||
try {
|
||||
// ### Create Invoice Template
|
||||
// Create an invoice by calling the invoice->create() method
|
||||
// with a valid ApiContext (See bootstrap.php for more on `ApiContext`)
|
||||
$invoiceTemplate->create($apiContext);
|
||||
} catch (Exception $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Create Invoice Template", "Template", null, $request, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Create Invoice Template", "Template", $invoiceTemplate->getTemplateId(), $request, $invoiceTemplate);
|
||||
|
||||
return $invoiceTemplate;
|
||||
28
sample/invoice-templates/DeleteInvoiceTemplate.php
Normal file
28
sample/invoice-templates/DeleteInvoiceTemplate.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
// # Delete Invoice Template Sample
|
||||
// This sample code demonstrate how you can delete
|
||||
// an invoice template
|
||||
|
||||
/** @var Template $template */
|
||||
$template = require 'CreateInvoiceTemplate.php';
|
||||
|
||||
use PayPal\Api\Template;
|
||||
|
||||
try {
|
||||
|
||||
// ### Delete Invoice Template
|
||||
// Delete invoice object by calling the
|
||||
// `delete` method
|
||||
// on the Invoice Template class by passing a valid
|
||||
// notification object
|
||||
// (See bootstrap.php for more on `ApiContext`)
|
||||
$deleteStatus = $template->delete($apiContext);
|
||||
} catch (Exception $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Delete Invoice Template", "Template", null, $deleteStatus, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Delete Invoice Template", "Template", $template->getTemplateId(), null, null);
|
||||
18
sample/invoice-templates/GetAllInvoiceTemplates.php
Normal file
18
sample/invoice-templates/GetAllInvoiceTemplates.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use PayPal\Api\Templates;
|
||||
|
||||
require 'CreateInvoiceTemplate.php';
|
||||
|
||||
try {
|
||||
$templates = Templates::getAll(array("fields" => "all"), $apiContext);
|
||||
} catch (Exception $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Get all Templates", "Templates", null, null, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Get all Templates", "Templates", null, null, $templates);
|
||||
|
||||
return $templates;
|
||||
31
sample/invoice-templates/GetInvoiceTemplate.php
Normal file
31
sample/invoice-templates/GetInvoiceTemplate.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
// # Retrieve Invoice Template Sample
|
||||
// This sample code demonstrate how you can get
|
||||
// an invoice template using templateId.
|
||||
|
||||
use PayPal\Api\Template;
|
||||
|
||||
$invoiceTemplate = require 'CreateInvoiceTemplate.php';
|
||||
|
||||
/** @var Template $invoiceTemplate */
|
||||
$templateId = $invoiceTemplate->getTemplateId();
|
||||
|
||||
// ### Retrieve Invoice Template
|
||||
// Retrieve the invoice template object by calling the
|
||||
// static `get` method
|
||||
// on the Template class by passing a valid
|
||||
// Template ID
|
||||
// (See bootstrap.php for more on `ApiContext`)
|
||||
try {
|
||||
$template = Template::get($templateId, $apiContext);
|
||||
} catch (Exception $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Get Invoice Template", "Template", $template->getTemplateId(), $templateId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Get Invoice Template", "Template", $template->getTemplateId(), $templateId, $template);
|
||||
|
||||
return $template;
|
||||
33
sample/invoice-templates/UpdateInvoiceTemplate.php
Normal file
33
sample/invoice-templates/UpdateInvoiceTemplate.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
// # Update Invoice Sample
|
||||
// This sample code demonstrate how you can update
|
||||
// an invoice.
|
||||
|
||||
/** @var Template $template */
|
||||
$template = require 'GetInvoiceTemplate.php';
|
||||
use PayPal\Api\Template;
|
||||
|
||||
|
||||
// ### Update Invoice
|
||||
$template->setUnitOfMeasure("QUANTITY");
|
||||
|
||||
// ### NOTE: These are the work-around added to the
|
||||
// sample, to get past the bug in PayPal APIs.
|
||||
$template->setCustom(null);
|
||||
|
||||
// For Sample Purposes Only.
|
||||
$request = clone $template;
|
||||
try {
|
||||
// ### Update Invoice Template
|
||||
// Update an invoice by calling the invoice->update() method
|
||||
// with a valid ApiContext (See bootstrap.php for more on `ApiContext`)
|
||||
$template->update($apiContext);
|
||||
} catch (Exception $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Invoice Template Updated", "Invoice", null, $request, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Invoice Template Updated", "Invoice", $template->getTemplateId(), $request, $template);
|
||||
Reference in New Issue
Block a user