Partner Docs
Search…
Charges
Charges describe what you charge your users for and how much. We show this information to users when subscribing to your API and also use it to ensure only charges of certain amounts are made, this gives users knowledge about how your pricing works.
​

Static Charges

Static Charges are charges that can only be made once per user request, this allows you to setup a "pay per request" model of payments.
Allows charging per request (i.e Β£0.003 per request)
Go
Node JS
Python
1
user, _ := paypi.Authenticate("<USER_TOKEN>")
2
​
3
charge, err := user.MakeCharge(paypi.MakeChargeInput{
4
ChargeIdentifier: "<CHARGE_IDENTIFIER>",
5
})
Copied!
1
const user = await paypi.authenticate("<Subscriber secret>")
2
​
3
await user.makeCharge("<CHARGE_IDENTIFIER>")
Copied!
1
# Async
2
user = await paypi.authenticate("<Subscription Secret>")
3
await user.make_charge("<CHARGE_IDENTIFIER>")
4
​
5
# Sync
6
user = paypi.authenticate_sync("<Subscription Secret>")
7
user.make_charge_sync("<CHARGE_IDENTIFIER>")
Copied!

Dynamic Charges

Dynamic Charges allow you to change the cost of the charge at the time of your request. This is to account for processing time, MB of data processed etc. A Dynamic Charge requires a cost but also a unit to state how much of the charge was used. This unit is multiplied by the cost to calculate the total cost to the user.
Allows charging based on usage (i.e Β£0.003 per MB)
​
Go
Node JS
Python
1
user, _ := paypi.Authenticate("<USER_TOKEN>")
2
​
3
charge, err := user.MakeCharge(paypi.MakeChargeInput{
4
ChargeIdentifier: "<CHARGE_IDENTIFIER>",
5
UnitsUsed: 5,
6
})
Copied!
1
const user = await paypi.authenticate("<Subscriber secret>")
2
​
3
const unitsUsed = 4
4
await user.makeCharge("<CHARGE_IDENTIFIER>", unitsUsed)
Copied!
1
units_used = 4
2
​
3
# Async
4
user = await paypi.authenticate("<Subscription Secret>")
5
await user.make_charge("<CHARGE_IDENTIFIER>", units_used)
6
​
7
# Sync
8
user = paypi.authenticate_sync("<Subscription Secret>")
9
user.make_charge_sync("<CHARGE_IDENTIFIER>", units_used)
Copied!
​

Extra Info

Charge Timings

When a charge is made, it is logged on the analytics dashboard in close to realtime, however it may take up to 3 hours for this charge to be shown in the "Available funds" of your account.

Errors

If a charge returns an error ensure to return this error to your user.
If there was an error after a charge has been made causing an invalid response, users are eligible to request the cost of their request back, so ensure your API is working correctly before going live.
​

Examples

Some examples of using charges in different API scenarios.

PDF to JPG Converter

This API converts PDF files to JPG files through an API, and wants to use PayPI to increase user uptake and reduce development work.
PDF to JPG wants to charge a different fee dependant on the size of the PDF file to convert, but also a fixed fee for each request.
The API uses the following charges to do this:
Charge Name
Charge Type
Charge Cost
Charge Identifier
PDF Size
DYNAMIC
Β£0.005 / MB
cid-AAAAAA
Per request cost
STATIC
Β£0.01
cid-BBBBBB
This then makes a conversion of a 1MB PDF file cost a total of Β£0.015 (Β£0.005 + Β£0.01).
They implement this in their API by passing the API Key in the Authorization header.
Go
1
package main
2
​
3
import (
4
"http"
5
"github.com/Paypi/paypi-go/paypi"
6
)
7
​
8
paypi.Key = "<API_SECRET_KEY>"
9
​
10
func handleRequest(w http.ResponseWriter, r *http.Request) {
11
// Check if a user's token is valid
12
user, err := paypi.Authenticate(r.Header.Get("Authorization"))
13
if err != nil {
14
http.Error(w, "User token is unauthorized", http.StatusUnauthorized)
15
return
16
}
17
18
// Carry out PDF conversion and calculate usage...
19
pdfSize := 1 // PDF size in MB
20
21
charge, err := user.MakeCharge(paypi.MakeChargeInput{
22
ChargeIdentifier: "cid-AAAAAA",
23
})
24
if err != nil {
25
http.Error(w, err.Message, http.StatusInternalServerError)
26
return
27
}
28
29
charge, err := user.MakeCharge(paypi.MakeChargeInput{
30
ChargeIdentifier: "cid-BBBBBB",
31
UnitsUsed: pdfSize,
32
})
33
if err != nil {
34
http.Error(w, err.Message, http.StatusInternalServerError)
35
return
36
}
37
​
38
fmt.Fprintf(w, "Returning your JPG")
39
}
Copied!