Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Merge b8241a2 into 774daeb
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Coffey committed Sep 29, 2016
2 parents 774daeb + b8241a2 commit c7367ac
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ MANIFEST
.cache
.tox/
.DS_Store
.idea
1 change: 1 addition & 0 deletions paypalrestsdk/__init__.py
Expand Up @@ -3,6 +3,7 @@
from paypalrestsdk.payment_experience import WebProfile
from paypalrestsdk.notifications import Webhook, WebhookEvent, WebhookEventType
from paypalrestsdk.invoices import Invoice
from paypalrestsdk.invoice_templates import InvoiceTemplate
from paypalrestsdk.vault import CreditCard
from paypalrestsdk.openid_connect import Tokeninfo, Userinfo
from paypalrestsdk.exceptions import ResourceNotFound, UnauthorizedAccess, MissingConfig
Expand Down
21 changes: 21 additions & 0 deletions paypalrestsdk/invoice_templates.py
@@ -0,0 +1,21 @@
import paypalrestsdk.util as util
from paypalrestsdk.resource import List, Find, Delete, Create, Update, Post, Resource
from paypalrestsdk.api import default as default_api

class InvoiceTemplate(List, Find, Create, Delete, Update):
"""InvoiceTemplate class wrapping the REST v1/invoicing/templates endpoint"
Usage::
>>> templates = InvoiceTemplate.all()
>>> invoice_template = InvoiceTemplate.new({})
>>> invoice.create() # return True or False
"""
path = "v1/invoicing/templates"

def __getitem__(self, key):
if key == 'id':
return self.__data__['template_id']
else:
return super.__getitem__(self, key)

10 changes: 10 additions & 0 deletions paypalrestsdk/invoices.py
Expand Up @@ -30,6 +30,16 @@ def record_payment(self, attributes):
def record_refund(self, attributes):
return self.post('record-refund', attributes, self)

def delete_external_payment(self, transactionId):
# /invoicing/invoices/<INVOICE-ID>/payment-records/<TRANSACTION-ID>
endpoint = util.join_url(self.path, str(self['id']), 'payment-records', str(transactionId))
return Resource(self.api.delete(endpoint), api=self.api)

def delete_external_refund(self, transactionId):
# /invoicing/invoices/<INVOICE-ID>/refund-records/<TRANSACTION-ID>
endpoint = util.join_url(self.path, str(self['id']), 'refund-records', str(transactionId))
return Resource(self.api.delete(endpoint), api=self.api)

def get_qr_code(self, height=500, width=500, api=None):

# height and width have default value of 500 as in the APIs
Expand Down
11 changes: 11 additions & 0 deletions samples/invoice_templates/create.py
@@ -0,0 +1,11 @@
from paypalrestsdk import InvoiceTemplate
from util import sample_invoice_template
import logging
logging.basicConfig(level=logging.INFO)

invoice_template = sample_invoice_template()
if invoice_template.create():
print("Invoice Template[%s] created successfully" % (invoice_template.template_id))
else:
print(invoice_template.error)

11 changes: 11 additions & 0 deletions samples/invoice_templates/delete.py
@@ -0,0 +1,11 @@
from paypalrestsdk import InvoiceTemplate
from util import sample_invoice_template
import logging
logging.basicConfig(level=logging.INFO)

invoice_template = sample_invoice_template()

if invoice_template.create() and invoice_template.delete():
print("Invoice Template[%s] deleted successfully" % invoice_template.template_id)
else:
print(invoice_template.error)
11 changes: 11 additions & 0 deletions samples/invoice_templates/find.py
@@ -0,0 +1,11 @@
from paypalrestsdk import InvoiceTemplate
from create import sample_invoice_template
import logging
logging.basicConfig(level=logging.INFO)

invoice_template = sample_invoice_template()
if invoice_template.create():
found_invoice_template = InvoiceTemplate.find(invoice_template.template_id)
print("Found Invoice Template[%s]" % found_invoice_template.template_id)
else:
print(invoice_template.error)
14 changes: 14 additions & 0 deletions samples/invoice_templates/get_all.py
@@ -0,0 +1,14 @@
from paypalrestsdk import InvoiceTemplate
from util import sample_invoice_template
import logging
logging.basicConfig(level=logging.INFO)

for i in range(2):
invoice_template = sample_invoice_template()
invoice_template.create()

history = InvoiceTemplate.all()

print("List Invoice Templates:")
for invoice_template in history.templates:
print(" -> Invoice Template[%s]" % (invoice_template.template_id))
18 changes: 18 additions & 0 deletions samples/invoice_templates/update.py
@@ -0,0 +1,18 @@
from paypalrestsdk import InvoiceTemplate
from util import sample_invoice_template
import random
import string
import logging
logging.basicConfig(level=logging.INFO)

invoice_template = sample_invoice_template()

if invoice_template.create():
invoice_template.template_data.items[0].quantity = 2
if invoice_template.update():
print("Invoice Template[%s] updated sucessfully" % invoice_template.template_id)
else:
print("Failed to update Invoice Template" + str(invoice_template.error))
else:
print("Failed to create Invoice Template")

42 changes: 42 additions & 0 deletions samples/invoice_templates/util.py
@@ -0,0 +1,42 @@
from paypalrestsdk import InvoiceTemplate
import random
import string

def sample_invoice_template():
return InvoiceTemplate({
"name": "Hours Template_".join([random.choice(string.letters) for i in xrange(10)]),
"default": True,
"unit_of_measure": "HOURS",
"template_data": {
"items": [{
"name": "Nutri Bullet",
"quantity": 1,
"unit_price": {
"currency": "USD",
"value": "50.00"
}
}
],
"merchant_info": {
"email": "stevendcoffey-facilitator@gmail.com"
},
"tax_calculated_after_discount": False,
"tax_inclusive": False,
"note": "Thank you for your business.",
"logo_url": "https://pics.paypal.com/v1/images/redDot.jpeg"
},
"settings": [
{
"field_name": "items.date",
"display_preference": {
"hidden": True
}
},
{
"field_name": "custom",
"display_preference": {
"hidden": True
}
}
]
})
106 changes: 106 additions & 0 deletions test/unit_tests/invoice_template_test.py
@@ -0,0 +1,106 @@
from test_helper import paypal, unittest
import random
import string
from mock import patch, ANY


class TestInvoiceTemplate(unittest.TestCase):

def setUp(self):
self.invoice_template_attributes = {
"name": "Hours Template_".join([random.choice(string.letters) for i in xrange(5)]),
"default": True,
"unit_of_measure": "HOURS",
"template_data": {
"items": [{
"name": "Nutri Bullet",
"quantity": 1,
"unit_price": {
"currency": "USD",
"value": "50.00"
}
}
],
"merchant_info": {
"email": "stevendcoffey-facilitator@gmail.com"
},
"tax_calculated_after_discount": False,
"tax_inclusive": False,
"note": "Thank you for your business.",
"logo_url": "https://pics.paypal.com/v1/images/redDot.jpeg"
},
"settings": [
{
"field_name": "items.date",
"display_preference": {
"hidden": True
}
},
{
"field_name": "custom",
"display_preference": {
"hidden": True
}
}
]
}
self.invoice_template = paypal.InvoiceTemplate(self.invoice_template_attributes)
self.invoice_template.template_id = 'TEMP-XYZ'

def test_getitem_override_reads_id_as_template_id(self):
attributes = self.invoice_template_attributes.copy()
attributes["id"] = "wrong"
attributes["template_id"] = "right"

invoice_template = paypal.InvoiceTemplate(attributes)
self.assertEqual(invoice_template["id"], "right")

@patch('test_helper.paypal.Api.post', autospec=True)
def test_create(self, mock):
invoice_template = paypal.InvoiceTemplate(self.invoice_template_attributes)
response = invoice_template.create()

mock.assert_called_once_with(self.invoice_template.api,
'v1/invoicing/templates', self.invoice_template_attributes, {'PayPal-Request-Id': invoice_template.request_id}, None)
self.assertEqual(response, True)

@patch('test_helper.paypal.Api.delete', autospec=True)
def test_delete(self, mock):
response = self.invoice_template.delete()
mock.assert_called_once_with(self.invoice_template.api,
'v1/invoicing/templates/' + self.invoice_template.template_id)
self.assertEqual(response, True)

@patch('test_helper.paypal.Api.put', autospec=True)
def test_update(self, mock):
response = self.invoice_template.update(self.invoice_template_attributes)

mock.assert_called_once_with(self.invoice_template.api,
'v1/invoicing/templates/' + self.invoice_template.template_id,
self.invoice_template_attributes, {'PayPal-Request-Id': self.invoice_template.request_id}, None)
self.assertEqual(response, True)

@patch('test_helper.paypal.Api.get', autospec=True)
def test_find(self, mock):
invoice_template = paypal.InvoiceTemplate.find(self.invoice_template.template_id)

mock.assert_called_once_with(self.invoice_template.api,
'v1/invoicing/templates/' + self.invoice_template.template_id, refresh_token=None)
self.assertTrue(isinstance(invoice_template, paypal.InvoiceTemplate))

@patch('test_helper.paypal.Api.get', autospec=True)
def test_all(self, mock):
mock.return_value = {
'total_count': 1,
'templates': [self.invoice_template]
}
history = paypal.InvoiceTemplate.all()

mock.assert_called_once_with(self.invoice_template.api,
'v1/invoicing/templates')
self.assertEqual(history.total_count, 1)
self.assertTrue(isinstance(history.templates[0], paypal.InvoiceTemplate))




12 changes: 12 additions & 0 deletions test/unit_tests/invoices_test.py
Expand Up @@ -171,3 +171,15 @@ def test_record_refund(self, mock):
mock.assert_called_once_with(self.invoice.api, 'v1/invoicing/invoices/' +
self.invoice.id + '/record-refund', refund_attributes, {'PayPal-Request-Id': ANY}, None)
self.assertEqual(response, True)

@patch('test_helper.paypal.Api.delete', autospec=True)
def test_delete_external_payment(self, mock):
response = self.invoice.delete_external_payment('1')

mock.assert_called_once_with(self.invoice.api, 'v1/invoicing/invoices/' + self.invoice.id + '/payment-records/1')

@patch('test_helper.paypal.Api.delete', autospec=True)
def test_delete_external_refund(self, mock):
response = self.invoice.delete_external_refund('1')

mock.assert_called_once_with(self.invoice.api, 'v1/invoicing/invoices/' + self.invoice.id + '/refund-records/1')

0 comments on commit c7367ac

Please sign in to comment.