def complete(self): self.completed = True if not len(self): logger.warn('Completing contract on empty order!') try: self._add_items() except: self.completed = False raise try: params = { 'PurchaseContractId': self.id, 'Charges.Tax.Amount': str(self.order.tax), 'Charges.Tax.CurrencyCode': 'USD', 'Charges.Shipping.Amount': str(self.order.shipping), 'Charges.Shipping.CurrencyCode': 'USD', } make_request('POST', 'SetContractCharges', params, self.settings) order_ids, request_id = make_request( 'POST', 'CompletePurchaseContract', {'PurchaseContractId': self.id,}, self.settings, ) return order_ids except: self.completed = False raise
def _add_items(self): params = { 'PurchaseContractId': self.id, } for i, item in enumerate(self.order.items): index = i+1 # Amazon wants the first index non-zero key_base = 'PurchaseItems.PurchaseItem.{0}.'.format(index) dict = item.as_dict() logger.debug('Item: {0}, .as_dict() = {1}'.format(item, dict)) for k, v in dict.iteritems(): params[key_base+k] = unicode(v) params[key_base+'MerchantId'] = self.settings['merchant-id'] make_request('POST', 'SetPurchaseItems', params, self.settings)
def update(self): params = { 'PurchaseContractId': self.id, } contract_list, request_id = make_request('GET', 'GetPurchaseContract', params, self.settings) contract = contract_list[0] assert contract.Id.text == self.id self.state = contract.State.text self.merchant_id = contract.MerchantId.text self.marketplace_id = contract.MarketplaceId.text self.expires = datetime.datetime.strptime( contract.ExpirationTimeStamp.text, '%Y-%m-%dT%H:%M:%S.%fZ', ) try: for dest in contract.Destinations.Destination[:]: address = dest.PhysicalDestinationAttributes.ShippingAddress self.add_destination( dest_name = dest.DestinationName.text, name = address.Name.text, address = [], # ??? Didn't get an address from Amazon while testing this. city = address.City.text, state = address.StateOrProvinceCode.text, zip = address.PostalCode.text, country_code = address.CountryCode.text, phone = address.PhoneNumber.text, ) except AttributeError: pass # No destinations chosen yet
def __init__(self, id=None, settings={}): self.settings = dict(settings) if id is None: id_list, request_id = make_request( 'POST', 'CreatePurchaseContract', { 'DirectedId': '', 'AuthorizationToken': '', }, settings, ) self.id = id_list[0] else: self.id = id self.destinations = {} self.order = Order() self.completed = False self.update()