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

@@ -2,7 +2,10 @@
namespace PayPal\Api;
use PayPal\Common\PayPalModel;
use PayPal\Common\PayPalResourceModel;
use PayPal\Rest\ApiContext;
use PayPal\Transport\PayPalRestCall;
use PayPal\Validation\ArgumentValidator;
/**
* Class Template
@@ -18,9 +21,8 @@ use PayPal\Common\PayPalModel;
* @property \PayPal\Api\TemplateSettings[] settings
* @property string unit_of_measure
* @property bool custom
* @property \PayPal\Api\Links[] links
*/
class Template extends PayPalModel
class Template extends PayPalResourceModel
{
/**
* Unique identifier id of the template.
@@ -214,56 +216,94 @@ class Template extends PayPalModel
}
/**
* Sets Links
* Retrieve the details for a particular template by passing the template ID to the request URI.
*
* @param \PayPal\Api\Links[] $links
*
* @return $this
* @param string $templateId
* @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 Template
*/
public function setLinks($links)
public static function get($templateId, $apiContext = null, $restCall = null)
{
$this->links = $links;
ArgumentValidator::validate($templateId, 'templateId');
$payLoad = "";
$json = self::executeCall(
"/v1/invoicing/templates/$templateId",
"GET",
$payLoad,
null,
$apiContext,
$restCall
);
$ret = new Template();
$ret->fromJson($json);
return $ret;
}
/**
* Delete a particular template by passing the template ID to the request URI.
*
* @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 bool
*/
public function delete($apiContext = null, $restCall = null)
{
ArgumentValidator::validate($this->getTemplateId(), "Id");
$payLoad = "";
self::executeCall(
"/v1/invoicing/templates/{$this->getTemplateId()}",
"DELETE",
$payLoad,
null,
$apiContext,
$restCall
);
return true;
}
/**
* Creates a template.
*
* @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 Template
*/
public function create($apiContext = null, $restCall = null)
{
$json = self::executeCall(
"/v1/invoicing/templates",
"POST",
$this->toJSON(),
null,
$apiContext,
$restCall
);
$this->fromJson($json);
return $this;
}
/**
* Gets Links
* Update an existing template by passing the template ID to the request URI. In addition, pass a complete template object in the request JSON. Partial updates are not supported.
*
* @return \PayPal\Api\Links[]
* @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 Template
*/
public function getLinks()
public function update($apiContext = null, $restCall = null)
{
return $this->links;
}
/**
* Append Links to the list.
*
* @param \PayPal\Api\Links $links
* @return $this
*/
public function addLink($links)
{
if (!$this->getLinks()) {
return $this->setLinks(array($links));
} else {
return $this->setLinks(
array_merge($this->getLinks(), array($links))
);
}
}
/**
* Remove Links from the list.
*
* @param \PayPal\Api\Links $links
* @return $this
*/
public function removeLink($links)
{
return $this->setLinks(
array_diff($this->getLinks(), array($links))
ArgumentValidator::validate($this->getTemplateId(), "Id");
$payLoad = $this->toJSON();
$json = self::executeCall(
"/v1/invoicing/templates/{$this->getTemplateId()}",
"PUT",
$payLoad,
null,
$apiContext,
$restCall
);
$this->fromJson($json);
return $this;
}
}

View File

@@ -3,6 +3,7 @@
namespace PayPal\Api;
use PayPal\Common\PayPalResourceModel;
use PayPal\Transport\PayPalRestCall;
use PayPal\Validation\ArgumentValidator;
use PayPal\Api\Template;
use PayPal\Rest\ApiContext;
@@ -237,6 +238,8 @@ class Templates extends PayPalResourceModel
/**
* Retrieve the details for a particular template by passing the template ID to the request URI.
*
* @deprecated Please use `Template::get()` instead.
* @see Template::get
* @param string $templateId
* @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
@@ -267,7 +270,7 @@ class Templates extends PayPalResourceModel
* @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
* @return Templates
*/
public static function getAll($params, $apiContext = null, $restCall = null)
public static function getAll($params = array(), $apiContext = null, $restCall = null)
{
ArgumentValidator::validate($params, 'params');
$payLoad = "";
@@ -286,78 +289,4 @@ class Templates extends PayPalResourceModel
$ret->fromJson($json);
return $ret;
}
/**
* Delete a particular template by passing the template ID to the request URI.
*
* @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 bool
*/
public function delete($apiContext = null, $restCall = null)
{
ArgumentValidator::validate($this->getId(), "Id");
$payLoad = "";
self::executeCall(
"/v1/invoicing/templates/{$this->getId()}",
"DELETE",
$payLoad,
null,
$apiContext,
$restCall
);
return true;
}
/**
* Creates a template.
*
* @param Template $template
* @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 Template
*/
public function create($template, $apiContext = null, $restCall = null)
{
ArgumentValidator::validate($template, 'template');
$payLoad = $template->toJSON();
$json = self::executeCall(
"/v1/invoicing/templates",
"POST",
$payLoad,
null,
$apiContext,
$restCall
);
$ret = new Template();
$ret->fromJson($json);
return $ret;
}
/**
* Update an existing template by passing the template ID to the request URI. In addition, pass a complete template object in the request JSON. Partial updates are not supported.
*
* @param Template $template
* @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 Template
*/
public function update($template, $apiContext = null, $restCall = null)
{
ArgumentValidator::validate($this->getId(), "Id");
ArgumentValidator::validate($template, 'template');
$payLoad = $template->toJSON();
$json = self::executeCall(
"/v1/invoicing/templates/{$this->getId()}",
"PUT",
$payLoad,
null,
$apiContext,
$restCall
);
$ret = new Template();
$ret->fromJson($json);
return $ret;
}
}

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