def capture_payment(self, amount=NOTSET): """Make a direct payment without a prior authorization, using the existing pending payment if found.""" self.amount = amount self.pending = self.purchase.get_pending(self.key, raises=False) if self.pending: self.payment = self.pending.capture log.debug("Using linked payment: %s", self.payment) self.payment.success = True if amount == NOTSET: self.set_amount_from_pending() else: log.debug("No pending %s payments for %s", self.key, self.purchase) self.payment = Payment( purchase=self.purchase, method=self.key, success=True) log.debug("Recorded %s payment of %s for #%s", self.key, self.amount, self.purchase.orderno) self.cleanup() return self.payment
def make_test_purchase(payment={}, **kwargs): purchaseargs = { "first_name": 'Mister', "last_name": 'Tester', "orderno" : "test%03i" % random.randrange(1,100), "email" : "*****@*****.**", "phone" : "555-555-1234", "ship_street1" : "123 Test St.", "ship_city" : "Testington", "ship_state" : "TX", "ship_postal_code" : "55555", "ship_country" : "US", "bill_street1" : "123 Test St.", "bill_city" : "Testington", "bill_state" : "TX", "bill_postal_code" : "55555", "bill_country" : "US", "tax" : Decimal("0"), "shipping" : Decimal("0"), } purchaseargs.update(kwargs) purchase = Purchase(**purchaseargs) purchase.recalc() purchase.save() if payment: for f in ('card_type', 'expire_month', 'expire_year', 'card_number', 'ccv'): assert(f in payment) p = Payment(purchase = purchase, amount = purchase.total) p.save() c = CreditCardDetail( payment=p, credit_type=payment['card_type'], expire_month=payment['expire_month'], expire_year=payment['expire_year'], ) if 'card_holder' in payment: c.card_holder = payment['card_holder'] c.storeCC(payment['card_number']) c.ccv = payment['ccv'] c.save() return purchase
def authorize_payment(self, amount=NOTSET): """Make an authorization, using the existing pending payment if found""" self.amount = amount log.debug("Recording %s authorization of %s for #%s", self.key, self.amount, self.purchase.orderno) self.pending = self.purchase.get_pending(self.key, raises=False) if self.pending: self.payment = Authorization() self.payment.capture = self.pending.capture if amount == NOTSET: self.set_amount_from_pending() else: log.debug("No pending %s authorizations for %s", self.key, self.purchase) self.payment = Authorization(purchase=self.purchase, method=self.key) self.cleanup() return self.payment
def authorize_payment(self, amount=NOTSET): """Make an authorization, using the existing pending payment if found""" self.amount = amount log.debug("Recording %s authorization of %s for #%s", self.key, self.amount, self.purchase.orderno) self.pending = self.purchase.get_pending(self.key, raises=False) if self.pending: self.payment = Authorization() self.payment.capture = self.pending.capture if amount == NOTSET: self.set_amount_from_pending() else: log.debug("No pending %s authorizations for %s", self.key, self.purchase) self.payment = Authorization( purchase=self.purchase, method=self.key) self.cleanup() return self.payment
class PaymentRecorder(object): """Manages proper recording of pending payments, payments, and authorizations.""" def __init__(self, purchase, key): self.purchase = purchase self.key = key self._amount = NOTSET self.transaction_id = "" self.reason_code = "" self.payment = None self.pending = None def _set_amount(self, amount): if amount != NOTSET: self._amount = amount def _get_amount(self): if self._amount == NOTSET: return self.purchase.total else: return self._amount amount = property(fset=_set_amount, fget=_get_amount) def authorize_payment(self, amount=NOTSET): """Make an authorization, using the existing pending payment if found""" self.amount = amount log.debug("Recording %s authorization of %s for #%s", self.key, self.amount, self.purchase.orderno) self.pending = self.purchase.get_pending(self.key, raises=False) if self.pending: self.payment = Authorization() self.payment.capture = self.pending.capture if amount == NOTSET: self.set_amount_from_pending() else: log.debug("No pending %s authorizations for %s", self.key, self.purchase) self.payment = Authorization( purchase=self.purchase, method=self.key) self.cleanup() return self.payment def capture_authorized_payment(self, authorization, amount=NOTSET): """Convert an authorization into a payment.""" self.amount = amount log.debug("Recording %s capture of authorization #%i for #%s", self.key, authorization.id, authorization.purchase.orderno) self.payment = authorization.capture self.payment.success=True self.payment.save() self.cleanup() return self.payment def make_refund(self, amount=NOTSET): self.amount = amount self.refund = Refund(amount=amount, purchase=self.purchase, reason_code=self.reason_code, method=self.key, transaction_id=self.transaction_id ) self.refund.save() return self.refund def capture_payment(self, amount=NOTSET): """Make a direct payment without a prior authorization, using the existing pending payment if found.""" self.amount = amount self.pending = self.purchase.get_pending(self.key, raises=False) if self.pending: self.payment = self.pending.capture log.debug("Using linked payment: %s", self.payment) self.payment.success = True if amount == NOTSET: self.set_amount_from_pending() else: log.debug("No pending %s payments for %s", self.key, self.purchase) self.payment = Payment( purchase=self.purchase, method=self.key, success=True) log.debug("Recorded %s payment of %s for #%s", self.key, self.amount, self.purchase.orderno) self.cleanup() return self.payment def record_failure(self, amount=NOTSET, details="", authorization=None): log.info('Recording a payment failure: purchase #%s order #%s, code %s\nmessage=%s', self.purchase, self.purchase.orderno, self.reason_code, details) self.amount = amount failure = PaymentFailure.objects.create(purchase=self.purchase, details=details, transaction_id=self.transaction_id, amount = self.amount, method = self.key, reason_code = self.reason_code ) return failure def cleanup(self): if self.pending: pending = self.pending self.payment.capture = pending.capture self.payment.purchase = pending.purchase self.payment.method = pending.method self.payment.details = pending.details # delete any extra pending payments for p in self.purchase.paymentspending.all(): if p != pending and p.capture.transaction_id=='LINKED': p.capture.delete() p.delete() self.payment.reason_code=self.reason_code self.payment.transaction_id=self.transaction_id self.payment.amount=self.amount self.payment.time_stamp = datetime.now() self.payment.save() purchase = self.payment.purchase signals.payment_complete.send(sender='bursar', purchase=self.purchase, payment=self.payment) log.debug('cleanup details: %s', self.payment) def create_pending(self, amount=NOTSET): """Create a placeholder payment entry for the purchase. This is done by step 2 of the payment process.""" if amount == NOTSET: amount = self.purchase.remaining self.amount = amount pendings = self.purchase.paymentspending ct = pendings.count() if ct > 0: log.debug("Deleting %i expired pending payment entries for order #%s", ct, self.purchase.orderno) for pending in pendings.all(): if pending.capture.transaction_id=='LINKED': pending.capture.delete() pending.delete() log.debug("Creating pending %s payment of %s for %s", self.key, amount, self.purchase) self.pending = PaymentPending.objects.create(purchase=self.purchase, amount=amount, method=self.key) return self.pending def set_amount_from_pending(self): """Try to figure out how much to charge. If it is set on the "pending" charge use that otherwise use the purchase total.""" amount = self.pending.amount # otherwise use the purchase total. if amount == Decimal('0.00'): amount = self.purchase.total log.debug('Settings amount from pending=%s', self.purchase.total) self.amount = amount
class PaymentRecorder(object): """Manages proper recording of pending payments, payments, and authorizations.""" def __init__(self, purchase, key): self.purchase = purchase self.key = key self._amount = NOTSET self.transaction_id = "" self.reason_code = "" self.payment = None self.pending = None def _set_amount(self, amount): if amount != NOTSET: self._amount = amount def _get_amount(self): if self._amount == NOTSET: return self.purchase.total else: return self._amount amount = property(fset=_set_amount, fget=_get_amount) def authorize_payment(self, amount=NOTSET): """Make an authorization, using the existing pending payment if found""" self.amount = amount log.debug("Recording %s authorization of %s for #%s", self.key, self.amount, self.purchase.orderno) self.pending = self.purchase.get_pending(self.key, raises=False) if self.pending: self.payment = Authorization() self.payment.capture = self.pending.capture if amount == NOTSET: self.set_amount_from_pending() else: log.debug("No pending %s authorizations for %s", self.key, self.purchase) self.payment = Authorization( purchase=self.purchase, method=self.key) self.cleanup() return self.payment def capture_authorized_payment(self, authorization, amount=NOTSET): """Convert an authorization into a payment.""" self.amount = amount log.debug("Recording %s capture of authorization #%i for #%s", self.key, authorization.id, authorization.purchase.orderno) self.payment = authorization.capture self.payment.success=True self.payment.save() self.cleanup() return self.payment def capture_payment(self, amount=NOTSET): """Make a direct payment without a prior authorization, using the existing pending payment if found.""" self.amount = amount self.pending = self.purchase.get_pending(self.key, raises=False) if self.pending: self.payment = self.pending.capture log.debug("Using linked payment: %s", self.payment) self.payment.success = True if amount == NOTSET: self.set_amount_from_pending() else: log.debug("No pending %s payments for %s", self.key, self.purchase) self.payment = Payment( purchase=self.purchase, method=self.key, success=True) log.debug("Recorded %s payment of %s for #%s", self.key, self.amount, self.purchase.orderno) self.cleanup() return self.payment def record_failure(self, amount=NOTSET, details="", authorization=None): log.info('Recording a payment failure: purchase #%s order #%s, code %s\nmessage=%s', self.purchase, self.purchase.orderno, self.reason_code, details) self.amount = amount failure = PaymentFailure.objects.create(purchase=self.purchase, details=details, transaction_id=self.transaction_id, amount = self.amount, method = self.key, reason_code = self.reason_code ) return failure def cleanup(self): if self.pending: pending = self.pending self.payment.capture = pending.capture self.payment.purchase = pending.purchase self.payment.method = pending.method self.payment.details = pending.details # delete any extra pending payments for p in self.purchase.paymentspending.all(): if p != pending and p.capture.transaction_id=='LINKED': p.capture.delete() p.delete() self.payment.reason_code=self.reason_code self.payment.transaction_id=self.transaction_id self.payment.amount=self.amount self.payment.time_stamp = datetime.now() self.payment.save() purchase = self.payment.purchase signals.payment_complete.send(sender='bursar', purchase=self.purchase, payment=self.payment) log.debug('cleanup details: %s', self.payment) def create_pending(self, amount=NOTSET): """Create a placeholder payment entry for the purchase. This is done by step 2 of the payment process.""" if amount == NOTSET: amount = self.purchase.remaining self.amount = amount pendings = self.purchase.paymentspending ct = pendings.count() if ct > 0: log.debug("Deleting %i expired pending payment entries for order #%s", ct, self.purchase.orderno) for pending in pendings.all(): if pending.capture.transaction_id=='LINKED': pending.capture.delete() pending.delete() log.debug("Creating pending %s payment of %s for %s", self.key, amount, self.purchase) self.pending = PaymentPending.objects.create(purchase=self.purchase, amount=amount, method=self.key) return self.pending def set_amount_from_pending(self): """Try to figure out how much to charge. If it is set on the "pending" charge use that otherwise use the purchase total.""" amount = self.pending.amount # otherwise use the purchase total. if amount == Decimal('0.00'): amount = self.purchase.total log.debug('Settings amount from pending=%s', self.purchase.total) self.amount = amount