Expand description
One payment API over any payment provider.
Write against Provider and which provider takes the money becomes a
deployment decision rather than a rewrite. Five ship with this workspace —
Stripe, iyzico, PayTR, Mollie and PayPal — and a provider that lives
elsewhere is a first-class one: implement Provider, name it with
ProviderId::new. Everything a caller needs is re-exported here; the
bundled adapters are behind features, one each.
kasapay = { version = "0.0.5", features = ["stripe", "iyzico"] }§The one thing to understand first
Provider::charge does not mean the money moved. It returns a Charge
whose Status is often Status::RequiresAction, with a NextAction
saying what the payer must do — confirm in the browser for Stripe, follow a
deep link into iyzico’s app for iyzico. Treating a returned Charge as a
completed payment is the mistake this crate is shaped to prevent.
§What the trait answers
Provider is charge, charge_status, capture, cancel, refund,
lookup and instruments, and Capabilities says which of them a given
provider actually does — before there is a payment to ask about. A
capability that says yes and a call that then fails is a bug in the
adapter.
Giving money back is Provider::refund, which answers a Refund
with its own life: its own identifier where the provider issues one, its
own RefundStatus, and at iyzico’s counter its own NextAction,
because there the payer approves the refund in an app. Status has no
Refunded and will not grow one — no provider reports a refund as a
payment’s status.
The call whose answer never arrived is Provider::lookup, keyed by
OrderRef — the caller’s own reference, which they had before they sent
anything. Ok(None) means the provider has no record and the charge can
safely be sent again. Two of the five can answer it; the rest say what to
do instead.
§What arrives without being asked for
Webhook is the second trait: it takes the headers and bytes of a
Delivery, shows they are the provider’s, and says what they mean as an
Event.
use kasapay::{Delivery, EventKind, Webhook};
match verifier.verify(&Delivery::new(headers, body)).await {
// The identifier goes into a unique index before anything ships: the
// second delivery of an event must collide rather than ship twice.
Ok(event) if event.kind == EventKind::Captured => "ship it",
// Not an error. A provider adding an event type is normal, and
// refusing one earns days of redeliveries for something nobody wanted.
Ok(_) => "acknowledged",
// Do not act on it — and still answer the provider what the provider
// documents. Those are two different questions.
Err(_) => "acknowledged",
}§Choosing a provider at runtime
use std::sync::Arc;
use kasapay::{Provider, ProviderId};
match id {
ProviderId::STRIPE => Some(Arc::new(stripe)),
ProviderId::IYZICO => Some(Arc::new(iyzico)),
_ => None,
}Modules§
- iyzico
- iyzico, behind kasapay’s
Providertrait. - kind
- What an identifier can name.
- mollie
- Mollie, behind kasapay’s
Providertrait. - paypal
- PayPal, behind kasapay’s
Providertrait. - paytr
- PayTR, behind kasapay’s
Providertrait. - stripe
- Stripe, behind kasapay’s
Providertrait.
Structs§
- Address
- Somewhere to bill, ship or register a payer at.
- Basket
Item - One line of what is being paid for.
- Buyer
- The person paying.
- Capabilities
- What a provider will do, asked before there is a payment to ask it about.
- Charge
- A charge, as the provider currently sees it.
- Charge
Request - A charge to create.
- Charge
Request Builder - Collects the parts of a
ChargeRequestbefore it is checked. - Delivery
- One delivery from a provider, exactly as it arrived.
- Error
- A payment operation failed.
- Event
- What a provider told us happened, once it has been shown to be theirs.
- Id
- How a provider names one thing of kind
K. - Idempotency
Key - A key that makes replaying a charge safe.
- Instrument
- One instrument a provider holds against a customer.
- Money
- An amount in one currency, counted in that currency’s minor unit.
- Order
Ref - Our own reference for an order, chosen by the caller.
- Provider
Id - Names a provider.
- Raw
- What a provider actually sent, for everything kasapay does not model.
- Refund
- Money given back off a payment, as the provider currently sees it.
- Refund
Request - A refund to make.
- Refund
Request Builder - Collects the parts of a
RefundRequestbefore it is checked. - Repeated
Header - A header a signature depends on arrived more than once.
- Secret
- Holds a credential and keeps it out of
Debugoutput and logs. - Unknown
Currency - The string was not a currency code kasapay supports.
Enums§
- Charge
Request Error - A
ChargeRequestwas built out of parts that do not make a valid charge. - Currency
- A currency kasapay knows how to move money in.
- Error
Kind - What went wrong, in terms a caller can branch on without knowing the provider.
- Event
Kind - What a delivery says happened.
- IdSource
- Whose uniqueness an identifier rests on.
- Item
Kind - What kind of thing is being sold.
- Money
Error - A decimal string could not be read as an amount in the given currency.
- Next
Action - What the payer has to do before the payment can go on.
- Refund
Reason - What a merchant tells the provider the money went back for.
- Refund
Request Error - A
RefundRequestwas built out of parts that do not make a valid refund. - Refund
Status - Where a refund stands.
- Status
- Where a payment stands.
Traits§
- IdKind
- What an identifier names — the other question, answered in the type.
- Provider
- Takes a payment and reports on it.
- Webhook
- Checks that a delivery is the provider’s, and says what it means.
Type Aliases§
- EventId
- How one delivery to a webhook address is named.
- Instrument
Id - How the provider names one card it holds, so a payment need not carry one.
- Payment
Id - How the provider names a payment.
- Refund
Id - How the provider names one refund, where it names it at all.