Skip to main content

Crate renox_billing

Crate renox_billing 

Source
Expand description

Subscriptions for Renox apps (Laravel Cashier): plans declared in code, free trials, upgrades and downgrades, cancellations with a grace period, with Stripe and Xendit.

use renox::prelude::*;
use renox_billing::{Billing, Interval, Plan};

App::new()
    .module(Auth::new().account()) // the account page shows the subscription
    .module(
        Billing::new()
            .plan(Plan::new("basic", "Basic").price(900, "USD", Interval::Month))
            .plan(Plan::new("pro", "Pro").price(1_900, "USD", Interval::Month).trial_days(14))
            .stripe(), // STRIPE_SECRET, STRIPE_WEBHOOK_SECRET, STRIPE_PRICE_BASIC, … in .env
    )

A plans page (/billing) sends the user to the gateway’s payment page; the subscription arrives by webhook (/billing/webhooks/{gateway} or /billing/webhooks/{gateway}/{account}, verified, stored once per event, applied by a queue worker) into the subscriptions table, and the account page shows it with “Change plan”, “Cancel” and “Resume”. In code, Billing::of answers subscribed(), on_trial(), on_grace_period() and does checkout, swap, cancel, cancel_now, resume, pause and unpause; SubscriptionRoutes::require_subscription guards routes. The guide is docs/billing.md in the Renox repository.

Re-exports§

pub use events::DisputeLost;
pub use events::DisputeOpened;
pub use events::DisputeWon;
pub use events::PaymentFailed;
pub use events::PaymentLinkExpired;
pub use events::PaymentRefunded;
pub use events::PaymentSucceeded;
pub use events::RefundFailed;
pub use events::RefundSucceeded;
pub use events::SubscriptionCanceled;
pub use events::SubscriptionCreated;
pub use events::SubscriptionPaused;
pub use events::SubscriptionUnpaused;
pub use events::SubscriptionUpdated;
pub use gateway::BoxFuture;
pub use gateway::Checkout;
pub use gateway::CheckoutRequest;
pub use gateway::Gateway;
pub use gateway::Notice;
pub use gateway::Payment;
pub use gateway::Remote;
pub use ngenius::Ngenius;
pub use payment::BillingPayment;
pub use payment::BillingRefund;
pub use payment::Charge;
pub use payment::ChargeRequest;
pub use payment::DisputeState;
pub use payment::DisputeUpdate;
pub use payment::PaymentKind;
pub use payment::PaymentPage;
pub use payment::PaymentState;
pub use payment::PaymentStatus;
pub use payment::PaymentUpdate;
pub use payment::Refund;
pub use payment::RefundResult;
pub use payment::RefundStatus;
pub use payment::RefundUpdate;
pub use payment::Unsupported;
pub use stripe::Stripe;
pub use xendit::Xendit;

Modules§

events
What the module announces. Listen with App::listen: send a welcome mail, unlock features, warn about a failed payment. When the app has the Audit module, a user’s new subscriptions, cancellations and failed payments are recorded in audit_logs too.
gateway
What a payment gateway is: the calls the module makes (a customer, a checkout, a plan change, a cancellation) and how its webhooks read. Stripe and Xendit come with the crate; another gateway is one impl of Gateway. Every answer is normalized: a subscription’s state is a Remote, a webhook is a list of Notices.
ngenius
N-Genius (Network International): hosted-payment-page orders. Every call first trades the API key for a short-lived access token (kept in memory per account until it expires), then uses it as a bearer token. One-off payments are orders (/transactions/outlets/{outlet}/orders, a charge and a link alike), read back with the order’s status and refunded through the refund link of the captured payment. Webhooks are checked against a shared secret sent in the x-webhook-secret header (set it as the webhook’s custom header in the N-Genius portal). N-Genius has no subscriptions here and no disputes API, so those operations answer Unsupported.
payment
One-off payments: the billing_payments and billing_refunds tables, their status types and the Unsupported error a gateway answers with for an operation it cannot do.
stripe
Stripe: Checkout Sessions in subscription mode, the Subscriptions API for plan changes (prorated), cancellations and resumes, and the customer.subscription.* and invoice.payment_* webhooks, signed in the Stripe-Signature header.
xendit
Xendit: Recurring Plans (/recurring/plans), whose hosted page links the customer’s card or e-wallet, and the recurring.plan.* and recurring.cycle.* webhooks, with the x-callback-token header. One-off payments are invoices (/v2/invoices, a charge and a link alike) and refunds (/refunds); their invoice and refund.* callbacks are applied like any one-off notice. Xendit has no disputes API here, so accept_dispute stays Unsupported.

Structs§

Billing
The billing module: plans, gateways and how changes behave. Add it next to Auth (with .account() for the subscription’s card on the account page).
BillingCustomer
An owner’s customer id at a gateway (one per gateway).
Customer
An owner’s subscriptions, and what they can do with them: from Billing::of. Every method is about one subscription name, default unless Customer::named says another.
Owner
Who pays: a kind (user, or the app’s name for what it bills, such as team) and an id, with an email address and a name for the gateway’s customer record.
Plan
A plan an app sells: a key that never changes once people subscribed (it is stored in subscriptions.plan), a name for people, a price and how often it’s charged.
Subscription
A subscription of an owner (a user, or what the app bills, such as a team) to a plan. An owner keeps one row per subscription they took; crate::Customer::subscription is the newest of a name.

Enums§

Interval
How often a plan is charged.
SubscriptionStatus
Where a subscription is, as the gateway last said (subscriptions.status).

Traits§

Billable
What can subscribe. Users are billable as they are; for a team (or an organization…), say which Owner it is:
SubscriptionRoutes
Guards for Routes: only users with a valid default subscription (or the one named, for the _named guards) get through: on a trial, paid for, or canceled with time left. Others go to the plans page (billing.plans) with a toast; a JSON request gets 402 Payment Required.