From 97dfebf855193609ecc1b5fde6548ff3680ec704 Mon Sep 17 00:00:00 2001 From: John Leith Date: Fri, 6 Feb 2015 15:47:54 -0700 Subject: [PATCH 1/3] adding offamazonpayments --- mws/mws.py | 4 +- mws/offamazonpayments.py | 174 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 mws/offamazonpayments.py diff --git a/mws/mws.py b/mws/mws.py index bf8d5a6..b94314c 100644 --- a/mws/mws.py +++ b/mws/mws.py @@ -177,7 +177,7 @@ def make_request(self, extra_data, method="GET", **kwargs): 'SignatureMethod': 'HmacSHA256', } if self.auth_token: - params['MWSAuthToken'] = self.auth_token + params['MWSAuthToken'] = self.auth_token params.update(extra_data) request_description = '&'.join(['%s=%s' % (k, urllib.quote(params[k], safe='-_.~').encode('utf-8')) for k in sorted(params)]) signature = self.calc_signature(method, request_description) @@ -205,7 +205,7 @@ def make_request(self, extra_data, method="GET", **kwargs): parsed_response = DataWrapper(data, response.headers) except HTTPError, e: - error = MWSError(str(e)) + error = MWSError(str(e.response.text)) error.response = e.response raise error diff --git a/mws/offamazonpayments.py b/mws/offamazonpayments.py new file mode 100644 index 0000000..90023dc --- /dev/null +++ b/mws/offamazonpayments.py @@ -0,0 +1,174 @@ +from .mws import MWS + + +class OffAmazonPayments(MWS): + SANDBOX_URI = "/OffAmazonPayments_Sandbox/2013-01-01/" + URI = "/OffAmazonPayments/2013-01-01/" + VERSION = "2013-01-01" + + def authorize(self, order_ref, order_total, auth_id): + return self.make_request( + extra_data=dict( + Action="Authorize", + AmazonOrderReferenceId=order_ref, + AuthorizationReferenceId=str(auth_id), + TransactionTimeout="60", + **{ + "AuthorizationAmount.Amount": "{:.2f}".format(order_total), + "AuthorizationAmount.CurrencyCode": "USD" + } + ) + ) + + def get_authorization_status(self, auth_id): + return self.make_request( + extra_data=dict( + Action="GetAuthorizationDetails", + AmazonAuthorizationId=auth_id + ) + ) + + def capture(self, auth_id, amount, capture_id, notes="", currency="USD"): + """ + Captures funds + :param auth_id: the authorization id you want to capture + :param amount: the amount you wish to capture + :param capture_id: An id that you make up + :return: direct response from amazon + """ + return self.make_request( + extra_data=dict( + Action="Capture", + AmazonAuthorizationId=auth_id, + CaptureReferenceId=capture_id, + SellerCaptureNote=notes, + **{ + "CaptureAmount.Amount": "{:.2f}".format(amount), + "CaptureAmount.CurrencyCode": currency, + } + ) + ) + + def get_capture_details(self, capture_id): + return self.make_request( + extra_data=dict( + Action="GetCaptureDetails", + AmazonCaptureId=capture_id + ) + ) + + def close_authorization(self, auth_id): + """ + Call to close an authorization after the total amount of + the authorization has been captured. + """ + return self.make_request( + extra_data=dict( + Action="CloseAuthorization", + AmazonAuthorizationId=auth_id + ) + ) + + def refund(self, capture_id, amount, refund_id, notes="", currency="USD"): + """ + Refunds a captured payment + :param capture_id: the id of the captured payment + :param amount: the amount to refund + :param refund_id: a made up refund id for your reference + :param notes: + :param currency: + :return: the direct return value from amazon + """ + return self.make_request( + extra_data=dict( + Action="Refund", + AmazonCaptureId=capture_id, + RefundReferenceId=refund_id, + SellerRefundNote=notes, + **{ + "RefundAmount.Amount": amount, + "RefundAmount.CurrencyCode": currency + } + ) + ) + + def get_refund_details(self, refund_id): + """ + Call to query the status of a particular refund. + If you received a Pending status when you called the Refund operation, + you can call this operation to get the current status. + """ + return self.make_request( + extra_data=dict( + Action="GetRefundDetails", + AmazonRefundId=refund_id + ) + ) + + def get_billing_agreement_details(self, order_ref, + address_consent_token): + return self.make_request( + extra_data=dict( + Action="GetBillingAgreementDetails", + AmazonBillingAgreementId=order_ref, + AddressConsentToken=address_consent_token + ) + ) + + def get_order_reference_details(self, order_ref, + address_consent_token=""): + kwargs = {} + if address_consent_token: + kwargs['AddressConsentToken'] = address_consent_token + + return self.make_request( + extra_data=dict( + Action="GetOrderReferenceDetails", + AmazonOrderReferenceId=order_ref, + **kwargs + ) + ) + + def set_order_reference_details(self, order_ref, order_total, + store_name, order_id=None, note=None, currency="USD"): + params = { + "OrderReferenceAttributes.OrderTotal.Amount": str(order_total), + "OrderReferenceAttributes.OrderTotal.CurrencyCode": currency, + "OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId": str( + order_id), + "OrderReferenceAttributes.SellerOrderAttributes.StoreName": store_name, + "OrderReferenceAttributes.SellerNote": note, + } + + return self.make_request( + extra_data=dict( + Action="SetOrderReferenceDetails", + AmazonOrderReferenceId=order_ref, + **params + ) + ) + + def confirm_order_reference(self, order_ref): + return self.make_request( + extra_data=dict( + Action="ConfirmOrderReference", + AmazonOrderReferenceId=order_ref, + ) + ) + + def cancel_order_reference(self, order_ref): + return self.make_request( + extra_data=dict( + Action="CancelOrderReference", + AmazonOrderReferenceId=order_ref + ) + ) + + def close_order_reference(self, order_ref): + return self.make_request( + extra_data=dict( + Action="CloseOrderReference", + AmazonOrderReferenceId=order_ref + ) + ) + From 76b70c2514b7e624e6e5e240e5f95aa7db83c3e9 Mon Sep 17 00:00:00 2001 From: John Leith Date: Mon, 9 Feb 2015 10:29:34 -0700 Subject: [PATCH 2/3] fixing a bug with the refund amount --- mws/offamazonpayments.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mws/offamazonpayments.py b/mws/offamazonpayments.py index 90023dc..7bda95f 100644 --- a/mws/offamazonpayments.py +++ b/mws/offamazonpayments.py @@ -86,7 +86,7 @@ def refund(self, capture_id, amount, refund_id, notes="", currency="USD"): RefundReferenceId=refund_id, SellerRefundNote=notes, **{ - "RefundAmount.Amount": amount, + "RefundAmount.Amount": "{:.2f}".format(amount), "RefundAmount.CurrencyCode": currency } ) From 953b16eaaca3bf37a408fdc26b01c328ac58f878 Mon Sep 17 00:00:00 2001 From: John Leith Date: Mon, 9 Feb 2015 10:31:34 -0700 Subject: [PATCH 3/3] adding timeout as a default arg --- mws/offamazonpayments.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mws/offamazonpayments.py b/mws/offamazonpayments.py index 7bda95f..a6b7c7c 100644 --- a/mws/offamazonpayments.py +++ b/mws/offamazonpayments.py @@ -6,13 +6,13 @@ class OffAmazonPayments(MWS): URI = "/OffAmazonPayments/2013-01-01/" VERSION = "2013-01-01" - def authorize(self, order_ref, order_total, auth_id): + def authorize(self, order_ref, order_total, auth_id, timeout=60): return self.make_request( extra_data=dict( Action="Authorize", AmazonOrderReferenceId=order_ref, AuthorizationReferenceId=str(auth_id), - TransactionTimeout="60", + TransactionTimeout=str(timeout), **{ "AuthorizationAmount.Amount": "{:.2f}".format(order_total), "AuthorizationAmount.CurrencyCode": "USD"