All guides Backups and undo: getting an earlier version of your app back Files and images your app's users upload: taking, storing, showing and sharing them Plans and limits: how many projects you can have, the Exotool notice, and what happens at the limit Resources: connecting an Exotool app to outside services Sign-in and permissions: users, logging in and who may do what in your app Storing data: objects, sets and where your app keeps them Taking payments: cards, charges and holds with Stripe or PayPal
Taking payments: cards, charges and holds with Stripe or PayPal
This guide is for the person whose app has to take money from its own users: a shop, a booking with a deposit, a membership fee. It covers what the project needs before it can charge a card, the credit card form, the functions that charge a card, charge it again or hold an amount on it, what they give back, amounts and currency, testing, and what commonly goes wrong. It is about payments taken by the app you build; it is not about paying for your Exotool plan.
Contents
- What you need first
- Stripe or PayPal
- The credit card form
- Charging a card
- The transaction record
- Charging a saved card again
- Holds: authorize, capture, release (Stripe only)
- Amounts and currency
- Where the card goes
- Testing and going live
- When something does not work
1. What you need first
An app takes payments through an E-Commerce Server resource. A resource is a saved connection to an outside service; this kind is a Stripe account or a PayPal account. The project uses it through its e-commerce server role, the slot that says "this project's payment account".
So, before any card can be charged: make an E-Commerce Server resource on the editor's Resources page, set it as the project's e-commerce server role, and build the project again (a change of roles takes effect at the next build). The Resources guide explains each step: making a resource, roles, and what is kept secret. This guide does not repeat it. Without the resource the app still compiles, with a warning, and the charge fails when someone pays (section 11).
2. Stripe or PayPal
| Stripe | PayPal | |
|---|---|---|
| What the resource asks for | Publishable key, Secret key, Currency (optional), API address (optional: leave it blank) | API endpoint, Client id, Secret |
| Where you find them | Stripe: Developers > API keys | PayPal Developer dashboard > Apps & Credentials: the client id and the secret of an app of a business account |
charge_credit_card, re_charge_credit_card |
Yes | Yes |
authorize_card, capture_card, release_card (holds) |
Yes | No: they return an error string |
| The card number reaches your app's server | No (section 9) | Yes: the form's values go to the server, which sends them to PayPal |
| Currency of the amounts | The resource's Currency field; usd when it is blank |
US dollars |
The secret key (Stripe) and the secret (PayPal) are kept encrypted and are never shown again. Stripe's publishable key is used by the app in the browser and is public by nature.
3. The credit card form
The Credit Card Form (element type credit_card_form1) is a ready-made form that you
place in the body of a page. It is one element: you do not add input rows to it. It shows
First Name, Last Name, Card Number, Card Type, the expiration month and year, the security
code (CVV2), a Pay Now button and a line of information text.
Besides its title it has three parameters: Information (info, the text under the
button), Do On Action (do_on_action, the activity code that runs when Pay Now is
pressed) and Wrapper Template (wrapper_template, the form's HTML: change it to change
the fields).
When the person presses Pay Now, the form collects its values into the variable
_cardvalues_ and then runs do_on_action. _cardvalues_ is a record with the fields
first_name, last_name, number, type (visa, mastercard, discover or amex),
expire_month, expire_year and cvv2 (the security code).
With a Stripe resource the form first hands the card to Stripe from the device. number and
cvv2 are then empty, and three fields are added: token (Stripe's payment method id),
card_brand and card_last4. If Stripe does not accept the card, the form shows Stripe's
message (or "The card was not accepted") and do_on_action does not run.
A new form comes with this do_on_action:
_chargeres_ = charge_credit_card(_cardvalues_,charge_amount,charge_description)
if (_chargeres_ == "OK") {
charge_succeeded = true
}
else {
charge_succeeded = false
alert(_chargeres_)
}
It expects two variables that your app sets before the person reaches the form:
charge_amount (the amount) and charge_description (what the charge is for). Afterwards
charge_succeeded says whether the payment went through. You can keep this code, or replace
it with your own.
4. Charging a card
charge_credit_card(cardinfo, total, description)
cardinfo is the values of a credit card form (_cardvalues_), total the amount and
description what the charge is for. It returns "OK", or an error string that says
why the card was not charged. Always test the result, and tell the person when it is not
"OK":
charge_amount = usd(order.total)
result = charge_credit_card(_cardvalues_, charge_amount, concat("Order ", order.number))
if (result == "OK") {
order.payment_record = transaction_record_id
order.status = "paid"
}
else {
alert(result)
}
- It runs on the server. The payment functions are server-only, so a statement that calls one runs on the server.
- On
"OK"it sets the server variabletransaction_record_id: the id of a new transaction record (section 5). When the charge fails,transaction_record_idis null. - The card values are cleared after the call, whether the charge went through or not. Do
not store
_cardvalues_in an object of your own. - Take the amount from the server. A field of a cloud object (here
order.total) is set only on the server. A variable that the device sets is the device's to change.
5. The transaction record
Each successful charge or hold makes a transaction record: a cloud object of the type
transactionRecord. transaction_record_id holds its id. Keep that id in a record of your own
(an order, a booking), as in the example above, and fetch the record when you need it
(locate_cloudobject runs on the server and returns false when there is no such record):
receipt = locate_cloudobject("transactionRecord", order.payment_record)
alert("Paid with ", receipt.card_description)
| Field | Holds |
|---|---|
charge_amount |
The amount charged or held |
description |
The description you gave |
date, datestring |
When: a timestamp, and the date as month/day/year |
card_description |
A short description of the card; with Stripe for example Visa ending 4242 |
card |
The saved card, for charging it again (section 6). Not the card number |
first_name, last_name |
From the form. email, address, address2, city, state and zip are copied too when the form's values carry fields of those names |
transaction_info |
The payment provider's answer |
is_rebill |
True on a record made by re_charge_credit_card |
is_authorization, authorization_state, authorization_id |
On a hold (section 7): the state is authorized, captured or released |
captured_amount |
On a captured hold: the amount taken |
6. Charging a saved card again
re_charge_credit_card(card, total, description)
card is the card field of an earlier transaction record. The person does not type the card
again. The result is the same as for charge_credit_card: "OK" and a new
transaction_record_id, or an error string. It works with Stripe and with PayPal.
earlier = locate_cloudobject("transactionRecord", member.payment_record)
result = re_charge_credit_card(earlier.card, 10.00, "Monthly fee")
if (result == "OK") {
member.last_paid_record = transaction_record_id
}
7. Holds: authorize, capture, release (Stripe only)
A hold reserves an amount on a card without taking it. Later you either take the money or let the hold go. It is meant for a deposit, an escrow or a reservation. Holds need a Stripe resource.
| Function | What it does | Returns |
|---|---|---|
authorize_card(cardinfo, total, description) |
Puts a hold on the card for the amount. cardinfo is _cardvalues_ |
"OK" and transaction_record_id (the record's authorization_state is authorized), or an error string |
capture_card(transaction_record_id [, amount]) |
Takes the money of a hold: all of it, or the smaller amount |
"OK" or an error string |
release_card(transaction_record_id) |
Lets the hold go. Nothing is taken | "OK" or an error string |
The first part belongs in the form's do_on_action, when the booking is made. The second
part runs later, in another activity, and does one of the two:
result = authorize_card(_cardvalues_, 200.00, "Deposit for the rental")
if (result == "OK") {
booking.hold_record = transaction_record_id
}
// later: keep 150.00 of the 200.00
result = capture_card(booking.hold_record, 150.00)
// or give it all back
result = release_card(booking.hold_record)
A hold can be captured once. After a capture or a release the record's authorization_state
is captured or released, and a second capture_card or release_card on it returns an
error string such as capture_card: that hold was already captured. This guide does not
cover how long a bank keeps a hold: the sources do not say.
8. Amounts and currency
- An amount is a number in the main unit of the currency, with cents as decimals:
25.00,12.34. usd(number)truncates a number to dollars and cents:usd(23.13000001)gives23.13. It cuts, it does not round up. Use it on an amount you have calculated, for exampleusd(price * (1 - discount / 100)).- Currency. With Stripe the amounts are in the currency of the resource: its Currency
field takes a three-letter code, and
usdis used when the field is blank. With PayPal the charge is made in US dollars.
9. Where the card goes
With Stripe the card never reaches your app's server. The credit card form hands the card
to Stripe from the person's device, with the publishable key. Your server receives a payment
method id, the brand and the last four digits, and charges with the secret key. For that
reason charge_credit_card and authorize_card must be given the values of a credit card
form: a card number sent to the server from a form of your own is refused.
With PayPal the form's values, the card number among them, go to your app's server, which
sends them to PayPal. In both cases the record's card field is not the card number: it is a
saved card, a reference that the provider gave, and card_description describes the card.
10. Testing and going live
Use the provider's test credentials while you build:
- Stripe: test keys (
pk_test_...andsk_test_...) charge nothing; live keys (pk_live_...,sk_live_...) charge for real. - PayPal: the endpoint
api.sandbox.paypal.comis for tests,api.paypal.comfor live payments.
A common arrangement is two resources: one with the test credentials for development, one with
the live credentials for production. A deployed app uses the resources its project had when it
was deployed, so set the production resource as the e-commerce server role, deploy, and set the
development one back. The Resources guide (section 9) and the deployment guide say what the
operator of a host can change afterwards without a rebuild. Two more points for a deployed
app: the host's PHP needs curl for payments, and a deployed app starts with empty data, so
transaction records made in the preview are not in it.
11. When something does not work
| What you see | Likely cause | What to do |
|---|---|---|
Compile warning: This credit card form cannot charge a card: the project has no E-Commerce Server resource. |
The e-commerce server role is not set | Make an E-Commerce Server resource, set the role, build again. A demo that never charges can ignore it |
Compile warning: charge_credit_card cannot charge a card: the project has no E-Commerce Server resource. |
The same, reported for the code that calls the function | The same |
A charge fails on the server and neither "OK" nor an error string comes back |
The app was built without an E-Commerce Server resource, so the payment code is not in it | Set the role and build again |
No payment resource is set for this app (the E-Commerce Server role) |
A hold function was called in an app built without the resource | Set the role and build again |
authorize_card needs a Stripe payment resource: the project's payment account cannot hold an amount on a card |
The resource is a PayPal account | Holds work with Stripe only. capture_card and release_card answer ... needs a Stripe payment resource |
| The resource was set, but nothing changed | The project was not rebuilt | Build again: roles take effect at the next build |
The Stripe resource has no secret key |
The secret key is blank in the resource (and not given on the host) | Enter the secret key in the resource and build again |
| An alert with a message from Stripe, or "The card was not accepted", right after Pay Now | Stripe refused the card on the device, before your code ran | The person corrects the card or uses another |
The card must be given to Stripe from the device ... |
A card number reached the server with a Stripe resource: the values did not come from a credit card form | Take the card with the Credit Card Form |
No payment method: the card form did not hand a Stripe payment method to the server |
With Stripe, cardinfo was empty or not a card form's values, for example _cardvalues_ used after an earlier call cleared it |
Call the function once, in the form's do_on_action, with _cardvalues_ |
The card requires an extra confirmation step (3-D Secure), which this payment form does not support yet; try another card |
The card's bank asks for 3-D Secure | Another card |
capture_card: no hold with that transaction record id, or capture_card: that hold was already captured (the same from release_card) |
The id is not that of a record made by authorize_card, or the hold was already taken or let go |
Keep transaction_record_id right after authorize_card and pass that; a hold is captured or released once |
Could not reach the payment service: ... |
The server could not connect to the provider | Try again; on a host of your own check that PHP has curl |
| Test payments work but no money arrives | The resource holds test keys or the sandbox endpoint | Use a resource with the live credentials (section 10) |