Пример #1
0
    def test_issue_credit(self):
        """
        Tests issuing credit with Paypal processor
        """
        refund = self.create_refund(self.processor_name)
        order = refund.order
        basket = order.basket
        amount = refund.total_credit_excl_tax
        currency = refund.currency
        source = order.sources.first()

        transaction_id = 'PAY-REFUND-1'
        paypal_refund = paypalrestsdk.Refund({'id': transaction_id})

        payment = paypalrestsdk.Payment({
            'transactions': [
                Resource({'related_resources': [Resource({'sale': paypalrestsdk.Sale({'id': 'PAY-SALE-1'})})]})
            ]
        })
        with mock.patch.object(paypalrestsdk.Payment, 'find', return_value=payment):
            with mock.patch.object(paypalrestsdk.Sale, 'refund', return_value=paypal_refund):
                actual_transaction_id = self.processor.issue_credit(order.number, order.basket, source.reference,
                                                                    amount, currency)
                self.assertEqual(actual_transaction_id, transaction_id)

        # Verify PaymentProcessorResponse created
        self.assert_processor_response_recorded(self.processor.NAME, transaction_id, {'id': transaction_id}, basket)
Пример #2
0
    def test_issue_credit(self):
        refund = self.create_refund(self.processor_name)
        order = refund.order
        basket = order.basket
        amount = refund.total_credit_excl_tax
        currency = refund.currency
        source = order.sources.first()

        transaction_id = 'PAY-REFUND-1'
        paypal_refund = paypalrestsdk.Refund({'id': transaction_id})

        payment = Payment(
            {'transactions': [Resource({'related_resources': [Resource({'sale': Sale({'id': 'PAY-SALE-1'})})]})]})
        with mock.patch.object(Payment, 'find', return_value=payment):
            with mock.patch.object(Sale, 'refund', return_value=paypal_refund):
                self.processor.issue_credit(source, amount, currency)

        # Verify PaymentProcessorResponse created
        self.assert_processor_response_recorded(self.processor.NAME, transaction_id, {'id': transaction_id}, basket)

        # Verify Source updated
        self.assertEqual(source.amount_refunded, amount)

        # Verify PaymentEvent created
        paid_type = PaymentEventType.objects.get(code='refunded')
        order = basket.order_set.first()
        payment_event = order.payment_events.first()
        self.assert_valid_payment_event_fields(payment_event, amount, paid_type, self.processor.NAME, transaction_id)
Пример #3
0
    def test_issue_credit_error(self):
        refund = self.create_refund(self.processor_name)
        order = refund.order
        basket = order.basket
        amount = refund.total_credit_excl_tax
        currency = refund.currency
        source = order.sources.first()

        transaction_id = 'PAY-REFUND-FAIL-1'
        expected_response = {'debug_id': transaction_id}
        paypal_refund = paypalrestsdk.Refund({'error': expected_response})

        payment = Payment(
            {'transactions': [Resource({'related_resources': [Resource({'sale': Sale({'id': 'PAY-SALE-1'})})]})]})

        # Test general exception
        with mock.patch.object(Payment, 'find', return_value=payment):
            with mock.patch.object(Sale, 'refund', side_effect=ValueError):
                self.assertRaises(GatewayError, self.processor.issue_credit, source, amount, currency)
                self.assertEqual(source.amount_refunded, 0)

        # Test error response
        with mock.patch.object(Payment, 'find', return_value=payment):
            with mock.patch.object(Sale, 'refund', return_value=paypal_refund):
                self.assertRaises(GatewayError, self.processor.issue_credit, source, amount, currency)

        # Verify PaymentProcessorResponse created
        self.assert_processor_response_recorded(self.processor.NAME, transaction_id, expected_response, basket)

        # Verify Source unchanged
        self.assertEqual(source.amount_refunded, 0)
Пример #4
0
    def get_user_info(self, api=None):

        api = api or default_api()
        endpoint = util.join_url(self.path, 'userinfo')
        attributes = [('schema', "paypalv1.1")]
        url = util.join_url_params(endpoint, attributes)
        return Resource(api.get(url), api=api)
Пример #5
0
 def test_to_dict(self):
   data = {
     "intent": "sale",
     "payer": {
       "payment_method": "credit_card",
       "funding_instruments": [{
         "credit_card": {
           "type": "visa",
           "number": "4417119669820331",
           "expire_month": "11",
           "expire_year": "2018",
           "cvv2": "874",
           "first_name": "Joe",
           "last_name": "Shopper" }}]},
     "transactions": [{
       "item_list": {
         "items": [{
           "name": "item",
           "sku": "item",
           "price": "1.00",
           "currency": "USD",
           "quantity": 1 }]},
       "amount": {
         "total": "1.00",
         "currency": "USD" },
       "description": "This is the payment transaction description." }]}
   resource = Resource(data)
   self.assertEqual(resource.to_dict(), data)
Пример #6
0
    def search(cls, params=None, api=None):
        api = api or default_api()
        params = params or {}

        url = util.join_url(cls.path, 'search')

        return Resource(api.post(url, params), api=api)
Пример #7
0
    def test_passing_api(self):
        """
        Check that api objects are passed on to new resources when given
        """
        class DummyAPI(object):
            post = lambda s, *a, **k: {}
            get = lambda s, *a, **k: {}

        api = DummyAPI()

        # Conversion
        resource = Resource({
            'name': 'testing',
        }, api=api)
        self.assertEqual(resource.api, api)
        convert_ret = resource.convert('test', {})
        self.assertEqual(convert_ret.api, api)

        class TestResource(Find, List, Post):
            path = '/'

        # Find
        find = TestResource.find('resourceid', api=api)
        self.assertEqual(find.api, api)

        # List
        list_ = TestResource.all(api=api)
        self.assertEqual(list_.api, api)

        # Post
        post = TestResource({'id': 'id'}, api=api)
        post_ret = post.post('test')
        self.assertEqual(post_ret.api, api)
Пример #8
0
    def test_default_resource(self):
        from paypalrestsdk import api
        original = api.__api__

        class DummyAPI(object):
            post = lambda s, *a, **k: {}
            get = lambda s, *a, **k: {}

        # Make default api object a dummy api object
        default = api.__api__ = DummyAPI()

        resource = Resource({})
        self.assertEqual(resource.api, default)

        class TestResource(Find, List, Post):
            path = '/'

        # Find
        find = TestResource.find('resourceid')
        self.assertEqual(find.api, default)

        # List
        list_ = TestResource.all()
        self.assertEqual(list_.api, default)

        api.__api__ = original  # Restore original api object
Пример #9
0
 def test_http_headers(self):
   data = {
    'name': 'testing',
    'header': { 'My-Header': 'testing' } }
   resource = Resource(data)
   self.assertEqual(resource.header, {'My-Header': 'testing'})
   self.assertEqual(resource.http_headers(), {'PayPal-Request-Id': resource.request_id, 'My-Header': 'testing'})
    def execute(cls, payment_token, params=None, api=None):
        api = api or default_api()
        params = params or {}

        url = util.join_url(cls.path, payment_token, 'agreement-execute')

        return Resource(api.post(url, params), api=api)
Пример #11
0
 def test_request_id(self):
   data = {
     'name': 'testing',
     'request_id': 1234 }
   resource = Resource(data)
   self.assertEqual(resource.to_dict(), {'name': 'testing'})
   self.assertEqual(resource.request_id, 1234)
   self.assertEqual(resource.http_headers(), {'PayPal-Request-Id': 1234})
Пример #12
0
 def test_setter(self):
   data = { 'name': 'testing' }
   resource = Resource(data)
   self.assertEqual(resource.name, 'testing' )
   resource.name = 'changed'
   self.assertEqual(resource.name, 'changed' )
   resource['name'] = 'again-changed'
   self.assertEqual(resource.name, 'again-changed' )
   resource.transaction = { 'description': 'testing' }
   self.assertEqual(resource.transaction.__class__, Resource)
   self.assertEqual(resource.transaction.description, 'testing')
Пример #13
0
    def search_transactions(self, start_date, end_date, api=None):
        if not start_date or not end_date:
            raise exceptions.MissingParam("Search transactions needs valid start_date and end_date.")
        api = api or default_api()

        # Construct url similar to
        # /billing-agreements/I-HT38K76XPMGJ/transactions?start-date=2014-04-13&end-date=2014-04-30
        endpoint = util.join_url(self.path, str(self['id']), 'transactions')
        date_range = [('start_date', start_date), ('end_date', end_date)]
        url = util.join_url_params(endpoint, date_range)

        return Resource(self.api.get(url), api=api)
Пример #14
0
    def get_qr_code(self, height=500, width=500, api=None):

        # height and width have default value of 500 as in the APIs
        api = api or default_api()

        # Construct url similar to
        # /invoicing/invoices/<INVOICE-ID>/qr-code?height=<HEIGHT>&width=<WIDTH>
        endpoint = util.join_url(self.path, str(self['id']), 'qr-code')
        image_attributes = [('height', height), ('width', width)]
        url = util.join_url_params(endpoint, image_attributes)

        return Resource(self.api.get(url), api=api)
Пример #15
0
 def test_getter(self):
   data = {
     'name': 'testing',
     'amount': 10.0,
     'transaction': { 'description': 'testing' },
     'items': [ { 'name': 'testing' } ] }
   resource = Resource(data)
   self.assertEqual(resource.name, 'testing')
   self.assertEqual(resource['name'], 'testing')
   self.assertEqual(resource.amount, 10.0)
   self.assertEqual(resource.items[0].__class__, Resource)
   self.assertEqual(resource.items[0].name, 'testing')
   self.assertEqual(resource.items[0]['name'], 'testing')
   self.assertEqual(resource.unknown, None)
   self.assertRaises(KeyError, lambda: resource['unknown'])
Пример #16
0
 def test_contains(self):
     data = {'name': 'testing'}
     resource = Resource(data)
     self.assertEqual('name' in resource, True)
     self.assertEqual('testing' in resource, False)
Пример #17
0
 def get_event_types(self, api=None):
     """Get the list of events types that are subscribed to a webhook
     """
     api = api or default_api()
     url = util.join_url(self.path, str(self['id']), 'event-types')
     return Resource(self.api.get(url), api=api)
Пример #18
0
 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)
Пример #19
0
 def next_invoice_number(cls, api=None):
     api = api or default_api()
     url = util.join_url(cls.path, 'next-invoice-number')
     return Resource(api.post(url), api=api)