def accept_adaptive_payment(self, data): """Accept a payment IPN coming through the Adaptive API. Paypal sends out two kind of IPNs when using Adaptive. The payment initiator (HiiCart) will get an Adaptive-specific IPN relating to the overall state of the payment. HiiCart may also receive normal Paypal IPNs if the user has their IPN url pointing here.""" self.log.debug("IPN for cart %s received" % data["tracking_id"]) cart = HiiCart.objects.get(_cart_uuid=data["tracking_id"]) for i in range(6): key_base = "transaction[%i]." % i if not any([k.startswith(key_base) for k in data.keys()]): break p = Payment.objects.filter(transaction_id=data[key_base + "id"]) if len(p) == 0: p = Payment(cart=cart, gateway=cart.gateway, transaction_id=data[key_base + "id"]) else: p = p[0] amount = re.sub("[^0-9.]", "", data[key_base + "amount"]) p.amount = Decimal(amount) p.state = _adaptive_states[data[key_base + "status"]] p.save() # Need .id if creating new payment p.notes.create(text="Payment receiver: %s" % data[key_base + "receiver"]) if "memo" in data: cart.notes.create(text="IPN Memo: %s" % data["memo"]) cart.update_state()
def _create_payment(self, cart, amount, transaction_id, state): """Record a payment.""" pmnt = Payment(amount=amount, gateway=self.name, cart=cart, state=state, transaction_id=transaction_id) pmnt.save() return pmnt