Пример #1
0
    def test_refund_created_payment(self):
        order = OrderFactory.create()
        order_payment = OrderPaymentFactory.create(
            order=order,
            payment_method='docdataIdeal',
            integration_data={'default_pm': 'ideal'},
        )
        DocdataPaymentFactory.create(order_payment=order_payment,
                                     payment_cluster_key='456-4',
                                     payment_cluster_id='test',
                                     default_pm='ideal',
                                     total_gross_amount=100)

        DonationFactory.create(
            project=self.project,
            order=order,
            amount=Money(100, 'EUR'),
        )

        with mock.patch.object(DocdataPaymentAdapter,
                               'refund_payment',
                               side_effect=self.mock_side_effect) as refund:
            refund_project(connection.tenant, self.project)

        self.order = Order.objects.get(pk=self.order.pk)
        self.assertEqual(refund.call_count, 1)
        self.assertEqual(self.order.status, 'refund_requested')

        self.order_payment.payment.status = 'refunded'
        self.order_payment.payment.save()

        self.order = Order.objects.get(pk=self.order.pk)
        self.assertEqual(self.order.status, 'cancelled')
Пример #2
0
 def setUp(self):
     super(PaymentsDocdataAdapterRefundTestCase, self).setUp()
     self.order_payment = OrderPaymentFactory.create(
         status='started',
         payment_method='docdataIdeal',
         integration_data={'default_pm': 'ideal'},
         order=OrderFactory.create(status='locked'))
     DocdataPaymentFactory.create(order_payment=self.order_payment,
                                  payment_cluster_key='123-4',
                                  default_pm='ideal',
                                  total_gross_amount=100,
                                  status='settled')
     self.adapter = DocdataPaymentAdapter(self.order_payment)
Пример #3
0
    def test_no_payment_method_change(self, mock_fetch_status,
                                      mock_transaction):
        self.assertEquals(PaymentLogEntry.objects.count(), 1)

        # Mock the status check with docdata
        mock_fetch_status.return_value = self.create_status_response(
            'AUTHORIZED')

        order = OrderFactory.create()
        order_payment = OrderPaymentFactory.create(
            order=order, payment_method='docdataCreditcard')
        docdata_payment = DocdataPaymentFactory.create(
            order_payment=order_payment,
            default_pm='mastercard',
            payment_cluster_id='1234',
            total_gross_amount=100)
        DocdataTransactionFactory.create(payment=docdata_payment,
                                         payment_method='VISA')
        c = Client()
        merchant_order_id = "{0}-1".format(order_payment.id)
        resp = c.get(reverse('docdata-payment-status-update',
                             kwargs={'merchant_order_id': merchant_order_id}))

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, 'success')

        # Reload the order payment
        order_payment = OrderPayment.objects.get(id=order_payment.id)
        self.assertEqual(order_payment.payment_method, 'docdataCreditcard')
Пример #4
0
    def setUp(self):
        super(TestRefund, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create(status=ProjectPhase.objects.get(
            slug='refunded'))

        self.order = OrderFactory.create()
        self.order_payment = OrderPaymentFactory.create(
            order=self.order,
            payment_method='docdataIdeal',
            integration_data={'default_pm': 'ideal'},
        )
        payment = DocdataPaymentFactory.create(
            order_payment=self.order_payment,
            payment_cluster_key='123-4',
            default_pm='ideal',
            total_gross_amount=100)

        DonationFactory.create(
            project=self.project,
            order=self.order,
            amount=Money(100, 'EUR'),
        )
        payment.status = 'authorized'
        payment.save()
        payment.status = 'settled'
        payment.save()

        self.project.refresh_from_db()
Пример #5
0
    def setUp(self):
        super(TestOrderPaymentAdminRefund, self).setUp()
        self.site = AdminSite()
        self.request_factory = RequestFactory()

        self.order_payment_admin = OrderPaymentAdmin(OrderPayment, self.site)
        self.order_payment = OrderPaymentFactory.create(
            payment_method='docdataIdeal',
            integration_data={'default_pm': 'ideal'},
        )
        DocdataPaymentFactory.create(order_payment=self.order_payment,
                                     payment_cluster_key='123-4',
                                     default_pm='ideal',
                                     total_gross_amount=100)
        self.order_payment.transition_to(StatusDefinition.SETTLED)
        self.order_payment.save()
Пример #6
0
    def test_no_payment_method_change(self, mock_fetch_status,
                                      mock_transaction):
        self.assertEquals(PaymentLogEntry.objects.count(), 1)

        # Mock the status check with docdata
        mock_fetch_status.return_value = self.create_status_response(
            'AUTHORIZED')

        order = OrderFactory.create()
        order_payment = OrderPaymentFactory.create(
            order=order, payment_method='docdataCreditcard')
        docdata_payment = DocdataPaymentFactory.create(
            order_payment=order_payment,
            default_pm='mastercard',
            payment_cluster_id='1234',
            total_gross_amount=100)
        DocdataTransactionFactory.create(payment=docdata_payment,
                                         payment_method='VISA')
        c = Client()
        merchant_order_id = "{0}-1".format(order_payment.id)
        resp = c.get(
            reverse('docdata-payment-status-update',
                    kwargs={'merchant_order_id': merchant_order_id}))

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, 'success')

        # Reload the order payment
        order_payment = OrderPayment.objects.get(id=order_payment.id)
        self.assertEqual(order_payment.payment_method, 'docdataCreditcard')
Пример #7
0
    def test_get_fee_absolute(self):
        """
            Test that the correct fee is returned given the defined percentage. In this test case the
            amount is 100 and the fee percentage is 1.5%, so the result should be 100 * 0.015.
        """

        order_payment = OrderPaymentFactory.create(amount=1000)
        docdata_payment = DocdataPaymentFactory.create(order_payment=order_payment,
                                                       default_pm='ideal',
                                                       total_gross_amount=1000)
        order_payment.amount = 100
        fee_total = docdata_payment.get_fee()
        self.assertEqual(fee_total, 100 * 0.015)
Пример #8
0
    def setUp(self):
        super(TestOrderPaymentAdminCheckStatus, self).setUp()
        self.site = AdminSite()
        self.request_factory = RequestFactory()

        self.order_payment_admin = OrderPaymentAdmin(OrderPayment, self.site)
        self.order_payment = OrderPaymentFactory.create(
            payment_method='docdataIdeal',
            integration_data={'default_pm': 'ideal'},
        )
        self.payment = DocdataPaymentFactory.create(
            order_payment=self.order_payment,
            payment_cluster_key='123-4',
            default_pm='ideal',
            total_gross_amount=100)
Пример #9
0
    def test_get_fee_relative(self):
        """
        Test that the correct fee is returned given the defined percentage.
        In this test case the amount is 100 and the fee percentage is 1.5%,
        so the result should be 100 * 0.015.
        """

        order_payment = OrderPaymentFactory.create(amount=1000)
        docdata_payment = DocdataPaymentFactory.create(
            order_payment=order_payment,
            default_pm='ideal',
            total_gross_amount=1000)
        order_payment.amount = 100
        fee_total = docdata_payment.get_fee()
        self.assertEqual(fee_total, 100 * 0.015)
Пример #10
0
    def test_unknown_payment_method_change(self, mock_fetch_status,
                                           mock_transaction):
        self.skipTest('Skipping test until we update it.')

        # Two payment log entries already exist: 2x 'a new payment status "started" '
        self.assertEquals(PaymentLogEntry.objects.count(), 2)

        # Mock the status check with docdata
        mock_fetch_status.return_value = self.create_status_response(
            'AUTHORIZED')

        order = OrderFactory.create()
        # Ensure that we use an existing payment_method or the adapter throws an exception
        order_payment = OrderPaymentFactory.create(order=order,
                                                   payment_method='docdataPaypal')
        docdata_payment = DocdataPaymentFactory.create(
            order_payment=order_payment,
            default_pm='paypal',
            payment_cluster_id='1236',
            total_gross_amount=100)

        DocdataTransactionFactory.create(payment=docdata_payment,
                                         payment_method='BLABLABLA')
        c = Client()
        merchant_order_id = "{0}-1".format(order_payment.id)
        resp = c.get(reverse('docdata-payment-status-update',
                             kwargs={'merchant_order_id': merchant_order_id}))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, 'success')

        # Reload the order payment
        order_payment = OrderPayment.objects.get(id=order_payment.id)
        self.assertEqual(order_payment.payment_method, 'docdataPaypal')

        # Check that all is logged correctly
        self.assertEquals(
            PaymentLogEntry.objects.filter(payment=docdata_payment).count(), 5)
        log = PaymentLogEntry.objects.all()[0]
        self.assertEqual(log.message,
                         "{0} - Payment method '{1}' not found for payment "
                         "with id {2} and order payment with id {3}.".format(
                             docdata_payment,
                             'BLABLABLA',
                             docdata_payment.id,
                             docdata_payment.order_payment.id))
        self.assertEqual(log.payment.id, docdata_payment.id)
        self.assertEqual(log.level, 'WARNING')
Пример #11
0
    def test_unknown_payment_method_change(self, mock_fetch_status,
                                           mock_transaction):
        self.skipTest('Skipping test until we update it.')

        # Two payment log entries already exist: 2x 'a new payment status "started" '
        self.assertEquals(PaymentLogEntry.objects.count(), 2)

        # Mock the status check with docdata
        mock_fetch_status.return_value = self.create_status_response(
            'AUTHORIZED')

        order = OrderFactory.create()
        # Ensure that we use an existing payment_method or the adapter throws an exception
        order_payment = OrderPaymentFactory.create(
            order=order, payment_method='docdataPaypal')
        docdata_payment = DocdataPaymentFactory.create(
            order_payment=order_payment,
            default_pm='paypal',
            payment_cluster_id='1236',
            total_gross_amount=100)

        DocdataTransactionFactory.create(payment=docdata_payment,
                                         payment_method='BLABLABLA')
        c = Client()
        merchant_order_id = "{0}-1".format(order_payment.id)
        resp = c.get(
            reverse('docdata-payment-status-update',
                    kwargs={'merchant_order_id': merchant_order_id}))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, 'success')

        # Reload the order payment
        order_payment = OrderPayment.objects.get(id=order_payment.id)
        self.assertEqual(order_payment.payment_method, 'docdataPaypal')

        # Check that all is logged correctly
        self.assertEquals(
            PaymentLogEntry.objects.filter(payment=docdata_payment).count(), 5)
        log = PaymentLogEntry.objects.all()[0]
        self.assertEqual(
            log.message, "{0} - Payment method '{1}' not found for payment "
            "with id {2} and order payment with id {3}.".format(
                docdata_payment, 'BLABLABLA', docdata_payment.id,
                docdata_payment.order_payment.id))
        self.assertEqual(log.payment.id, docdata_payment.id)
        self.assertEqual(log.level, 'WARNING')
    def _initialize_payments(self):
        """
        Initialize some projects, donations, payments.
        """
        # required for project save
        self.init_projects()

        yesterday = timezone.now() - timedelta(days=1)

        self.project1 = ProjectFactory.create(status__sequence=5, campaign_started=yesterday)  # has no payout
        # has a new payout
        self.project2 = ProjectFactory.create(status__sequence=8, campaign_started=yesterday)
        # has an in_progress payout
        self.project3 = ProjectFactory.create(status__sequence=8, campaign_started=yesterday)
        # has a settled payout
        self.project4 = ProjectFactory.create(status__sequence=8, campaign_started=yesterday)

        # make donations for each project
        status_progressions = [
            # successful order
            (StatusDefinition.AUTHORIZED, StatusDefinition.SETTLED),
            # successful order, but RDP says it's chargeback
            (StatusDefinition.AUTHORIZED, StatusDefinition.SETTLED),
            # successful order, but RDP says it's refund
            (StatusDefinition.AUTHORIZED, StatusDefinition.SETTLED),
            # cancelled
            (StatusDefinition.CANCELLED,),
            # chargeback
            (StatusDefinition.AUTHORIZED, StatusDefinition.SETTLED, StatusDefinition.CHARGED_BACK),
            # refunded
            (StatusDefinition.AUTHORIZED, StatusDefinition.SETTLED, StatusDefinition.REFUNDED),
            # something else went wrong
            (StatusDefinition.AUTHORIZED, StatusDefinition.SETTLED, StatusDefinition.FAILED),
        ]
        for i, progressions in enumerate(status_progressions):

            order = OrderFactory.create()
            order_payment = OrderPaymentFactory.create(order=order)
            cluster_id = 'payment%d' % (i+1)

            DonationFactory.create(project=self.project1, order=order, amount=5)
            DonationFactory.create(project=self.project2, order=order, amount=10)
            DonationFactory.create(project=self.project3, order=order, amount=15)
            DonationFactory.create(project=self.project4, order=order, amount=20)

            payment = DocdataPaymentFactory.create(
                order_payment=order_payment,
                status=StatusDefinition.STARTED,
                merchant_order_id=cluster_id,
                payment_cluster_id=cluster_id,
                payment_cluster_key=hashlib.md5('%d' % order.pk).hexdigest(),
                default_pm='visa',
                total_gross_amount=order.total * 100,
            )

            # do the status progression, this sets of the order/order_payment status progression
            for status in progressions:
                payment.status = status
                payment.save()

        # some messed up situations
        j = len(status_progressions)
        _payments = []
        for i in range(j, j+2):
            order = OrderFactory.create()
            order_payment = OrderPaymentFactory.create(order=order)
            cluster_id = 'payment%d' % (i+1)

            DonationFactory.create(project=self.project1, order=order, amount=5)
            DonationFactory.create(project=self.project2, order=order, amount=10)

            payment = DocdataPaymentFactory.create(
                order_payment=order_payment,
                status=StatusDefinition.STARTED,
                merchant_order_id=cluster_id,
                payment_cluster_id=cluster_id,
                payment_cluster_key=hashlib.md5('%d' % order.pk).hexdigest(),
                default_pm='visa',
                total_gross_amount=order.total * 100,
            )
            _payments.append(payment)
            order_payment.authorized()
            order_payment.save()
            order_payment.settled()
            order_payment.save()

        DocdataPayment.objects.filter(id__in=[p.id for p in _payments]).update(status=StatusDefinition.SETTLED)

        # create remotedocdata payments that should match 7/9 of the local payments
        self.rdp_list = []
        for i, payment_type in enumerate(
                ('paid', 'chargedback', 'refund', None, 'chargedback', 'refund', None)
                ):
            if payment_type is None:
                continue
            amount_collected = Decimal(50) if payment_type == 'paid' else -Decimal(50)
            rdp = RemoteDocdataPaymentFactory.create(
                local_payment=None,
                triple_deal_reference='pid123456789t',
                payment_type=payment_type,
                merchant_reference='payment%d' % (i+1),
                amount_collected=amount_collected
            )
            self.rdp_list.append(rdp)
            update_remote_docdata_status(rdp)
            rdp.save()
        for payment_type in ('chargedback', 'refund'):
            i = i + 1
            rdp = RemoteDocdataPaymentFactory.create(
                local_payment=None,
                triple_deal_reference='pid123456789t',
                payment_type=payment_type,
                merchant_reference='payment%d' % (i+1),
                amount_collected=-Decimal(15)
            )
            self.rdp_list.append(rdp)
            update_remote_docdata_status(rdp)
            rdp.save()

        self.assertEqual(RemoteDocdataPayment.objects.count(), 7)

        # make some assertions
        for project in [self.project1, self.project2]:
            project.update_amounts(save=True)
            self.assertEqual(project.donation_set.count(), 9)  # = len(status_progressions) + 2 weirdo's

        for project in [self.project3, self.project4]:
            project.update_amounts(save=True)
            self.assertEqual(project.donation_set.count(), 7)  # = len(status_progressions)

        # bring payouts in desired state
        self.assertFalse(self.project1.projectpayout_set.exists())

        payout2 = self.project2.projectpayout_set.first()
        payout2.payout_rule = BaseProjectPayout.PayoutRules.not_fully_funded
        self.assertEqual(payout2.status, StatusDefinition.NEW)
        self.assertEqual(payout2.amount_raised, 50)

        payout3 = self.project3.projectpayout_set.first()
        payout3.payout_rule = BaseProjectPayout.PayoutRules.not_fully_funded
        payout3.in_progress()
        payout3.save()
        self.assertEqual(payout3.amount_raised, 45)

        payout4 = self.project4.projectpayout_set.first()
        payout4.payout_rule = BaseProjectPayout.PayoutRules.not_fully_funded
        payout4.in_progress()
        payout4.save()
        payout4.settled()
        payout4.save()
        self.assertEqual(payout4.amount_raised, 60)