def refund(self, refund_amount=None): "refund the last transaction" if len(self.cart.payments.all()) == 0: return # get the id of the last payment for refunding payments = self.cart.payments.order_by('-last_updated') if not payments: return payment = payments[0] transaction_id = payment.transaction_id if refund_amount and isinstance(refund_amount, (long, float, int)): fps.do_fps("Refund", "GET", self.settings, TransactionId=transaction_id, RefundAmount=refund_amount, CallerReference=uuid4().hex) else: fps.do_fps("Refund", "GET", self.settings, TransactionId=transaction_id, CallerReference=uuid4().hex) payment.state = 'REFUND' payment.save()
def cancel_recurring(self): "Cancel recurring lineitem." if len(self.cart.recurring_lineitems) == 0: return item = self.cart.recurring_lineitems[0] token = item.payment_token fps.do_fps("CancelToken", "GET", self.settings, TokenId=token) item.is_active = False item.save() self.cart.update_state() return CancelResult(None)
def verify_signature(self, raw_data, http_method, endpoint_uri): self._update_with_store_settings() response = fps.do_fps("VerifySignature", http_method, self.settings, UrlEndPoint=endpoint_uri, HttpParameters=raw_data) xml = ET.XML(response) el = xml.find(".//{http://fps.amazonaws.com/doc/2008-09-17/}VerificationStatus") return el is not None and el.text == "Success"
def make_pay_request(self, token, caller_reference=None): """ Make a Pay request for the purchase with the given token. Return status received from Amazon. """ self._update_with_store_settings() if caller_reference is None: caller_reference = self.cart.cart_uuid self.log.debug("Doing FPS: %s" % token) response = fps.do_fps("Pay", "GET", self.settings, CallerReference=caller_reference, SenderTokenId=token, **{"TransactionAmount.CurrencyCode": "USD", "TransactionAmount.Value": self.cart.total}) xml = ET.XML(response) self.log.debug("FPS response: %s" % xml) status = xml.find(".//%sTransactionStatus" % _FPS_NS) self.log.debug("FPS status: %s" % status) if status is None: error = self._process_error(response) if not error: msg = "Pay request failed for purchase '%s' with response: %s" % ( self.cart, response) else: msg = "Pay request failed for purchase '%s' with code/message: '%s' '%s' (request id: '%s')" % ( self.cart, error[0], error[1], error[2]) self.log.warn(msg) self.cart.notes.create(text=msg) # An error code, maybe? code = xml.find(".//Code") if code is not None: return code.text return None else: # This work for pending payments? transaction_id = xml.find(".//%sTransactionId" % _FPS_NS).text self.log.debug('FPS trans id: %s' % transaction_id) if status.text == "Pending": self._create_payment(self.cart.total, transaction_id, "PENDING") elif status.text == "Success": pending = self.cart.payments.filter(transaction_id=transaction_id, state="PENDING") if pending: pending[0].state = "PAID" pending[0].save() else: self._create_payment(self.cart.total, transaction_id, "PAID") self.begin_recurring() else: # Cancelled or Failure msg = "Pay request for purchase '%s' returned status '%s'. \nFull response: %s" % ( self.cart, status.text, response) self.log.warn(msg) self.cart.notes.create(text=msg) self.cart.update_state() return status.text
def cancel_recurring(self, cart): """Cancel recurring lineitem.""" if cart.recurringlineitems.count() == 0: return self._update_with_cart_settings(cart) item = cart.recurringlineitems.all()[0] token = item.payment_token response = fps.do_fps("CancelToken", "GET", self.settings, TokenId=token) item.is_active = False item.save() cart.update_state()
def verify_signature(self, raw_data, http_method, endpoint_uri): self._update_with_store_settings() response = fps.do_fps("VerifySignature", http_method, self.settings, UrlEndPoint=endpoint_uri, HttpParameters=raw_data) xml = ET.XML(response) el = xml.find( ".//{http://fps.amazonaws.com/doc/2008-09-17/}VerificationStatus") return el is not None and el.text == "Success"
def make_pay_request(self, token, caller_reference=None): """ Make a Pay request for the purchase with the given token. Return status received from Amazon. """ self._update_with_store_settings() if caller_reference is None: caller_reference = self.cart.cart_uuid self.log.debug("Doing FPS: %s" % token) response = fps.do_fps("Pay", "GET", self.settings, CallerReference=caller_reference, SenderTokenId=token, **{ "TransactionAmount.CurrencyCode": "USD", "TransactionAmount.Value": self.cart.total }) xml = ET.XML(response) self.log.debug("FPS response: %s" % xml) status = xml.find(".//%sTransactionStatus" % _FPS_NS) self.log.debug("FPS status: %s" % status) if status is None: error = self._process_error(response) if not error: msg = "Pay request failed for purchase '%s' with response: %s" % ( self.cart, response) else: msg = "Pay request failed for purchase '%s' with code/message: '%s' '%s' (request id: '%s')" % ( self.cart, error[0], error[1], error[2]) self.log.warn(msg) self.cart.notes.create(text=msg) # An error code, maybe? code = xml.find(".//Code") if code is not None: return code.text return None else: # This work for pending payments? transaction_id = xml.find(".//%sTransactionId" % _FPS_NS).text self.log.debug('FPS trans id: %s' % transaction_id) if status.text == "Pending": self._create_payment(self.cart.total, transaction_id, "PENDING") elif status.text == "Success": pending = self.cart.payments.filter( transaction_id=transaction_id, state="PENDING") if pending: pending[0].state = "PAID" pending[0].save() else: self._create_payment(self.cart.total, transaction_id, "PAID") self.begin_recurring() else: # Cancelled or Failure msg = "Pay request for purchase '%s' returned status '%s'. \nFull response: %s" % ( self.cart, status.text, response) self.log.warn(msg) self.cart.notes.create(text=msg) self.cart.update_state() return status.text