Add samples for Invoice Template API

This commit is contained in:
Jay Patel
2016-09-19 16:22:06 -05:00
parent 4f20bc4b97
commit f0feddc8f2
8 changed files with 336 additions and 117 deletions

View File

@@ -225,6 +225,7 @@ if (PHP_SAPI == 'cli') {
<li><a href="#experience">Payment Experience</a></li>
<li><a href="#notifications">Notifications</a></li>
<li><a href="#invoice">Invoice</a></li>
<li><a href="#invoice-templates">Invoice Templates</a></li>
<li><a href="#identity">Identity (LIPP)</a></li>
</ul>
@@ -1343,6 +1344,73 @@ if (PHP_SAPI == 'cli') {
</ul>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 id="invoice-templates" class="panel-title"><a
href="https://developer.paypal.com/docs/api/invoicing/#templates"
target="_blank">Invoice Templates</a></h3>
</div>
<!-- List group -->
<ul class="list-group">
<li class="list-group-item">
<div class="row">
<div class="col-md-8"><h5>Create Invoice Template</h5></div>
<div class="col-md-4">
<a href="invoice-templates/CreateInvoiceTemplate.php" class="btn btn-primary pull-left execute"> Try It <i
class="fa fa-play-circle-o"></i></a>
<a href="doc/invoice/GetNextInvoiceNumber.html" class="btn btn-default pull-right">Source <i
class="fa fa-file-code-o"></i></a>
</div>
</div>
</li>
<li class="list-group-item">
<div class="row">
<div class="col-md-8"><h5>Get Invoice Template</h5></div>
<div class="col-md-4">
<a href="invoice-templates/GetInvoiceTemplate.php" class="btn btn-primary pull-left execute"> Try It <i
class="fa fa-play-circle-o"></i></a>
<a href="doc/invoice/CreateInvoice.html" class="btn btn-default pull-right">Source <i
class="fa fa-file-code-o"></i></a>
</div>
</div>
</li>
<li class="list-group-item">
<div class="row">
<div class="col-md-8"><h5>Update Invoice Template</h5></div>
<div class="col-md-4">
<a href="invoice-templates/UpdateInvoiceTemplate.php" class="btn btn-primary pull-left execute"> Try It <i
class="fa fa-play-circle-o"></i></a>
<a href="doc/invoice/CreateThirdPartyInvoice.html" class="btn btn-default pull-right">Source <i
class="fa fa-file-code-o"></i></a>
</div>
</div>
</li>
<li class="list-group-item">
<div class="row">
<div class="col-md-8"><h5>Delete an Invoice Template</h5></div>
<div class="col-md-4">
<a href="invoice-templates/DeleteInvoiceTemplate.php" class="btn btn-primary pull-left execute"> Try It <i
class="fa fa-play-circle-o"></i></a>
<a href="doc/invoice/SendInvoice.html" class="btn btn-default pull-right">Source <i
class="fa fa-file-code-o"></i></a>
</div>
</div>
</li>
<li class="list-group-item">
<div class="row">
<div class="col-md-8"><h5>Get All Invoice Templates</h5></div>
<div class="col-md-4">
<a href="invoice-templates/GetAllInvoiceTemplates.php" class="btn btn-primary pull-left execute"> Try It <i
class="fa fa-play-circle-o"></i></a>
<a href="doc/invoice/UpdateInvoice.html" class="btn btn-default pull-right">Source <i
class="fa fa-file-code-o"></i></a>
</div>
</div>
</li>
</ul>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 id="identity" class="panel-title"><a

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

View 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);

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

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

View 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);