def get_details(): from amazon_pay.client import AmazonPayClient client = AmazonPayClient( mws_access_key=session['mws_access_key'], mws_secret_key=session['mws_secret_key'], merchant_id=session['merchant_id'], sandbox=True, region='na', currency_code='USD', log_enabled=True, log_file_name="log.txt", log_level="DEBUG") billing_agreement_id = request.form['billingAgreementId'] session['billing_agreement_id'] = billing_agreement_id response = client.set_billing_agreement_details( amazon_billing_agreement_id=billing_agreement_id, store_name='Amazon Pay Python SDK') if response.success: response = client.get_billing_agreement_details( amazon_billing_agreement_id=billing_agreement_id, address_consent_token=session['access_token']) pretty = json.dumps( json.loads( response.to_json()), indent=4) return pretty
def confirm(): pretty_confirm = None pretty_authorize = None client = AmazonPayClient( mws_access_key=session['mws_access_key'], mws_secret_key=session['mws_secret_key'], merchant_id=session['merchant_id'], sandbox=True, region=REGION_CODE, currency_code=CURRENCY_CODE, log_enabled=True, log_file_name="/tmp/amzpay.log", log_level="DEBUG") print('session', session) response = client.confirm_order_reference( amazon_order_reference_id=session['order_reference_id']) pretty_confirm = json.dumps(json.loads(response.to_json()), indent=4) if response.success: response = client.authorize( amazon_order_reference_id=session['order_reference_id'], authorization_reference_id=rand(), authorization_amount=session['order_amount'], transaction_timeout=0, capture_now=bool(random.randint(0, 1))) pretty_authorize = json.dumps(json.loads(response.to_json()), indent=4) return render_template( 'confirm.html', confirm=pretty_confirm, authorize=pretty_authorize)
def authorize(): from amazon_pay.client import AmazonPayClient client = AmazonPayClient( mws_access_key=session['mws_access_key'], mws_secret_key=session['mws_secret_key'], merchant_id=session['merchant_id'], sandbox=True, region='na', currency_code='USD', log_enabled=True, log_file_name="log.txt", log_level="DEBUG") response = client.authorize_on_billing_agreement( amazon_billing_agreement_id=session['billing_agreement_id'], authorization_reference_id=rand(), authorization_amount='1.99', seller_authorization_note='Python SDK payment authorization.', transaction_timeout=0, capture_now=True) response = client.get_billing_agreement_details( amazon_billing_agreement_id=session['billing_agreement_id'], address_consent_token=session['access_token']) pretty = json.dumps( json.loads( response.to_json()), indent=4) return pretty
def get_details(): if request.method == 'POST': from amazon_pay.client import AmazonPayClient client = AmazonPayClient( mws_access_key=request.session['mws_access_key'], mws_secret_key=request.session['mws_secret_key'], merchant_id=request.session['merchant_id'], sandbox=True, region='na', currency_code='USD', log_enabled=True, log_file_name="log.txt", log_level="DEBUG") order_reference_id = request.POST.get('orderReferenceId') request.session['order_reference_id'] = order_reference_id print(request.session['order_reference_id']) response = client.set_order_reference_details( amazon_order_reference_id=order_reference_id, order_total='19.95') if response.success: response = client.get_order_reference_details( amazon_order_reference_id=order_reference_id, address_consent_token=request.session['access_token']) pretty = json.dumps(json.loads(response.to_json()), indent=4) return pretty
def get_details(): from amazon_pay.client import AmazonPayClient client = AmazonPayClient( mws_access_key=session['mws_access_key'], mws_secret_key=session['mws_secret_key'], merchant_id=session['merchant_id'], sandbox=True, region='na', currency_code='USD', log_enabled=True, log_file_name="log.txt", log_level="DEBUG") order_reference_id = request.form['orderReferenceId'] session['order_reference_id'] = order_reference_id print(session['order_reference_id']) response = client.set_order_reference_details( amazon_order_reference_id=order_reference_id, order_total='19.95') if response.success: response = client.get_order_reference_details( amazon_order_reference_id=order_reference_id, address_consent_token=session['access_token']) pretty = json.dumps( json.loads( response.to_json()), indent=4) return pretty
def SetOrderAttributes(**kwargs): print("Inside SetOrderAttributes()") print(kwargs) client = AmazonPayClient(mws_access_key=kwargs["MWS_ACCESS_KEY"], mws_secret_key=kwargs["MWS_SECRET_KEY"], merchant_id=kwargs["MERCHANT_ID"], region=kwargs["REGION"], currency_code=kwargs["CURRENCY_CODE"], sandbox=True) print(client) timestamp_ = time.time() ret = client.set_order_attributes( amazon_order_reference_id=kwargs["ORDER_REF_ID"], amount=kwargs["ORDER_TOTAL"], currency_code=kwargs["CURRENCY_CODE"], seller_note='Thank you for donating {} {} to the charity of the day!'. format(kwargs["ORDER_TOTAL"], kwargs["CURRENCY_CODE"]), seller_order_id='#-donatio-{}-{}'.format(timestamp_, kwargs["ORDER_TOTAL"]), store_name='Donatio: Gamify your Charity', custom_information='-') print("Set Order Attributes") print(ret.to_json()) response = client.confirm_order_reference( amazon_order_reference_id=kwargs["ORDER_REF_ID"], success_url= "https://donatio-site.herokuapp.com/paymentStatus?success=true", failure_url= "https://donatio-site.herokuapp.com/paymentStatus?success=false") print("Confirm Order Reference") print(response.to_json())
def setUp(self): self.maxDiff = None self.mws_access_key = 'mws_access_key' self.mws_secret_key = 'mws_secret_key' self.merchant_id = 'merchant_id' self.service_version = '2013-01-01' self.mws_endpoint = \ 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/{}'.format( self.service_version) self.client = AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='na', currency_code='USD' ) self.request = PaymentRequest( params={'test': 'test'}, config={'mws_access_key': self.mws_access_key, 'mws_secret_key': self.mws_secret_key, 'api_version': '2013-01-01', 'merchant_id': self.merchant_id, 'mws_endpoint': self.mws_endpoint, 'headers': {'test': 'test'}, 'handle_throttle': True}) self.response = PaymentResponse('<test>الفلانية فلا</test>') self.supplementary_data = '{"AirlineMetaData" : {"version": 1.0, "airlineCode": "PAX", "flightDate": "2018-03-24T20:29:19.22Z", "departureAirport": "CDG", "destinationAirport": "LUX", "bookedLastTime": -1, "classOfTravel": "F", "passengers": {"numberOfPassengers": 4, "numberOfChildren": 1, "numberOfInfants": 1 }}, "AccommodationMetaData": {"version": 1.0, "startDate": "2018-03-24T20:29:19.22Z", "endDate": "2018-03-24T20:29:19.22Z", "lengthOfStay": 5, "numberOfGuests": 4, "class": "Standard", "starRating": 5, "bookedLastTime": -1 }, "OrderMetaData": {"version": 1.0, "numberOfItems": 3, "type": "Digital" }, "BuyerMetaData": {"version" : 1.0, "isFirstTimeCustomer" : true, "numberOfPastPurchases" : 2, "numberOfDisputedPurchases" : 3, "hasOpenDispute" : true, "riskScore" : 0.75 }}'
def review(): client = AmazonPayClient( mws_access_key=session['mws_access_key'], mws_secret_key=session['mws_secret_key'], merchant_id=session['merchant_id'], sandbox=True, region=REGION_CODE, currency_code=CURRENCY_CODE, log_enabled=True, log_file_name="/tmp/amzpay.log", log_level="DEBUG") order_reference_id = request.form['orderReferenceId'] session['order_reference_id'] = order_reference_id print('get details for orderId', session['order_reference_id']) response = client.set_order_reference_details( amazon_order_reference_id=order_reference_id, order_total=session['order_amount'], seller_note='My seller note.', seller_order_id=secrets.token_hex(16), store_name='My store name.', custom_information='My custom information.') if response.success: response = client.get_order_reference_details( amazon_order_reference_id=order_reference_id, address_consent_token=session['amazon_Login_accessToken']) order_detail = json.dumps(json.loads(response.to_json()), indent=4) return render_template('review.html', order_detail=order_detail)
def setUp(self): self.maxDiff = None self.mws_access_key = 'mws_access_key' self.mws_secret_key = 'mws_secret_key' self.merchant_id = 'merchant_id' self.service_version = '2013-01-01' self.mws_endpoint = \ 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/{}'.format( self.service_version) self.client = AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='na', currency_code='test') self.request = PaymentRequest( params={'test': 'test'}, config={'mws_access_key': self.mws_access_key, 'mws_secret_key': self.mws_secret_key, 'api_version': '2013-01-01', 'merchant_id': self.merchant_id, 'mws_endpoint': self.mws_endpoint, 'headers': {'test': 'test'}, 'handle_throttle': True}) self.response = PaymentResponse('<test>test</test>')
def test_environment_variables(self): os.environ['AP_REGION'] = 'na' os.environ['AP_MWS_ACCESS_KEY'] = 'AP_MWS_ACCESS_KEY' os.environ['AP_MERCHANT_ID'] = 'AP_MERCHANT_ID' os.environ['AP_CURRENCY_CODE'] = 'AP_CURRENCY_CODE' os.environ['AP_MWS_SECRET_KEY'] = 'AP_MWS_SECRET_KEY' client = AmazonPayClient(sandbox=True) self.assertEqual(client.region, 'na') self.assertEqual(client.mws_access_key, 'AP_MWS_ACCESS_KEY') self.assertEqual(client.mws_secret_key, 'AP_MWS_SECRET_KEY') self.assertEqual(client.merchant_id, 'AP_MERCHANT_ID') self.assertEqual(client.currency_code, 'AP_CURRENCY_CODE') os.environ['AP_REGION'] = 'AP_REGION' with self.assertRaises(KeyError): client = AmazonPayClient()
def test_region_exception(self): with self.assertRaises(KeyError): AmazonPayClient(mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='should_throw_exception', currency_code='test')
def api(self): return AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, region=self.region, currency_code=self.currency_code, handle_throttle=self.handle_throttle, sandbox=self.sandbox)
def confirm(): from amazon_pay.client import AmazonPayClient pretty_confirm = None pretty_authorize = None client = AmazonPayClient( mws_access_key=session['mws_access_key'], mws_secret_key=session['mws_secret_key'], merchant_id=session['merchant_id'], sandbox=True, region='na', currency_code='USD', log_enabled=True, log_file_name="log.txt", log_level="DEBUG") print(session) response = client.confirm_order_reference( amazon_order_reference_id=session['order_reference_id']) pretty_confirm = json.dumps( json.loads( response.to_json()), indent=4) if response.success: response = client.authorize( amazon_order_reference_id=session['order_reference_id'], authorization_reference_id=rand(), authorization_amount='19.95', transaction_timeout=0, capture_now=False) pretty_authorize = json.dumps( json.loads( response.to_json()), indent=4) return render_template( 'confirm.html', confirm=pretty_confirm, authorize=pretty_authorize)
def confirm(): from amazon_pay.client import AmazonPayClient client = AmazonPayClient( mws_access_key=session['mws_access_key'], mws_secret_key=session['mws_secret_key'], merchant_id=session['merchant_id'], sandbox=True, region='na', currency_code='USD', log_enabled=True, log_file_name="log.txt", log_level="DEBUG") response = client.confirm_billing_agreement( amazon_billing_agreement_id=session['billing_agreement_id']) pretty = json.dumps( json.loads( response.to_json()), indent=4) return render_template('confirm.html', confirm_response=pretty)
def test_application_settings(self): client = AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='na', currency_code='test', application_name='test_application', application_version='test_application_version') self.assertEqual(client.application_name, 'test_application') self.assertEqual(client.application_version, 'test_application_version')
def AmazonConfirmationBackend(): pretty_confirm = None pretty_authorize = None client = AmazonPayClient( mws_access_key=settings.MWS_ACCESS, mws_secret_key=settings.MWS_SECRET, merchant_id=settings.MERCHANT_ID, sandboz=True, region='na', currency_code='USD', log_enabled=True, log_file_name='log.txt', log_level='DEBUG' ) print(client)
def authorization_notification(payload): amzn_id = payload["AuthorizationNotification"]["AuthorizationDetails"][ "AmazonAuthorizationId"] # trim everything after the last dash - seems like there should be a more # straightforward way to do this match = re.search("^(.*)[-]", amzn_id) amzn_id = match.group(1) logging.info(amzn_id) client = AmazonPayClient( mws_access_key=MWS_ACCESS_KEY, mws_secret_key=MWS_SECRET_KEY, merchant_id=AMAZON_MERCHANT_ID, region="na", currency_code="USD", sandbox=AMAZON_SANDBOX, ) response = client.get_order_reference_details( amazon_order_reference_id=amzn_id) response = response.to_dict() logging.info(json.dumps(response, indent=4)) details = response["GetOrderReferenceDetailsResponse"][ "GetOrderReferenceDetailsResult"]["OrderReferenceDetails"] amount = details["OrderTotal"]["Amount"] logging.info(amount) name = HumanName(details["Buyer"]["Name"]) first_name = name.first last_name = name.last email = details["Buyer"]["Email"] zipcode = get_zip(details=details) description = details["SellerOrderAttributes"]["StoreName"] logging.info("----Getting contact....") contact = Contact.get_or_create(email=email, first_name=first_name, last_name=last_name, zipcode=zipcode) logging.info(contact) if contact.first_name == "Subscriber" and contact.last_name == "Subscriber": logging.info(f"Changing name of contact to {first_name} {last_name}") contact.first_name = first_name contact.last_name = last_name contact.save() if contact.first_name != first_name or contact.last_name != last_name: logging.info( f"Contact name doesn't match: {contact.first_name} {contact.last_name}" ) if zipcode and not contact.created and contact.mailing_postal_code != zipcode: contact.mailing_postal_code = zipcode contact.save() logging.info("----Adding opportunity...") opportunity = Opportunity(contact=contact, stage_name="Closed Won") opportunity.amount = amount opportunity.description = description opportunity.lead_source = "Amazon Alexa" opportunity.amazon_order_id = amzn_id opportunity.campaign_id = AMAZON_CAMPAIGN_ID opportunity.name = ( f"[Alexa] {contact.first_name} {contact.last_name} ({contact.email})") opportunity.save() logging.info(opportunity) notify_slack(contact=contact, opportunity=opportunity) if contact.duplicate_found: send_multiple_account_warning(contact)
class AmazonPayClientTest(unittest.TestCase): def setUp(self): self.maxDiff = None self.mws_access_key = 'mws_access_key' self.mws_secret_key = 'mws_secret_key' self.merchant_id = 'merchant_id' self.service_version = '2013-01-01' self.mws_endpoint = \ 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/{}'.format( self.service_version) self.client = AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='na', currency_code='USD' ) self.request = PaymentRequest( params={'test': 'test'}, config={'mws_access_key': self.mws_access_key, 'mws_secret_key': self.mws_secret_key, 'api_version': '2013-01-01', 'merchant_id': self.merchant_id, 'mws_endpoint': self.mws_endpoint, 'headers': {'test': 'test'}, 'handle_throttle': True}) self.response = PaymentResponse('<test>الفلانية فلا</test>') self.supplementary_data = '{"AirlineMetaData" : {"version": 1.0, "airlineCode": "PAX", "flightDate": "2018-03-24T20:29:19.22Z", "departureAirport": "CDG", "destinationAirport": "LUX", "bookedLastTime": -1, "classOfTravel": "F", "passengers": {"numberOfPassengers": 4, "numberOfChildren": 1, "numberOfInfants": 1 }}, "AccommodationMetaData": {"version": 1.0, "startDate": "2018-03-24T20:29:19.22Z", "endDate": "2018-03-24T20:29:19.22Z", "lengthOfStay": 5, "numberOfGuests": 4, "class": "Standard", "starRating": 5, "bookedLastTime": -1 }, "OrderMetaData": {"version": 1.0, "numberOfItems": 3, "type": "Digital" }, "BuyerMetaData": {"version" : 1.0, "isFirstTimeCustomer" : true, "numberOfPastPurchases" : 2, "numberOfDisputedPurchases" : 3, "hasOpenDispute" : true, "riskScore" : 0.75 }}' def mock_requests_post(self, url, data=None, headers=None, verify=False): mock_response = Mock() mock_response.text = '<GetBillingAgreementDetailsResponse>\ <GetBillingAgreementDetailsResult><BillingAgreementDetails>\ <BillingAgreementStatus><State>Draft</State>\ </BillingAgreementStatus></BillingAgreementDetails>\ </GetBillingAgreementDetailsResult>\ </GetBillingAgreementDetailsResponse>' mock_response.status_code = 200 return mock_response def mock_requests_500_post( self, url, data=None, headers=None, verify=False): mock_response = Mock() mock_response.text = '<error>test</error>' mock_response.status_code = 500 return mock_response def mock_requests_generic_error_post( self, url, data=None, headers=None, verify=False): mock_response = Mock() mock_response.text = '<error>test</error>' mock_response.status_code = 502 return mock_response def mock_requests_503_post( self, url, data=None, headers=None, verify=False): mock_response = Mock() mock_response.text = '<error>test</error>' mock_response.status_code = 503 return mock_response def mock_get_login_profile(self, url, headers, params, verify): mock_response = Mock() mock_response.json.return_value = {"aud": "client_id"} mock_response.status_code = 200 return mock_response def test_sandbox_setter(self): self.client.sandbox = False self.assertEqual( self.client._mws_endpoint, 'https://mws.amazonservices.com/OffAmazonPayments/2013-01-01') self.client.sandbox = True self.assertEqual( self.client._mws_endpoint, 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/2013-01-01') def test_sanitize_response_data(self): current_file_dir = os.path.dirname(__file__) test_file_path = os.path.join(current_file_dir, "log.txt") f = open(test_file_path, "r") source_text = f.read() f.close() text = self.request._sanitize_response_data(source_text) test_file_path = os.path.join(current_file_dir, "sanlog.txt") f = open(test_file_path, "r") san_text = f.read() f.close self.assertEqual(text, san_text) def test_region_exception(self): with self.assertRaises(KeyError): AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='should_throw_exception', currency_code='test') def test_set_endpoint(self): self.client._set_endpoint() self.assertEqual( self.client._mws_endpoint, 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/2013-01-01') def test_sign(self): test_signature = self.request._sign('my_test_string') self.assertEqual( test_signature, 'JQZYxe8EFlLE3XCAWotsn329rpZF7OFYhA8oo7rUV2E=') def test_application_settings(self): client = AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='na', currency_code='USD', application_name='test_application', application_version='test_application_version') self.assertEqual(client.application_name, 'test_application') self.assertEqual( client.application_version, 'test_application_version') def test_properties(self): self.assertEqual(self.client.mws_access_key, 'mws_access_key') self.assertEqual(self.client.mws_secret_key, 'mws_secret_key') self.assertEqual(self.client.merchant_id, 'merchant_id') self.assertEqual(self.client._region_code, 'na') self.assertEqual(self.client.currency_code, 'USD') self.assertEqual(self.client.handle_throttle, False) self.assertEqual(self.client.sandbox, True) @patch('requests.post') def test_generic_error_response(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_generic_error_post self.request.send_post() response = self.request.response self.assertEqual(type(response), PaymentErrorResponse) @patch('requests.post') def test_500_response(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_500_post self.request.send_post() response = self.request.response.to_dict() self.assertEqual(response['error'], '500') @patch('requests.post') def test_503_response(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_503_post self.request.send_post() response = self.request.response.to_dict() self.assertEqual(response['error'], '503') @patch('requests.post') def test_headers(self, mock_urlopen): py_version = ".".join(map(str, sys.version_info[:3])) mock_urlopen.side_effect = self.mock_requests_post self.client.get_service_status() if sys.version_info[0] == 3 and sys.version_info[1] >= 2: py_valid = True header_expected = { 'Content-Type': 'application/x-www-form-urlencoded', "User-Agent":'amazon-pay-sdk-python/{0} ({1}Python/{2}; {3}/{4})'.format( str(ap_version.versions['application_version']), (''), py_version, str(platform.system()), str(platform.release()) ) } self.assertEqual(mock_urlopen.call_args[1]['headers'], header_expected) self.assertTrue(py_valid, True) @patch('requests.post') def test_get_merchant_account_status(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_merchant_account_status( merchant_id='A2AMGDUDUJFL', mws_auth_token='amzn.mws.d8f2d-6a5f-b46293482379') parameters = { 'Action': 'GetMerchantAccountStatus', 'SellerId': 'A2AMGDUDUJFL', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b46293482379'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_create_order_reference_for_id(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.create_order_reference_for_id( object_id='B01-462347-4762387', object_id_type='BillingAgreement', order_total='1', inherit_shipping_address=False, confirm_now=True, platform_id='testPlatformId123', seller_note='testSellerNote2145', seller_order_id='testSellerOrderId21434', supplementary_data=self.supplementary_data, store_name='testStoreName1234', custom_information='testCustomInfo12435', merchant_id='A2AMR0DUGHIUEHQ', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b06476237468923749823') parameters = { 'Action': 'CreateOrderReferenceForId', 'Id': 'B01-462347-4762387', 'IdType': 'BillingAgreement', 'OrderReferenceAttributes.OrderTotal.Amount': '1', 'OrderReferenceAttributes.OrderTotal.CurrencyCode': 'USD', 'InheritShippingAddress': 'false', 'ConfirmNow': 'true', 'OrderReferenceAttributes.PlatformId': 'testPlatformId123', 'OrderReferenceAttributes.SellerNote': 'testSellerNote2145', 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId': 'testSellerOrderId21434', 'OrderReferenceAttributes.SupplementaryData': self.supplementary_data, 'OrderReferenceAttributes.SellerOrderAttributes.StoreName': 'testStoreName1234', 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation': 'testCustomInfo12435', 'SellerId': 'A2AMR0DUGHIUEHQ', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b06476237468923749823'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_billing_agreement_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_billing_agreement_details( amazon_billing_agreement_id='B01-47236478-46253862', address_consent_token='AFYDFWIGHUIP', merchant_id='ADEIUYIOQUIOW', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-7462348237498') parameters = { 'Action': 'GetBillingAgreementDetails', 'AmazonBillingAgreementId': 'B01-47236478-46253862', 'AddressConsentToken': 'AFYDFWIGHUIP', 'SellerId': 'ADEIUYIOQUIOW', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-7462348237498'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_set_billing_agreement_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.set_billing_agreement_details( amazon_billing_agreement_id='B01-47236478-462863428', platform_id='testPlatformId89', seller_note='testSellerNote3251', seller_billing_agreement_id='testBillingAgreement1213', store_name='testStoreName5237', custom_information='testCustomInfo32365', merchant_id='AGDUIEJOQEOPQWIKO', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b06a-bc12-4623862') parameters = { 'Action': 'SetBillingAgreementDetails', 'AmazonBillingAgreementId': 'B01-47236478-462863428', 'BillingAgreementAttributes.PlatformId': 'testPlatformId89', 'BillingAgreementAttributes.SellerNote': 'testSellerNote3251', 'BillingAgreementAttributes.SellerBillingAgreementAttributes.SellerBillingAgreementId': 'testBillingAgreement1213', 'BillingAgreementAttributes.SellerBillingAgreementAttributes.StoreName': 'testStoreName5237', 'BillingAgreementAttributes.SellerBillingAgreementAttributes.CustomInformation': 'testCustomInfo32365', 'SellerId': 'AGDUIEJOQEOPQWIKO', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b06a-bc12-4623862'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_confirm_billing_agreement(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.confirm_billing_agreement( amazon_billing_agreement_id='B01-47236478-46284638789', merchant_id='AGFUHWIEJLMLK', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b06a-bc12-4263289') parameters = { 'Action': 'ConfirmBillingAgreement', 'AmazonBillingAgreementId': 'B01-47236478-46284638789', 'SellerId': 'AGFUHWIEJLMLK', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b06a-bc12-4263289'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_validate_billing_agreement(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.validate_billing_agreement( amazon_billing_agreement_id='B01-47236478-46287462347823490', merchant_id='AGFUHWHYDIIJQWL', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b06a-bc12-457267342897') parameters = { 'Action': 'ValidateBillingAgreement', 'AmazonBillingAgreementId': 'B01-47236478-46287462347823490', 'SellerId': 'AGFUHWHYDIIJQWL', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b06a-bc12-457267342897'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_authorize_on_billing_agreement(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.authorize_on_billing_agreement( amazon_billing_agreement_id='B01-4653268-47632947', authorization_reference_id='testAuthRefId31253', authorization_amount='1', seller_authorization_note='testSellerAuthNote3612367', transaction_timeout=0, capture_now=True, soft_descriptor='testSoftDescriptor42837', seller_note='testSellerNote4721893', platform_id='testPlatformId47237', seller_order_id='testSellerOrderId4237', store_name='testStoreName842398', custom_information='testCustomInfo623', supplementary_data=self.supplementary_data, inherit_shipping_address=False, merchant_id='A2AMR0FDYHGHJD', mws_auth_token='amzn.mws.d6ac8f2d-463286-fhegsdj46238') parameters = { 'Action': 'AuthorizeOnBillingAgreement', 'AmazonBillingAgreementId': 'B01-4653268-47632947', 'TransactionTimeout': '0', 'AuthorizationReferenceId': 'testAuthRefId31253', 'AuthorizationAmount.Amount': '1', 'AuthorizationAmount.CurrencyCode': 'USD', 'CaptureNow': 'true', 'SellerAuthorizationNote': 'testSellerAuthNote3612367', 'SoftDescriptor': 'testSoftDescriptor42837', 'SellerNote': 'testSellerNote4721893', 'PlatformId': 'testPlatformId47237', 'InheritShippingAddress': 'false', 'SellerOrderAttributes.SellerOrderId': 'testSellerOrderId4237', 'SellerOrderAttributes.StoreName': 'testStoreName842398', 'SellerOrderAttributes.CustomInformation': 'testCustomInfo623', 'SellerOrderAttributes.SupplementaryData': self.supplementary_data, 'SellerId': 'A2AMR0FDYHGHJD', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-463286-fhegsdj46238'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_close_billing_agreement(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.close_billing_agreement( amazon_billing_agreement_id='B01-4236278-3761372', closure_reason='testClosureReason', merchant_id='A2AMR0DGUQHWIJQWL', mws_auth_token='amzn.mws.d6ac8f2d-463286-fhegsdj46238') parameters = { 'Action': 'CloseBillingAgreement', 'AmazonBillingAgreementId': 'B01-4236278-3761372', 'ClosureReason': 'testClosureReason', 'SellerId': 'A2AMR0DGUQHWIJQWL', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-463286-fhegsdj46238'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_set_order_reference_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.set_order_reference_details( amazon_order_reference_id='P01-1234567-7654897', order_total='1', platform_id='platformId4673', seller_note='sellerNote38278', seller_order_id='sellerOrderId123', store_name='testStoreName387289', custom_information='customInfo34278', merchant_id='A2AMR0CLHYUTGH', mws_auth_token='amzn.mws.d8f2d-6a5f-b06a4628', supplementary_data=self.supplementary_data) parameters = { 'Action': 'SetOrderReferenceDetails', 'AmazonOrderReferenceId': 'P01-1234567-7654897', 'OrderReferenceAttributes.OrderTotal.Amount': '1', 'OrderReferenceAttributes.OrderTotal.CurrencyCode': 'USD', 'OrderReferenceAttributes.PlatformId': 'platformId4673', 'OrderReferenceAttributes.SellerNote': 'sellerNote38278', 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId': 'sellerOrderId123', 'OrderReferenceAttributes.SellerOrderAttributes.StoreName': 'testStoreName387289', 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation': 'customInfo34278', 'SellerId': 'A2AMR0CLHYUTGH', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b06a4628', 'OrderReferenceAttributes.SellerOrderAttributes.SupplementaryData': self.supplementary_data} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_set_order_attributes(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.set_order_attributes( amazon_order_reference_id='P01-1234567-4827348237', currency_code='USD', amount='1', seller_order_id='testSellerOrderId5371', payment_service_provider_id='AGHJHHJKJHL', payment_service_provider_order_id='testPSPOrderId', platform_id='testPlatformId472', seller_note='testSellerNote4628', request_payment_authorization='true', store_name='testStoreName26157', list_order_item_categories=['test'], custom_information='testCustomInfo35273', merchant_id='AGHJHHJKJHL', mws_auth_token='amzn.mws.d8f2d-6a5f-b06a4628', supplementary_data=self.supplementary_data) parameters = { 'Action': 'SetOrderAttributes', 'AmazonOrderReferenceId': 'P01-1234567-4827348237', 'OrderAttributes.OrderTotal.Amount': '1', 'OrderAttributes.OrderTotal.CurrencyCode': 'USD', 'OrderAttributes.SellerOrderAttributes.CustomInformation': 'testCustomInfo35273', 'OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.1': 'test', 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderId': 'AGHJHHJKJHL', 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderOrderId': 'testPSPOrderId', 'OrderAttributes.PlatformId': 'testPlatformId472', 'OrderAttributes.RequestPaymentAuthorization': 'true', 'OrderAttributes.SellerNote': 'testSellerNote4628', 'OrderAttributes.SellerOrderAttributes.SellerOrderId': 'testSellerOrderId5371', 'OrderAttributes.SellerOrderAttributes.StoreName': 'testStoreName26157', 'SellerId': 'AGHJHHJKJHL', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b06a4628', 'OrderAttributes.SellerOrderAttributes.SupplementaryData': self.supplementary_data} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_order_reference_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_order_reference_details( amazon_order_reference_id='P01-476238-47238', address_consent_token='ADUHIQILPLP', access_token='AHJJOKJJHNJNJK', merchant_id='ADGJUHJWKJKJ', mws_auth_token='amzn.mws.d8f2d-6a5f-b427489234798') parameters = { 'Action': 'GetOrderReferenceDetails', 'AmazonOrderReferenceId': 'P01-476238-47238', 'AddressConsentToken': 'ADUHIQILPLP', 'AccessToken': 'AHJJOKJJHNJNJK', 'SellerId': 'ADGJUHJWKJKJ', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b427489234798' } data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_confirm_order_reference(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.confirm_order_reference( amazon_order_reference_id='P01-476238-47263849238', merchant_id='AHDGJHDJKFJIIIJ', mws_auth_token='amzn.mws.d8f2d-6a5f-b42rwe74237489', success_url='https://www.success.com', failure_url='https://www.failure.com', authorization_amount='5', currency_code='USD' ) parameters = { 'Action': 'ConfirmOrderReference', 'AmazonOrderReferenceId': 'P01-476238-47263849238', 'SellerId': 'AHDGJHDJKFJIIIJ', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b42rwe74237489', 'SuccessUrl': 'https://www.success.com', 'FailureUrl': 'https://www.failure.com', 'AuthorizationAmount.Amount': '5', 'AuthorizationAmount.CurrencyCode': 'USD' } data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_cancel_order_reference(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.cancel_order_reference( amazon_order_reference_id='P01-476238-472642737489', cancelation_reason='testCancelReason', merchant_id='AJHDELWJEKELW', mws_auth_token='amzn.mws.d8f2d-6a5f-b42rw72372897893') parameters = { 'Action': 'CancelOrderReference', 'AmazonOrderReferenceId': 'P01-476238-472642737489', 'CancelationReason': 'testCancelReason', 'SellerId': 'AJHDELWJEKELW', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b42rw72372897893'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_close_order_reference(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.close_order_reference( amazon_order_reference_id='P01-476238-472642737489', closure_reason='testClosureReason24156', merchant_id='AJHYJHJLYFYGTUHK', mws_auth_token='amzn.mws.d8f2d-6a5f-b42ryurueruio3uio87') parameters = { 'Action': 'CloseOrderReference', 'AmazonOrderReferenceId': 'P01-476238-472642737489', 'ClosureReason': 'testClosureReason24156', 'SellerId': 'AJHYJHJLYFYGTUHK', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b42ryurueruio3uio87'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_list_order_reference(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.list_order_reference( query_id='testSellerOrderId124', query_id_type='SellerOrderId', created_time_range_start='testStart', created_time_range_end='testEnd', sort_order='ascending', page_size=1, merchant_id='AFHRWKJEKJLJKL', mws_auth_token='amzn.mws.d8f2d-6a5f-b42ryurueruio3uio87', order_reference_status_list_filter=['test1', 'test2']) if self.client.region in ('na'): payment_domain = 'NA_USD' elif self.client.region in ('uk', 'gb'): payment_domain = 'EU_GBP' elif self.client.region in ('jp', 'fe'): payment_domain = 'FE_JPY' elif self.client.region in ('eu', 'de', 'fr', 'it', 'es', 'cy'): payment_domain = 'EU_EUR' else: raise ValueError("Error. The current region code does not match our records") parameters = { 'Action': 'ListOrderReference', 'QueryId': 'testSellerOrderId124', 'QueryIdType': 'SellerOrderId', 'PaymentDomain': payment_domain, 'CreatedTimeRange.StartTime': 'testStart', 'CreatedTimeRange.EndTime': 'testEnd', 'SortOrder': 'ascending', 'PageSize': 1, 'SellerId': 'AFHRWKJEKJLJKL', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b42ryurueruio3uio87', 'OrderReferenceStatusListFilter.OrderReferenceStatus.1': 'test1', 'OrderReferenceStatusListFilter.OrderReferenceStatus.2': 'test2'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_list_order_reference_time_check_error(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_generic_error_post self.client.list_order_reference( query_id='testSellerOrderId12444', query_id_type='SellerOrderId', created_time_range_start='testStart', created_time_range_end=None, sort_order=None, page_size=None, merchant_id='AGDJHKWJLHHK', mws_auth_token='amzn.mws.d8f2d-6a5f-b42r23564783492380', order_reference_status_list_filter=None) if self.client.region in ('na'): payment_domain = 'NA_USD' elif self.client.region in ('uk', 'gb'): payment_domain = 'EU_GBP' elif self.client.region in ('jp', 'fe'): payment_domain = 'FE_JPY' elif self.client.region in ('eu', 'de', 'fr', 'it', 'es', 'cy'): payment_domain = 'EU_EUR' else: raise ValueError("Error. The current region code does not match our records") parameters = { 'Action': 'ListOrderReference', 'QueryId': 'testSellerOrderId12444', 'QueryIdType': 'SellerOrderId', 'PaymentDomain': payment_domain, 'SellerId': 'AGDJHKWJLHHK', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b42r23564783492380', 'CreatedTimeRange.StartTime': 'testStart'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_list_order_reference_by_next_token(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.list_order_reference_by_next_token( next_page_token='yrtewyy4823749329482394023940', merchant_id='AHFUHWJELWJELEJW', mws_auth_token='amzn.mws.d8f2d-6a5f-b42r23436248623748') parameters= { 'Action': 'ListOrderReferenceByNextToken', 'NextPageToken': 'yrtewyy4823749329482394023940', 'SellerId': 'AHFUHWJELWJELEJW', 'MWSAuthToken': 'amzn.mws.d8f2d-6a5f-b42r23436248623748'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_authorize(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.authorize( amazon_order_reference_id='P01-351-461238848937', authorization_reference_id='testAuthId123', authorization_amount='1', seller_authorization_note='testAuthNote123', transaction_timeout=0, capture_now=True, soft_descriptor='testSoftDescriptor12', merchant_id='A2AMR0CUYDHYIOW', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b06a-bc3276378843298-fgeswyd') parameters = { 'Action': 'Authorize', 'AmazonOrderReferenceId': 'P01-351-461238848937', 'AuthorizationReferenceId': 'testAuthId123', 'AuthorizationAmount.Amount': '1', 'AuthorizationAmount.CurrencyCode': 'USD', 'SellerAuthorizationNote': 'testAuthNote123', 'TransactionTimeout': '0', 'CaptureNow': 'true', 'SoftDescriptor': 'testSoftDescriptor12', 'SellerId': 'A2AMR0CUYDHYIOW', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b06a-bc3276378843298-fgeswyd'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_authorization_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_authorization_details( amazon_authorization_id='P01-351-461238848937-A42374987239849', merchant_id='AGDFHGWEHGWJH', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b06a-bc412328378') parameters = { 'Action': 'GetAuthorizationDetails', 'AmazonAuthorizationId': 'P01-351-461238848937-A42374987239849', 'SellerId': 'AGDFHGWEHGWJH', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b06a-bc412328378'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_capture(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.capture( amazon_authorization_id='P01-1234567-7654321-A467823648', capture_reference_id='testCaptureRefId123', capture_amount='1', seller_capture_note='testCaptureNote124', soft_descriptor='testSoftDescriptor123', merchant_id='A2AMR8YRGWKHK', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b06a-472637-753648') parameters = { 'Action': 'Capture', 'AmazonAuthorizationId': 'P01-1234567-7654321-A467823648', 'CaptureReferenceId': 'testCaptureRefId123', 'CaptureAmount.Amount': '1', 'CaptureAmount.CurrencyCode': 'USD', 'SellerCaptureNote': 'testCaptureNote124', 'SoftDescriptor': 'testSoftDescriptor123', 'SellerId': 'A2AMR8YRGWKHK', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b06a-472637-753648'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_capture_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_capture_details( amazon_capture_id='P01-4763247-C6472482379', merchant_id='A2AYDGTIQUYOHO', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b645234782374903') parameters = { 'Action': 'GetCaptureDetails', 'AmazonCaptureId': 'P01-4763247-C6472482379', 'SellerId': 'A2AYDGTIQUYOHO', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b645234782374903'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_close_authorization(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.close_authorization( amazon_authorization_id='P01-4763247-A6568472482379', closure_reason='testClosure', merchant_id='A2ATTYIUHBUMTYU', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b645234782374903') parameters = { 'Action': 'CloseAuthorization', 'AmazonAuthorizationId': 'P01-4763247-A6568472482379', 'ClosureReason': 'testClosure', 'SellerId': 'A2ATTYIUHBUMTYU', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b645234782374903'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_refund(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.refund( amazon_capture_id='P01-4763247-C645749', refund_reference_id='testRefundRefId125', refund_amount='1', seller_refund_note='testRefundNote123', soft_descriptor='testSoftDescriptor167', merchant_id='A2ATGUHFHWDJEOPW', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b645234782374903') parameters = { 'Action': 'Refund', 'AmazonCaptureId': 'P01-4763247-C645749', 'RefundReferenceId': 'testRefundRefId125', 'RefundAmount.Amount': '1', 'RefundAmount.CurrencyCode': 'USD', 'SellerRefundNote': 'testRefundNote123', 'SoftDescriptor': 'testSoftDescriptor167', 'SellerId': 'A2ATGUHFHWDJEOPW', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b645234782374903'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_refund_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_refund_details( amazon_refund_id='P01-4763247-R643927483', merchant_id='A2ATGUYIOUHIJL', mws_auth_token='amzn.mws.d6ac8f2d-6a5f-b6447623479') parameters = { 'Action': 'GetRefundDetails', 'AmazonRefundId': 'P01-4763247-R643927483', 'SellerId': 'A2ATGUYIOUHIJL', 'MWSAuthToken': 'amzn.mws.d6ac8f2d-6a5f-b6447623479'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_service_status(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_service_status() parameters = { 'Action': 'GetServiceStatus'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) def test_is_order_reference_id(self): self.assertTrue(self.client.is_order_reference_id('P')) self.assertTrue(self.client.is_order_reference_id('S')) self.assertFalse(self.client.is_order_reference_id('X')) def test_is_billing_agreement_id(self): self.assertTrue(self.client.is_billing_agreement_id('B')) self.assertTrue(self.client.is_billing_agreement_id('C')) self.assertFalse(self.client.is_billing_agreement_id('X')) def test_response_invalid_xml(self): with self.assertRaises(ValueError): PaymentResponse('<invalid></xml>') @patch('requests.post') def test_response_to_xml(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post response = self.client.get_service_status() self.assertTrue(et.fromstring(response.to_xml())) @patch('requests.post') def test_response_to_json(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post response = self.client.get_service_status() self.assertTrue(json.loads(response.to_json())) def test_response_to_json_utf8(self): text = self.response.to_json() utf8_text = '{"test": "الفلانية فلا"}' self.assertEqual(text, utf8_text) @patch('requests.post') def test_response_to_dict(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post response = self.client.get_service_status() self.assertEqual(type(response.to_dict()), dict) @patch('requests.get') def test_get_login_profile(self, mock_urlopen): mock_urlopen.side_effect = self.mock_get_login_profile response = self.client.get_login_profile('access_token', 'client_id') def test_environment_variables(self): os.environ['AP_REGION'] = 'na' os.environ['AP_MWS_ACCESS_KEY'] = 'AP_MWS_ACCESS_KEY' os.environ['AP_MERCHANT_ID'] = 'AP_MERCHANT_ID' os.environ['AP_CURRENCY_CODE'] = 'AP_CURRENCY_CODE' os.environ['AP_MWS_SECRET_KEY'] = 'AP_MWS_SECRET_KEY' client = AmazonPayClient(sandbox=True) self.assertEqual(client.region, 'na') self.assertEqual(client.mws_access_key, 'AP_MWS_ACCESS_KEY') self.assertEqual(client.mws_secret_key, 'AP_MWS_SECRET_KEY') self.assertEqual(client.merchant_id, 'AP_MERCHANT_ID') self.assertEqual(client.currency_code, 'AP_CURRENCY_CODE') os.environ['AP_REGION'] = 'AP_REGION' with self.assertRaises(KeyError): client = AmazonPayClient()
class AmazonPayClientTest(unittest.TestCase): def setUp(self): self.maxDiff = None self.mws_access_key = 'mws_access_key' self.mws_secret_key = 'mws_secret_key' self.merchant_id = 'merchant_id' self.service_version = '2013-01-01' self.mws_endpoint = \ 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/{}'.format( self.service_version) self.client = AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='na', currency_code='test') self.request = PaymentRequest( params={'test': 'test'}, config={'mws_access_key': self.mws_access_key, 'mws_secret_key': self.mws_secret_key, 'api_version': '2013-01-01', 'merchant_id': self.merchant_id, 'mws_endpoint': self.mws_endpoint, 'headers': {'test': 'test'}, 'handle_throttle': True}) self.response = PaymentResponse('<test>test</test>') def mock_requests_post(self, url, data=None, headers=None, verify=False): mock_response = Mock() mock_response.text = '<GetBillingAgreementDetailsResponse>\ <GetBillingAgreementDetailsResult><BillingAgreementDetails>\ <BillingAgreementStatus><State>Draft</State>\ </BillingAgreementStatus></BillingAgreementDetails>\ </GetBillingAgreementDetailsResult>\ </GetBillingAgreementDetailsResponse>' mock_response.status_code = 200 return mock_response def mock_requests_500_post( self, url, data=None, headers=None, verify=False): mock_response = Mock() mock_response.text = '<error>test</error>' mock_response.status_code = 500 return mock_response def mock_requests_generic_error_post( self, url, data=None, headers=None, verify=False): mock_response = Mock() mock_response.text = '<error>test</error>' mock_response.status_code = 502 return mock_response def mock_requests_503_post( self, url, data=None, headers=None, verify=False): mock_response = Mock() mock_response.text = '<error>test</error>' mock_response.status_code = 503 return mock_response def mock_get_login_profile(self, url, headers, params, verify): mock_response = Mock() mock_response.json.return_value = {"aud": "client_id"} mock_response.status_code = 200 return mock_response def test_sandbox_setter(self): self.client.sandbox = False self.assertEqual( self.client._mws_endpoint, 'https://mws.amazonservices.com/OffAmazonPayments/2013-01-01') self.client.sandbox = True self.assertEqual( self.client._mws_endpoint, 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/2013-01-01') def test_sanitize_response_data(self): current_file_dir = os.path.dirname(__file__) test_file_path = os.path.join(current_file_dir, "log.txt") f = open(test_file_path, "r") source_text = f.read() f.close() text = self.request._sanitize_response_data(source_text) test_file_path = os.path.join(current_file_dir, "sanlog.txt") f = open(test_file_path, "r") san_text = f.read() f.close self.assertEqual(text, san_text) def test_region_exception(self): with self.assertRaises(KeyError): AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='should_throw_exception', currency_code='test') def test_set_endpoint(self): self.client._set_endpoint() self.assertEqual( self.client._mws_endpoint, 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/2013-01-01') def test_sign(self): test_signature = self.request._sign('my_test_string') self.assertEqual( test_signature, 'JQZYxe8EFlLE3XCAWotsn329rpZF7OFYhA8oo7rUV2E=') def test_application_settings(self): client = AmazonPayClient( mws_access_key=self.mws_access_key, mws_secret_key=self.mws_secret_key, merchant_id=self.merchant_id, handle_throttle=False, sandbox=True, region='na', currency_code='test', application_name='test_application', application_version='test_application_version') self.assertEqual(client.application_name, 'test_application') self.assertEqual( client.application_version, 'test_application_version') def test_properties(self): self.assertEqual(self.client.mws_access_key, 'mws_access_key') self.assertEqual(self.client.mws_secret_key, 'mws_secret_key') self.assertEqual(self.client.merchant_id, 'merchant_id') self.assertEqual(self.client._region_code, 'na') self.assertEqual(self.client.currency_code, 'test') self.assertEqual(self.client.handle_throttle, False) self.assertEqual(self.client.sandbox, True) @patch('requests.post') def test_generic_error_response(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_generic_error_post self.request.send_post() response = self.request.response self.assertEqual(type(response), PaymentErrorResponse) @patch('requests.post') def test_500_response(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_500_post self.request.send_post() response = self.request.response.to_dict() self.assertEqual(response['error'], '500') @patch('requests.post') def test_503_response(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_503_post self.request.send_post() response = self.request.response.to_dict() self.assertEqual(response['error'], '503') @patch('requests.post') def test_headers(self, mock_urlopen): py_version = ".".join(map(str, sys.version_info[:3])) mock_urlopen.side_effect = self.mock_requests_post self.client.get_service_status() if sys.version_info[0] == 3 and sys.version_info[1] >= 2: py_valid = True header_expected = { 'Content-Type': 'application/x-www-form-urlencoded', "User-Agent":'amazon-pay-sdk-python/{0} ({1}Python/{2}; {3}/{4})'.format( str(ap_version.versions['application_version']), (''), py_version, str(platform.system()), str(platform.release()) ) } self.assertEqual(mock_urlopen.call_args[1]['headers'], header_expected) self.assertTrue(py_valid, True) @patch('requests.post') def test_create_order_reference_for_id(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.create_order_reference_for_id( object_id='test', object_id_type='test', order_total='test', inherit_shipping_address=False, confirm_now=True, platform_id='test', seller_note='test', seller_order_id='test', store_name='test', custom_information='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'CreateOrderReferenceForId', 'Id': 'test', 'IdType': 'test', 'OrderTotal.Amount': 'test', 'OrderTotal.CurrencyCode': 'test', 'InheritShippingAddress': 'false', 'ConfirmNow': 'true', 'PlatformId': 'test', 'SellerNote': 'test', 'SellerOrderId': 'test', 'StoreName': 'test', 'CustomInformation': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_billing_agreement_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_billing_agreement_details( amazon_billing_agreement_id='test', address_consent_token='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'GetBillingAgreementDetails', 'AmazonBillingAgreementId': 'test', 'AddressConsentToken': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_set_billing_agreement_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.set_billing_agreement_details( amazon_billing_agreement_id='test', platform_id='test', seller_note='test', seller_billing_agreement_id='test', store_name='test', custom_information='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'SetBillingAgreementDetails', 'AmazonBillingAgreementId': 'test', 'BillingAgreementAttributes.PlatformId': 'test', 'BillingAgreementAttributes.SellerNote': 'test', 'BillingAgreementAttributes.SellerBillingAgreementAttributes.SellerBillingAgreementId': 'test', 'BillingAgreementAttributes.SellerBillingAgreementAttributes.StoreName': 'test', 'BillingAgreementAttributes.SellerBillingAgreementAttributes.CustomInformation': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_confirm_billing_agreement(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.confirm_billing_agreement( amazon_billing_agreement_id='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'ConfirmBillingAgreement', 'AmazonBillingAgreementId': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_validate_billing_agreement(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.validate_billing_agreement( amazon_billing_agreement_id='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'ValidateBillingAgreement', 'AmazonBillingAgreementId': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_authorize_on_billing_agreement(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.authorize_on_billing_agreement( amazon_billing_agreement_id='test', authorization_reference_id='test', authorization_amount='test', seller_authorization_note='test', transaction_timeout=0, capture_now=True, soft_descriptor='test', seller_note='test', platform_id='test', seller_order_id='test', store_name='test', custom_information='test', inherit_shipping_address=False, merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'AuthorizeOnBillingAgreement', 'AmazonBillingAgreementId': 'test', 'TransactionTimeout': '0', 'AuthorizationReferenceId': 'test', 'AuthorizationAmount.Amount': 'test', 'AuthorizationAmount.CurrencyCode': 'test', 'CaptureNow': 'true', 'SellerAuthorizationNote': 'test', 'SoftDescriptor': 'test', 'SellerNote': 'test', 'PlatformId': 'test', 'InheritShippingAddress': 'false', 'SellerOrderAttributes.SellerOrderId': 'test', 'SellerOrderAttributes.StoreName': 'test', 'SellerOrderAttributes.CustomInformation': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_close_billing_agreement(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.close_billing_agreement( amazon_billing_agreement_id='test', closure_reason='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'CloseBillingAgreement', 'AmazonBillingAgreementId': 'test', 'ClosureReason': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_set_order_reference_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.set_order_reference_details( amazon_order_reference_id='test', order_total='test', platform_id='test', seller_note='test', seller_order_id='test', store_name='test', custom_information='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'SetOrderReferenceDetails', 'AmazonOrderReferenceId': 'test', 'OrderReferenceAttributes.OrderTotal.Amount': 'test', 'OrderReferenceAttributes.OrderTotal.CurrencyCode': 'test', 'OrderReferenceAttributes.PlatformId': 'test', 'OrderReferenceAttributes.SellerNote': 'test', 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId': 'test', 'OrderReferenceAttributes.SellerOrderAttributes.StoreName': 'test', 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_order_reference_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_order_reference_details( amazon_order_reference_id='test', address_consent_token='test', access_token='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'GetOrderReferenceDetails', 'AmazonOrderReferenceId': 'test', 'AddressConsentToken': 'test', 'AccessToken': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_confirm_order_reference(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.confirm_order_reference( amazon_order_reference_id='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'ConfirmOrderReference', 'AmazonOrderReferenceId': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_cancel_order_reference(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.cancel_order_reference( amazon_order_reference_id='test', cancelation_reason='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'CancelOrderReference', 'AmazonOrderReferenceId': 'test', 'CancelationReason': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_close_order_reference(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.close_order_reference( amazon_order_reference_id='test', closure_reason='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'CloseOrderReference', 'AmazonOrderReferenceId': 'test', 'ClosureReason': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_authorize(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.authorize( amazon_order_reference_id='test', authorization_reference_id='test', authorization_amount='test', seller_authorization_note='test', transaction_timeout=0, capture_now=True, soft_descriptor='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'Authorize', 'AmazonOrderReferenceId': 'test', 'TransactionTimeout': '0', 'AuthorizationReferenceId': 'test', 'AuthorizationAmount.Amount': 'test', 'AuthorizationAmount.CurrencyCode': 'test', 'SellerAuthorizationNote': 'test', 'TransactionTimeout': '0', 'CaptureNow': 'true', 'SoftDescriptor': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_authorization_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_authorization_details( amazon_authorization_id='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'GetAuthorizationDetails', 'AmazonAuthorizationId': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_capture(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.capture( amazon_authorization_id='test', capture_reference_id='test', capture_amount='test', seller_capture_note='test', soft_descriptor='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'Capture', 'AmazonAuthorizationId': 'test', 'CaptureReferenceId': 'test', 'CaptureAmount.Amount': 'test', 'CaptureAmount.CurrencyCode': 'test', 'SellerCaptureNote': 'test', 'SoftDescriptor': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_capture_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_capture_details( amazon_capture_id='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'GetCaptureDetails', 'AmazonCaptureId': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_close_authorization(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.close_authorization( amazon_authorization_id='test', closure_reason='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'CloseAuthorization', 'AmazonAuthorizationId': 'test', 'ClosureReason': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_refund(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.refund( amazon_capture_id='test', refund_reference_id='test', refund_amount='test', seller_refund_note='test', soft_descriptor='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'Refund', 'AmazonCaptureId': 'test', 'RefundReferenceId': 'test', 'RefundAmount.Amount': 'test', 'RefundAmount.CurrencyCode': 'test', 'SellerRefundNote': 'test', 'SoftDescriptor': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_refund_details(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_refund_details( amazon_refund_id='test', merchant_id='test', mws_auth_token='test') parameters = { 'Action': 'GetRefundDetails', 'AmazonRefundId': 'test', 'SellerId': 'test', 'MWSAuthToken': 'test'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) @patch('requests.post') def test_get_service_status(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post self.client.get_service_status() parameters = { 'Action': 'GetServiceStatus'} data_expected = self.request._querystring(parameters) self.assertEqual(mock_urlopen.call_args[1]['data'], data_expected) def test_is_order_reference_id(self): self.assertTrue(self.client.is_order_reference_id('P')) self.assertTrue(self.client.is_order_reference_id('S')) self.assertFalse(self.client.is_order_reference_id('X')) def test_is_billing_agreement_id(self): self.assertTrue(self.client.is_billing_agreement_id('B')) self.assertTrue(self.client.is_billing_agreement_id('C')) self.assertFalse(self.client.is_billing_agreement_id('X')) def test_response_invalid_xml(self): with self.assertRaises(ValueError): PaymentResponse('<invalid></xml>') @patch('requests.post') def test_response_to_xml(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post response = self.client.get_service_status() self.assertTrue(et.fromstring(response.to_xml())) @patch('requests.post') def test_response_to_json(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post response = self.client.get_service_status() self.assertTrue(json.loads(response.to_json())) @patch('requests.post') def test_response_to_dict(self, mock_urlopen): mock_urlopen.side_effect = self.mock_requests_post response = self.client.get_service_status() self.assertEqual(type(response.to_dict()), dict) @patch('requests.get') def test_get_login_profile(self, mock_urlopen): mock_urlopen.side_effect = self.mock_get_login_profile response = self.client.get_login_profile('access_token', 'client_id') def test_environment_variables(self): os.environ['AP_REGION'] = 'na' os.environ['AP_MWS_ACCESS_KEY'] = 'AP_MWS_ACCESS_KEY' os.environ['AP_MERCHANT_ID'] = 'AP_MERCHANT_ID' os.environ['AP_CURRENCY_CODE'] = 'AP_CURRENCY_CODE' os.environ['AP_MWS_SECRET_KEY'] = 'AP_MWS_SECRET_KEY' client = AmazonPayClient(sandbox=True) self.assertEqual(client.region, 'na') self.assertEqual(client.mws_access_key, 'AP_MWS_ACCESS_KEY') self.assertEqual(client.mws_secret_key, 'AP_MWS_SECRET_KEY') self.assertEqual(client.merchant_id, 'AP_MERCHANT_ID') self.assertEqual(client.currency_code, 'AP_CURRENCY_CODE') os.environ['AP_REGION'] = 'AP_REGION' with self.assertRaises(KeyError): client = AmazonPayClient()