mirror of
https://github.com/ubicloud/ubicloud.git
synced 2025-10-04 22:02:18 +08:00
Previously, project_id and subject_id needed to be a uuid, and object_id could be a uuid or ubid. This allows accepting an object for all three. This simplifies almost all callers of these methods. It also doesn't require either parsing a ubid or generating a ubid. Previously, even if a uuid was given, it would turn it into a ubid in order to extract the class, and guess ApiKey if it was an et ubid.
48 lines
1.7 KiB
Ruby
48 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Clover
|
|
hash_branch(:project_prefix, "discount-code") do |r|
|
|
r.post r.web? do
|
|
authorize("Project:billing", @project)
|
|
handle_validation_failure("project/billing")
|
|
|
|
if (discount_code = typecast_params.nonempty_str("discount_code"))
|
|
discount_code = discount_code.strip.downcase
|
|
Validation.validate_short_text(discount_code, "discount_code")
|
|
|
|
# Check if the discount code exists
|
|
discount = DiscountCode.first(code: discount_code) { expires_at > Sequel::CURRENT_TIMESTAMP }
|
|
end
|
|
|
|
unless discount
|
|
Clog.emit("Invalid discount code attempted") { {invalid_discount_code: {project_id: @project.id, code: discount_code}} }
|
|
raise_web_error("Discount code not found.")
|
|
end
|
|
|
|
begin
|
|
DB.transaction do
|
|
hash = ProjectDiscountCode.dataset.returning.insert(
|
|
id: ProjectDiscountCode.generate_uuid,
|
|
project_id: @project.id,
|
|
discount_code_id: discount.id
|
|
).first
|
|
@project.this.update(credit: Sequel[:credit] + discount.credit_amount.to_f)
|
|
audit_log(ProjectDiscountCode.call(hash), "create")
|
|
end
|
|
rescue Sequel::UniqueConstraintViolation
|
|
raise_web_error("Discount code has already been applied to this project.")
|
|
end
|
|
|
|
unless @project.billing_info
|
|
stripe_customer = Stripe::Customer.create(name: current_account.name, email: current_account.email)
|
|
DB.transaction do
|
|
billing_info = BillingInfo.create(stripe_id: stripe_customer["id"])
|
|
@project.update(billing_info_id: billing_info.id)
|
|
end
|
|
end
|
|
|
|
flash["notice"] = "Discount code successfully applied."
|
|
r.redirect billing_path
|
|
end
|
|
end
|
|
end
|