Skip to Content
PaymentsCollecting Payments

Collecting Payments

collect() sends a payment prompt to a customer’s phone and returns immediately — it does not wait for them to respond. See How It Works for why, and Webhooks for the reliable way to find out what they did.

import { VectaPayClient, money } from "@vectapay/sdk"; const vectapay = new VectaPayClient({ apiKey: process.env.VECTAPAY_API_KEY! }); const payment = await vectapay.payments.collect({ customer: { phone: "254712345678" }, amount: money(50000), // KES 500.00 reference: "INV-001", description: "Payment for invoice INV-001", metadata: { orderId: "ORD-123" }, });

Collecting by phone or by email

collect() routes on the customer shape you send:

You sendWhat happens
customer.phoneA mobile-money prompt (M-Pesa STK or Airtel) goes to that handset. collect() returns as soon as the prompt is sent.
customer.email, no phonePaystack’s hosted checkout — card, bank transfer, or M-Pesa without your own Daraja credentials. The response carries checkoutRedirectUrl; send the customer’s browser there.
BothThe phone path wins, so an existing phone-only integration keeps working.

An email-only call needs a Paystack account connected; if there isn’t one, the request fails with a provider-routing error rather than falling back to another rail. Add a returnUrl to send the customer back to your site after checkout.

Request fields

FieldRequiredNotes
customer.phonephone/emailKenyan mobile number. formatPhoneNumber() normalizes user input first — see below.
customer.emailphone/emailUsed only when there is no phone; starts a hosted checkout.
amountYesA Money value — see below. Currently KES 10–150,000; validateAmount() checks this client-side.
referenceYes1–12 characters. Shown to the customer and on their statement — your order/invoice number.
descriptionYes1–40 characters. Shown on the payment prompt itself.
providerAccountIdNoSend to a specific connected provider account instead of your default — see Providers. Not allowed in test mode, which always uses the test line.
returnUrlNoHosted checkout only — where to send the customer after paying.
idempotencyKeyNoResend the same key within the window and get the original result back — see below.
metadataNoYour own data, echoed back on the payment and in webhooks.

Test mode

A Test API key pk_test_… routes every collect() into the sandbox, where:

  • Phone payments only reach your verified test phone. Any other number is rejected with 400 telling you the last three digits of the number you verified.
  • providerAccountId cannot be used. Test payments always run on VectaPay’s M-Pesa test line; sending another account ID is a 400.
  • Email-only payments use your Paystack test keys, if you’ve connected them. Paystack is the only provider with a merchant-owned test account.
  • Prompts are rate-limited and never billed. The API returns 429 with retryAfterSeconds when a budget is used up — see Testing for the exact budgets.
  • Every payment is recorded with environment: "SANDBOX" and shows in Sandbox view in the dashboard.

Money

Every amount in every one of these SDKs is a value object, — passing a raw number where an amount is expected is a compile-time type error in TypeScript, Java, and C#, and a validation error in Python. This is deliberate: a bare number can’t carry a currency, so it’s too easy to accidentally send 500 meaning KES 500 into a call that silently interprets it as 50000 cents. The constructor takes minor units (cents) so nothing drifts from rounding:

money(50000) // { amount: "50000", currency: "KES" } — KES 500.00 money(50000, "KES") // same, explicit

Idempotency

await vectapay.payments.collect({ // ... idempotencyKey: `checkout-${orderId}`, });

If a customer double-taps “Pay” and your code sends the same request twice with the same key, the second call returns the original payment instead of charging them again, same result both times. Use a key derived from something that’s genuinely one-per-attempt in your system (an order ID, a cart checkout ID).

Reusing a key for a request with a different amount, method, or reference than the one it was first used for is the one case that’s always rejected — you get a ConflictError (HTTP 409) instead of either the stale result or a second charge. This almost always means a bug on the caller’s side (a key that isn’t actually unique to the attempt); a genuine retry should send the exact same parameters as the original call.

Omit idempotencyKey entirely and VectaPay derives one from your business, reference, and amount — deduping identical-looking requests for 24 hours. That’s enough to absorb a real retry storm without permanently blocking a new charge against the same reference next week. Supply your own key when you need a dedup window longer than 24 hours, or when your own reference isn’t guaranteed unique per attempt on its own.

Normalizing phone numbers

Users type phone numbers inconsistently. Run whatever they typed through formatPhoneNumber() before sending it — it accepts 0712345678, +254712345678, 712345678, or 254712345678, and throws a clear error for anything else.

import { formatPhoneNumber } from "@vectapay/sdk"; const phone = formatPhoneNumber(userInput); // throws if unrecognizable

The response

{ "success": true, "message": "Payment request sent", "data": { "paymentId": "pay_...", "merchantRequestId": "...", "checkoutRequestId": "...", "status": "PENDING", "providerAccountUsed": { "id": "...", "name": "...", "environment": "PRODUCTION" }, "checkoutRedirectUrl": null } }

Store paymentId — every other payments call takes it. checkoutRequestId is specific to M-Pesa STK push and only meaningful for live status queries against that rail. checkoutRedirectUrl is a URL only for an email-only hosted-checkout payment.

This response means the prompt was sent, not that the customer paid. Confirm the outcome with a webhook — the sequence diagram there shows exactly where this response fits in the overall timeline.

Reading payments back

import { PublicPaymentStatus } from "@vectapay/sdk"; const payment = await vectapay.payments.get(paymentId); // List with filters const page = await vectapay.payments.list({ status: PublicPaymentStatus.SUCCESS, limit: 20 }); // Iterate every match without hand-rolling pagination for await (const p of vectapay.payments.listIterator({ status: PublicPaymentStatus.SUCCESS })) { console.log(p.id, p.amount); }
Last updated on