コード例 #1
0
    def upgrade_plan(self, shop_id, plan_config_id, charge_id):
        """
            Upgrades the plan for the current [shop_id] using the [plan_config_id].
            It also saved the [charge_id] on the plan model.
        """
        shop_model = self.get(id=shop_id)
        plan_config = PlanConfigService().get(id=plan_config_id)

        api_object = "application_charge" if plan_config.billing_type == "O" else "recurring_application_charge"
        charge = self.find_app_charge(api_object, shop_model, charge_id)

        if charge["status"] == "accepted":
            self.activate_charge(api_object, shop_model, charge["id"])
        elif charge["status"] == "declined":
            return False

        #Check if the charge is already activated
        charge = self.find_app_charge(api_object, shop_model, charge_id)

        if charge["status"] == "active":

            plan = PlanService().new(shop=shop_model)

            #copies all the attributes from the plan_config to the plan model
            for field in plan_config.update_fields():
                setattr(plan, field, getattr(plan_config, field, ""))

            plan.charge_id = charge_id
            plan.installed_at = datetime.utcnow()
            plan.save()
        else:
            return False

        return True