Exemple #1
0
    def set_offer(self, offer_amount):
        offer_amount = Decimal(offer_amount)
        # refund any previous offers
        previous_offers = self.offers.exclude(
            charge_id=u''
        ).filter(
            refund_id=u''
        )
        if not previous_offers:
            self.offer = offer_amount
            self.save()
        else:
            for offer in previous_offers:
                payments.refund(offer)

        new_offer = Offer(
            user=self.user,
            amount=offer_amount,
            bid=self,
        )
        new_offer.save()
        payments.authorize(new_offer)
        self.offer = offer_amount
        self.save()
        return new_offer
Exemple #2
0
 def save(self, *args, **kwargs):
     is_new = not self.pk
     super(Claim, self).save(*args, **kwargs)
     if is_new:
         # refund any authorize offers by this user
         users_offers = Offer.objects.filter(bid__issue=self.issue,
                                             user=self.user,
                                             refund_id=u'',
                                             charge_id__isnull=False)
         for offer in users_offers:
             payments.refund(offer)
Exemple #3
0
 def save(self, *args, **kwargs):
     is_new = not self.pk
     super(Claim, self).save(*args, **kwargs)
     if is_new:
         # refund any authorize offers by this user
         users_offers = Offer.objects.filter(
             bid__issue=self.issue,
             user=self.user,
             refund_id=u'',
             charge_id__isnull=False
         )
         for offer in users_offers:
             payments.refund(offer)
Exemple #4
0
    def payout(self):
        try:
            # get all authorized offers for this issue
            valid_offers = Offer.objects.filter(
                bid__issue=self.issue,
                charge_id__isnull=False,
                refund_id=u'',
            ).exclude(
                user=self.user
            )
            # check on a surplus
            # TODO: move this to the payments utils
            sum_offers = valid_offers.aggregate(Sum('amount'))['amount__sum']
            users_ask = self.ask
            offer_adjustment = 1
            if sum_offers > users_ask:
                # surplus is the amount of offers over ask
                surplus = sum_offers - users_ask
                # the claim bonus is the claimaints share of the surplus
                claim_bonus = (surplus / (valid_offers.count() + 1))
                # giveback is the aount to distribute among the offerers
                offer_giveback = surplus - claim_bonus
                # this is the percent of the original payout to be charged
                offer_adjustment = 1 - (offer_giveback / sum_offers)

            for offer in valid_offers:
                adjusted_offer_amount = (offer.amount * offer_adjustment)
                discount_amount = offer.amount - adjusted_offer_amount
                payments.refund(offer)
                # create final adjusted offer
                new_offer = Offer(
                    user=offer.user,
                    bid=offer.bid,
                    amount=adjusted_offer_amount,
                    discount=discount_amount
                )
                new_offer.save()
                # capture payment to this users account
                payout = Payout(
                    user=offer.user,
                    claim=self,
                    amount=adjusted_offer_amount,
                    discount=discount_amount
                )
                payout.save()

                payments.charge(offer, payout)
            return True
        except Exception as e:
            print 'payout error: %s' % e.message
            return False
Exemple #5
0
    def payout(self):
        try:
            # get all authorized offers for this issue
            valid_offers = Offer.objects.filter(
                bid__issue=self.issue,
                charge_id__isnull=False,
                refund_id=u'',
            ).exclude(user=self.user)
            # check on a surplus
            # TODO: move this to the payments utils
            sum_offers = valid_offers.aggregate(Sum('amount'))['amount__sum']
            users_ask = self.ask
            offer_adjustment = 1
            if sum_offers > users_ask:
                # surplus is the amount of offers over ask
                surplus = sum_offers - users_ask
                # the claim bonus is the claimaints share of the surplus
                claim_bonus = (surplus / (valid_offers.count() + 1))
                # giveback is the aount to distribute among the offerers
                offer_giveback = surplus - claim_bonus
                # this is the percent of the original payout to be charged
                offer_adjustment = 1 - (offer_giveback / sum_offers)

            for offer in valid_offers:
                adjusted_offer_amount = (offer.amount * offer_adjustment)
                discount_amount = offer.amount - adjusted_offer_amount
                payments.refund(offer)
                # create final adjusted offer
                new_offer = Offer(user=offer.user,
                                  bid=offer.bid,
                                  amount=adjusted_offer_amount,
                                  discount=discount_amount)
                new_offer.save()
                # capture payment to this users account
                payout = Payout(user=offer.user,
                                claim=self,
                                amount=adjusted_offer_amount,
                                discount=discount_amount)
                payout.save()

                payments.charge(offer, payout)
            return True
        except Exception as e:
            print 'payout error: %s' % e.message
            return False
Exemple #6
0
    def set_offer(self, offer_amount):
        offer_amount = Decimal(offer_amount)
        # refund any previous offers
        previous_offers = self.offers.exclude(charge_id=u'').filter(
            refund_id=u'')
        if not previous_offers:
            self.offer = offer_amount
            self.save()
        else:
            for offer in previous_offers:
                payments.refund(offer)

        new_offer = Offer(
            user=self.user,
            amount=offer_amount,
            bid=self,
        )
        new_offer.save()
        payments.authorize(new_offer)
        self.offer = offer_amount
        self.save()
        return new_offer