Exemple #1
0
    def create_plan(self, amount, tax, name, description, return_url, cancel_url):
        """
        Create and activate monthly billing plan.
        https://developer.paypal.com/docs/api/payments.billing-plans

        :param amount: Decimal value of plan payment for one month including tax.
        :param tax: Decimal value of VAT tax.
        :param name: Name of the billing plan.
        :param description: Description of the billing plan.
        :param return_url: Callback view URL for approved billing plan.
        :param cancel_url: Callback view URL for cancelled billing plan.
        :return: Billing plan ID.
        """
        if amount < tax:
            raise PayPalError('Plan price should be greater than tax.')

        plan = paypal.BillingPlan(
            {
                'name': name,
                'description': description,
                'type': 'INFINITE',
                'payment_definitions': [
                    {
                        'name': 'Monthly payment for {}'.format(name),
                        'type': 'REGULAR',
                        'frequency_interval': 1,
                        'frequency': 'MONTH',
                        'cycles': 0,
                        'amount': {
                            'currency': self.currency_name,
                            'value': self._format_decimal(amount - tax),
                        },
                        'charge_models': [
                            {
                                'type': 'TAX',
                                'amount': {
                                    'currency': self.currency_name,
                                    'value': self._format_decimal(tax),
                                },
                            }
                        ],
                    }
                ],
                'merchant_preferences': {
                    'return_url': return_url,
                    'cancel_url': cancel_url,
                    'auto_bill_amount': 'YES',
                },
            }
        )

        try:
            if plan.create() and plan.activate():
                return plan.id
            else:
                raise PayPalError(plan.error)
        except paypal.exceptions.ConnectionError as e:
            raise PayPalError(e)
Exemple #2
0
def create_if_not_exists(product_catalog):
    """
    :param return_url: the return url
    :type product_catalog: sm.core.models.ProductCatalog
    """
    # TODO: (sanja) when we start using new plans, make sure this code doesn't raise error
    if product_catalog.product.plan not in [
            models.SubscriptionPlan.FLEX_PREPAID,
            models.SubscriptionPlan.ANNUAL_YEARLY
    ]:
        raise ValueError("Only flex prepaid and yearly paid is supported")

    monthly = product_catalog.product.plan == models.SubscriptionPlan.FLEX_PREPAID

    plan = models.PaypalBillingPlan.objects.filter(
        product_catalog=product_catalog, ).first()

    amount = dict(value=float(product_catalog.price), currency='USD')

    if not plan:
        plan = paypalrestsdk.BillingPlan(
            dict(name=str(product_catalog),
                 description=str(product_catalog),
                 type='INFINITE',
                 payment_definitions=[
                     dict(name='Regular Payments',
                          type='REGULAR',
                          frequency_interval="1",
                          frequency='MONTH' if monthly else "YEAR",
                          amount=amount,
                          cycles=0)
                 ],
                 merchant_preferences=dict(return_url="http://localhost",
                                           cancel_url="http://localhost",
                                           initial_fail_amount_action='CANCEL',
                                           max_fail_attempts=1)), api)

        if plan.create():
            if plan.activate():
                plan = models.PaypalBillingPlan.objects.create(
                    product_catalog=product_catalog, paypal_id=plan.id)
            else:
                raise Error(plan.error)
        else:
            raise Error(plan.error)

    return plan
Exemple #3
0
    def create_plan(self, amount, name, description, return_url, cancel_url):
        """
        Create and activate montlhy billing plan using PayPal Rest API.
        On success returns plan_id
        """
        plan = paypal.BillingPlan({
            'name':
            name,
            'description':
            description,
            'type':
            'INFINITE',
            'payment_definitions': [{
                'name':
                'Monthly payment for {}'.format(name),
                'type':
                'REGULAR',
                'frequency_interval':
                1,
                'frequency':
                'MONTH',
                'cycles':
                0,
                'amount': {
                    'currency': self.currency_name,
                    'value': str(amount)
                }
            }],
            'merchant_preferences': {
                'return_url': return_url,
                'cancel_url': cancel_url,
                'auto_bill_amount': 'YES',
            }
        })

        try:
            if plan.create() and plan.activate():
                return plan.id
            else:
                raise PayPalError(plan.error)
        except paypal.exceptions.ConnectionError as e:
            six.reraise(PayPalError, e)