Example #1
0
    def subscription_plan_service_onaccept(form):
        """
            Onaccept of subscription_plan<=>service link:
            - register plan with the service (or update the registration)
        """

        # Get record
        form_vars = form.vars
        try:
            record_id = form_vars.id
        except AttributeError:
            record_id = None
        if not record_id:
            return

        # If not bulk:
        if not current.response.s3.bulk:

            table = current.s3db.fin_subscription_plan_service
            query = (table.id == record_id) & \
                    (table.deleted == False)
            row = current.db(query).select(
                table.plan_id,
                table.service_id,
                limitby=(0, 1),
            ).first()
            if not row:
                return

            from s3.s3payments import S3PaymentService
            try:
                adapter = S3PaymentService.adapter(row.service_id)
            except (KeyError, ValueError) as e:
                current.response.error = "Service registration failed: %s" % e
            else:
                success = adapter.register_subscription_plan(row.plan_id)
                if not success:
                    current.response.error = "Service registration failed"
Example #2
0
    def proc_order_create_onaccept(form):
        """
            After an Order has been created then redirect to PayPal to Register the Subscription
        """

        db = current.db
        s3db = current.s3db

        # Lookup Plan
        # @ToDo: Better link between Order & Plan (DRY, FKs)
        ptable = s3db.fin_subscription_plan
        post_vars_get = current.request.post_vars.get
        term = post_vars_get("sub_term_value")
        if term is None:
            # Service
            hours = post_vars_get("sub_hours_value")
            if hours == "10":
                plan = "Support 10"
            elif hours == "40":
                plan = "Support 40"
            else:
                plan = "Support 80"
            plan = db(ptable.name == plan).select(ptable.id,
                                                  limitby = (0, 1)
                                                  ).first()
        else:
            # Instance
            if term == "YR":
                interval_count = 12
            else:
                # term == "MO"
                interval_count = 1

            plan = db(ptable.interval_count == interval_count).select(ptable.id,
                                                                      limitby = (0, 1)
                                                                      ).first()

        plan_id = plan.id

        # Assume only a single Payment Service defined
        stable = s3db.fin_payment_service
        service = db(stable.deleted == False).select(stable.id,
                                                     limitby = (0, 1)
                                                     ).first()
        service_id = service.id

        # Lookup current User
        auth = current.auth
        pe_id = auth.s3_user_pe_id(auth.user.id)

        # Register with PayPal
        from s3.s3payments import S3PaymentService
        try:
            adapter = S3PaymentService.adapter(service_id)
        except (KeyError, ValueError) as e:
            current.response.error = "Service registration failed: %s" % e
        else:
            subscription_id = adapter.register_subscription(plan_id, pe_id)
            if subscription_id:
                # Link PO to Subscription
                ttable = s3db.proc_order_tag
                ttable.insert(order_id = form.vars.id,
                              tag = "subscription_id",
                              value = subscription_id,
                              )
                # Go to PayPal to confirm payment
                stable = s3db.fin_subscription
                subscription = db(stable.id == subscription_id).select(stable.approval_url,
                                                                       limitby = (0, 1)
                                                                       ).first()
                from gluon import redirect
                redirect(subscription.approval_url)
            else:
                # @ToDo: Read Details from the Log
                current.response.error = "Subscription registration failed"