Connect Stripe and take card payments
This guide connects your Stripe account to Hardcount, saves a test customer’s card, and follows the first charge. Hardcount works out each bill and makes the invoice; Stripe charges the card, and the money goes straight to your Stripe account.
Do it in test mode first. Each mode, test and live, has its own Stripe connection. You connect live mode the same way when you are ready, in the dashboard’s live mode or with a live key.
Before you start
Section titled “Before you start”- Hardcount running, with its address in
HARDCOUNTand a test secret key inHARDCOUNT_(first invoice).KEY - A test customer, with its Hardcount ID (
hc_) incus_ … CUSTOMER_for step 3. The first invoice page creates one in its step 3.ID - A Stripe account, in test mode.
- An address Stripe can reach. Stripe sends its webhooks (messages about
payments) to Hardcount over the internet. It cannot reach your own
computer: there, install the
Stripe CLI and sign it in to your
Stripe account with
stripe login. Step 2 uses it to pass the webhooks on.
How payments work, in short: Payments.
1. Save your Stripe keys
Section titled “1. Save your Stripe keys”-
In Stripe, open Developers → API keys. Copy the secret key (
sk_, or a restricted key,test_ … rk_) and the publishable key (test_ … pk_).test_ … -
In Hardcount, open Settings → Payments, with the dashboard in test mode. Under Connect your Stripe API keys, paste both keys and click Connect test mode.
You see: “Verified as” and your Stripe account’s name.
Or with the API. read -rs asks for the secret key without showing it,
so the key is not on your screen and not in the command your shell
records. Paste the key, then press Enter:
read -rs STRIPE_SKjq -n --arg sk "$STRIPE_SK" --arg pk "pk_test_…" \ '{livemode: false, secret_key: $sk, publishable_key: $pk}' \ | curl -s -X POST "$HARDCOUNT/v1/settings/stripe" \ -H "Authorization: Bearer $HARDCOUNT_KEY" \ -H "Content-Type: application/json" \ -d @- | jq '{id, stripe_account_name, verified_at, last_verified_error, last_verified_error_kind}'unset STRIPE_SKYou see: the connection’s id (hc_), your account’s name, a
verified_ time, and null for the two error fields. When the check
fails, the keys are still saved, but nothing can be charged until a check
passes. last_ says why, and last_ says
what to do:
rejected: Stripe refused the key. Save a working key.unavailable: Hardcount could not reach Stripe, so the key was not checked. Save the same keys again to check them.
A key manages only its own mode’s Stripe connection. Your test key connects
test mode, and a request from it for live mode answers 403. Connect live
mode with a live key (hc_), or in the dashboard in live mode.
2. Send Stripe’s events to Hardcount
Section titled “2. Send Stripe’s events to Hardcount”Hardcount learns that a card was saved, or a payment succeeded, from
Stripe’s webhooks. Each connection has its own webhook address:
<your Hardcount address>/, with the id from
step 1. Settings → Payments shows it under Deliver Stripe events to
Hardcount.
-
Send Stripe’s events to that address.
-
On a server Stripe can reach: in Stripe, open Developers → Webhooks and add an endpoint with that address. Choose the Snapshot payload style: Hardcount refuses a thin event with
400. Pick these 12 events, the ones Hardcount lists under the address:payment_,intent. succeeded payment_intent. payment_ failed checkout.session. completed setup_,intent. succeeded setup_intent. setup_ failed charge.,refund. updated refund.,created refund.,updated refund.failed charge.,dispute. created charge.,dispute. updated charge.dispute. closed
You see: the endpoint’s signing secret (
whsec_…) in Stripe. -
On your own computer: Stripe cannot reach it, so skip the endpoint in the Stripe Dashboard. With the Stripe CLI signed in to your Stripe account, run this in a second terminal and leave it running. It passes Stripe’s events on to Hardcount. Settings → Payments shows the command with your own address.
Terminal window stripe listen \--events payment_intent.succeeded,payment_intent.payment_failed,checkout.session.completed,setup_intent.succeeded,setup_intent.setup_failed,charge.refund.updated,refund.created,refund.updated,refund.failed,charge.dispute.created,charge.dispute.updated,charge.dispute.closed \--forward-to http://localhost:8080/v1/webhooks/stripe/hc_spc_…The command names the events to forward, the same list as above: the Stripe CLI does not start without one.
You see: the CLI prints a signing secret (
whsec_…).
-
-
Paste the signing secret into Hardcount and click Finish setup. Or send it with
PATCH /andv1/ settings/ stripe/ test/ webhook {"webhook_.secret": "whsec_ …"} You see: the card reads Stripe test mode connected, with a Ready badge.
Until the secret is saved, Hardcount answers Stripe’s webhooks with 404.
A customer then sees “card saved” on Stripe’s page, but Hardcount does not
learn of the card until Stripe sends the event again. Stripe sends a
failed delivery again for up to three days in live mode, and for a few
hours in test mode.
3. Save a customer’s card
Section titled “3. Save a customer’s card”The customer types their card on a Stripe page, never on yours. You give them a link to it:
-
From the dashboard: on the customer’s page, Payment methods, click Add payment method, then email the link or copy it.
-
From the API:
Terminal window curl -s -X POST "$HARDCOUNT/v1/customers/$CUSTOMER_ID/payment-methods/setup-session" \-H "Authorization: Bearer $HARDCOUNT_KEY" \-H "Content-Type: application/json" \-d '{}' | jq -r .urlYou see: a
https://address. Send it to the customer, or open it yourself in test mode. Addcheckout. stripe. com/ … "return_to the body to choose where Stripe sends them afterwards. Hardcount addsurl" ?status=successor?status=cancelto it.An error with
return_instead? Hardcount has no address to send the customer back to. The quick start sets one. On another install, setaddress_ not_ set CUSTOMER_(below), or passPORTAL_ URL "return_.url" -
By email from Hardcount:
POST /sends the link to the customer’s email address. It needs email set up (Set up email).v1/ customers/ {customer_ id}/ payment-methods/ send-setup-email
In test mode, save Stripe’s test card 4242 4242 4242 4242, with any
future expiry, any CVC and any postal code.
You see: on the customer’s page, Payment methods lists Visa
····4242. GET / lists it too.
A card saved this way becomes the customer’s default, the one Hardcount
charges.
A customer can also save a card by paying an invoice with Pay on its hosted invoice page.
4. The first charge
Section titled “4. The first charge”An invoice the customer already has. An invoice issued before the card
was saved stays open, such as the one the
first invoice page made (its step 7 saves the
ID in INVOICE_). Charge it now:
-
From the dashboard: open the invoice and click Collect payment, then confirm.
-
From the API:
Terminal window curl -s -X POST "$HARDCOUNT/v1/invoices/$INVOICE_ID/collect" \-H "Authorization: Bearer $HARDCOUNT_KEY" | jq '{status, payment_status}'You see:
"status": "paid"and"payment_.status": "succeeded"
Left alone, the invoice is charged on the next billing run (Payments).
A new invoice. Put the customer on a plan and let an invoice be issued. For a test customer, you need not wait for the period end: canceling the subscription issues its final invoice at once (first invoice, step 6). Right after the invoice is issued, Hardcount uses any credit the customer holds, then charges the card for the rest.
You see, either way:
- the invoice reads Paid in the dashboard, and the payment shows in your Stripe dashboard, under Payments;
- your app gets
invoice.andpaid payment.(Receive webhooks).succeeded
To see a failure, save 4000 0000 0000 0341 on another customer. Stripe
saves it, then declines every charge. The invoice’s payment goes to
failed, payment. is sent, and dunning starts: Hardcount’s
automatic payment retries for an unpaid invoice.
5. Disputes
Section titled “5. Disputes”A dispute is a customer asking their bank to reverse a card payment. With
the three charge. events in step 2, Hardcount records each
dispute on its invoice, which stays paid, and sends
payment., then payment. when Stripe closes
it. Read them with GET /. Respond to a dispute
in Stripe, before its deadline.
More: Card disputes.
Addresses Stripe sends customers back to
Section titled “Addresses Stripe sends customers back to”After a customer saves a card or pays, Stripe sends them to a page of your dashboard. Set these on your Hardcount server. None has a default: while one is unset, the flow it serves is refused, so no customer is sent to an address that doesn’t exist.
| Setting | The page after | Where the customer lands | While it is unset |
|---|---|---|---|
CUSTOMER_ |
a setup link from step 3, with no return_ |
<CUSTOMER_ (or cancel) |
The setup link is refused with return_ |
PAYMENT_ |
the “add a payment method” email Hardcount sends when an invoice finds no card | <PAYMENT_ (or cancel); it is a full address |
That email is not sent |
HOSTED_ |
Pay on the hosted invoice page | <HOSTED_, the invoice’s own page |
The page shows no Pay button |
More: Settings.
Next: go live: Production on one VM. To test a whole charge, from saving a card to a refund: Stripe end-to-end test.
Stuck, or found a bug? Ask in Discussions · Open an issue