From f0feddc8f2de8e837af34442d2c8150134038506 Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Mon, 19 Sep 2016 16:22:06 -0500 Subject: [PATCH] Add samples for Invoice Template API --- lib/PayPal/Api/Template.php | 124 ++++++++++++------ lib/PayPal/Api/Templates.php | 79 +---------- sample/index.php | 68 ++++++++++ .../CreateInvoiceTemplate.php | 72 ++++++++++ .../DeleteInvoiceTemplate.php | 28 ++++ .../GetAllInvoiceTemplates.php | 18 +++ .../invoice-templates/GetInvoiceTemplate.php | 31 +++++ .../UpdateInvoiceTemplate.php | 33 +++++ 8 files changed, 336 insertions(+), 117 deletions(-) create mode 100644 sample/invoice-templates/CreateInvoiceTemplate.php create mode 100644 sample/invoice-templates/DeleteInvoiceTemplate.php create mode 100644 sample/invoice-templates/GetAllInvoiceTemplates.php create mode 100644 sample/invoice-templates/GetInvoiceTemplate.php create mode 100644 sample/invoice-templates/UpdateInvoiceTemplate.php diff --git a/lib/PayPal/Api/Template.php b/lib/PayPal/Api/Template.php index e5b0f79..4602e9b 100644 --- a/lib/PayPal/Api/Template.php +++ b/lib/PayPal/Api/Template.php @@ -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; } } diff --git a/lib/PayPal/Api/Templates.php b/lib/PayPal/Api/Templates.php index 6e36a95..c9b0ad0 100644 --- a/lib/PayPal/Api/Templates.php +++ b/lib/PayPal/Api/Templates.php @@ -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; - } - } diff --git a/sample/index.php b/sample/index.php index daeb2d9..094a9b8 100644 --- a/sample/index.php +++ b/sample/index.php @@ -225,6 +225,7 @@ if (PHP_SAPI == 'cli') {
  • Payment Experience
  • Notifications
  • Invoice
  • +
  • Invoice Templates
  • Identity (LIPP)
  • @@ -1343,6 +1344,73 @@ if (PHP_SAPI == 'cli') { +
    + + + +
    +

    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; diff --git a/sample/invoice-templates/DeleteInvoiceTemplate.php b/sample/invoice-templates/DeleteInvoiceTemplate.php new file mode 100644 index 0000000..99610b6 --- /dev/null +++ b/sample/invoice-templates/DeleteInvoiceTemplate.php @@ -0,0 +1,28 @@ +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); diff --git a/sample/invoice-templates/GetAllInvoiceTemplates.php b/sample/invoice-templates/GetAllInvoiceTemplates.php new file mode 100644 index 0000000..0bef017 --- /dev/null +++ b/sample/invoice-templates/GetAllInvoiceTemplates.php @@ -0,0 +1,18 @@ + "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; \ No newline at end of file diff --git a/sample/invoice-templates/GetInvoiceTemplate.php b/sample/invoice-templates/GetInvoiceTemplate.php new file mode 100644 index 0000000..ba0b6e4 --- /dev/null +++ b/sample/invoice-templates/GetInvoiceTemplate.php @@ -0,0 +1,31 @@ +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; \ No newline at end of file diff --git a/sample/invoice-templates/UpdateInvoiceTemplate.php b/sample/invoice-templates/UpdateInvoiceTemplate.php new file mode 100644 index 0000000..0afb4cb --- /dev/null +++ b/sample/invoice-templates/UpdateInvoiceTemplate.php @@ -0,0 +1,33 @@ +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);