# PrestaShop

> Mobile Money and Card inside the PrestaShop checkout, refunds both ways, and one connection per shop of a multistore.

The Wajub module adds three payment options to a PrestaShop 8.1 or 9 store: **Mobile Money** and
**Card**, each with Wajub's payment form inside the checkout, and **Other payment methods**, which
takes the payer to Wajub's payment page for whatever else your account offers. It creates the order
when the customer places it, settles it from Wajub's webhook, and refunds it both ways. A payment
counts for what it collected, the customer pays the rest, and Sandbox and Live never mix.

## Requirements

| Component       | Minimum               | Note                                                                                                                            |
| --------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| PrestaShop      | 8.1                   | Tested on 8.2 with the Classic theme and 9.0 with Hummingbird                                                                   |
| PHP             | 8.1, with cURL        | The module refuses to install below this, and says why                                                                          |
| Database        | MySQL 5.6, or MariaDB | Whichever your PrestaShop runs on                                                                                               |
| HTTPS           | Required in Live      | Wajub brings payers back only to an `https://` address, and a live webhook needs one: see [Sandbox and Live](#sandbox-and-live) |
| A Wajub account | Required              | Sandbox is enough to start. Live needs an [activated account](https://docs.wajub.com/getting-started/going-live)                                      |

The module has no dependency: its API client is its own, and the zip ships nothing else.

## Install the module

1. **Upload the zip**

   In the back office, **Modules**, **Module Manager**, **Upload a module**, then pick
   `wajub.zip`. The module is not on PrestaShop Addons or wajub.com yet: ask Wajub for the zip. It holds
   one folder, `wajub/`.

2. **Open its settings**

   **Configure** opens the module's page, in four tabs: **Connection**, **Payment methods**,
   **Appearance** and **Webhooks**. PrestaShop gives that page to the SuperAdmin and to the
   profile of the employee who installed the module; **Advanced Parameters**, **Team**,
   **Permissions** gives it to others, under **Payment**, **Wajub**.

3. **Connect, then test**

   Connect the shop to your account (below), place a test order, then switch to Live.

The module adds two order states, **Awaiting Wajub payment** and **Partially paid with Wajub**. An
order paid in full moves to PrestaShop's **Payment accepted**.

## Connect your Wajub account

The shop needs one secret key per environment. Let Wajub create it with **Connect with Wajub**, or
paste your own.

### Connect with Wajub

Click **Connect Sandbox** or **Connect Live** (**Reconnect Sandbox**, **Reconnect Live** once a key is
saved) on the
**Connection** tab. The shop shows a code and opens the Dashboard's approval page in a new tab:
check that the code is the same, choose the environment, full access or a restricted key (the
permissions the module needs, payments, refunds and webhooks, are ticked), and how long the key
lasts, 90 days or **No expiry**, then click **Authorize**. The shop collects the key once, checks it
as it checks a pasted key, saves it encrypted and registers the webhook with it. The key never
reaches the browser.

| Rule                  | Detail                                                                                                                                                                                                                                                       |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Who approves          | The owner, an Administrator or a Developer of your Wajub account, or a Sandbox User for a sandbox key only, after a security code. Wajub e-mails the owner and every Administrator about every key created this way. Approve only a code your shop shows you |
| Live                  | The approval page offers it once your account is activated for live payments, to a member whose role is not limited to Sandbox                                                                                                                               |
| 15 minutes            | A request waits 15 minutes for approval. The shop must then collect the key within 15 minutes of the approval, or Wajub deletes it and you start again                                                                                                       |
| One request at a time | A request started in another tab or window replaces the first, and the first page says so                                                                                                                                                                    |

To connect, the shop sends the Dashboard its name and domain, the environment asked for, the client
type `prestashop`, a hash of its address and the permissions it needs. Any app can use the same
exchange: see [Connect your app to Wajub](https://docs.wajub.com/libraries/plugins/connect).

### When a connected key expires

A key that lasts 90 days stops working on its date, which the **Connection** tab shows. From 14 days
before it:

- every page of the back office shows &#x2A;*Your Wajub key expires soon.**, then &#x2A;*Your Wajub key has
  expired.**, to the employees who may change the module's settings, for the key each shop pays with,
  with a link to connect again (see [Back-office alerts](#back-office-alerts));
- the shop's e-mail address (**Shop Parameters**, **Contact**) gets one e-mail per key, Sandbox or
  Live, in the shop's default language. The module's scheduled task sends it, or the first page of
  the back office that shows the notice. A copy of the shop at another address sends none.

Choose **No expiry** for a store whose checkout must not stop.

### Paste your keys

Open **Paste keys instead** on the **Connection** tab, and paste the keys from [the
Dashboard](https://dashboard.wajub.com/settings/developer/keys): `sk_test.` or `rk_test.` for
Sandbox, `sk.` or `rk.` for Live. Each key is checked with Wajub before it is saved, and the page
shows only its last characters afterwards. A live key pasted in the Sandbox field is refused.

## Sandbox and Live

Each environment pays with its own key only. Sandbox without a sandbox key takes no payment at all,
even with a live key saved, and the **Connection** tab says so. The module refuses to switch to Live
without a live key, asks you to confirm before it does, and keeps a live key it would need while
Live is selected.

A payment is read and cancelled with the key of the environment it was made in, whichever
environment the shop uses now. A refund from the order page goes through only while the shop uses
the environment the order was paid in: otherwise it is refused, and the page says to switch back or
to refund from the Dashboard.

While the shop takes test payments, the header of every page of the back office shows **Wajub ·
Sandbox**, a link to the **Connection** tab, to the employees who may open the module's settings.
On a shop the public uses (debug mode off) that still takes test payments, the back office also
shows &#x2A;*Wajub is in Sandbox.**: see [Back-office alerts](#back-office-alerts).

> [!WARNING]
> **Serve the shop over HTTPS in Live**
>
> In Live, Wajub brings payers back only to an `https://` address. On a shop served over plain
> HTTP, a Mobile Money or card payer sent to their bank's or operator's page stays there, and comes
> back to the shop by hand. No live webhook can be registered at an `http://` address either: the
> order waits for **Check status**, or for the scheduled task once the cancellation delay has passed. While Mobile Money or Card is on,
> the **Connection** and **Payment methods** tabs say so in Live, and on a shop the public uses
> still in Sandbox; so does the confirmation of the switch to Live. The fix is in **Shop
> Parameters**, **General**: enable SSL, and on PrestaShop 8, SSL on all pages too.

## Register the webhook

**Register automatically**, on the **Webhooks** tab, finds or creates the shop's endpoint in your
Wajub account, subscribes it to the events below, and saves its signing secret. An endpoint that
already has the shop's address is kept, and given the events it lacks. **Get a new signing secret**
renews the secret. **Connect with Wajub** registers the endpoint too. When an update of the module
listens to more events, it adds them to the endpoint the shop registered, and keeps the others.

| Events                                                                                              | What the shop does                                                                                       |
| --------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
| `payment.succeeded`, `payment.failed`, `payment.expired`, `payment.cancelled`, `payment.processing` | Pays the order, in full or in part, or notes on it why its payment did not go through                    |
| `payment.split.processing`, `payment.split.succeeded`, `payment.split.failed`                       | Follows a payment made in instalments: its order is never given back or cancelled while it is being paid |
| `refund.created`, `refund.processing`, `refund.succeeded`, `refund.failed`, `refund.cancelled`      | Records a refund made in the Dashboard as a credit slip, and follows those made from the order page      |
| `dispute.created`, `dispute.updated`, `dispute.won`, `dispute.lost`, `dispute.closed`               | Notes the dispute on the order. Answer it from the Dashboard                                             |

The [event catalogue](https://docs.wajub.com/developer-tools/webhooks/events) describes each one.

The tab shows the shop's webhook address, `https://example.com/module/wajub/webhook` (or
`index.php?fc=module&module=wajub&controller=webhook` without friendly URLs), the last delivery the
shop accepted, and, when deliveries are refused, why and what to do. To set it up by hand, add that
address in the Dashboard, **Developers**, **Webhooks**, with the events above, and paste its signing
secret in the tab.

Wajub cannot reach a shop on your own computer: test it with `wajub listen --forward-to` and the
shop's webhook address, from the [CLI](https://docs.wajub.com/developer-tools/cli/listen), then paste the secret
`wajub listen --print-secret` shows into **Sandbox signing secret** on the **Webhooks** tab. Live webhooks need an `https://`
address.

## The checkout

### Mobile Money and Card

Each option shows Wajub's payment form inside its payment box, in your theme's font, colours and
rounding. **Appearance**, **Payment form**, forces a light or dark form. A Mobile Money payer
types the number to pay with and picks their operator from a list with its logo, the number's own
operator already chosen (a setting, on the **Payment methods** tab, finds it from the number
instead), then approves the payment on their phone. A card payer enters their card, with 3-D Secure
when the bank asks.

As soon as an option is selected, the shop prepares a payment for the cart, before the order exists:
the amount, the currency, the option's kind and the cart, nothing about the payer. **Place order**
creates the order, then activates that payment for it, with the payer's name, e-mail address and
phone number. A payment prepared and never activated is charged to no one and expires.

Without JavaScript, or when the form does not load within 15 seconds, the payer pays on Wajub's
payment page, which offers that option's kind only.

### Other payment methods

The payer goes to Wajub's payment page, which offers only what your account has beyond Mobile Money
and cards: PayPal, wallets and the like. The option shows only when something is left.

Each option shows only when your account offers it for the cart's currency, total and country
(`GET /payments/options`). When Wajub does
not answer, Mobile Money and Card show, and Other payment methods does not.

### Placing the order

The order is created when the customer places it, in **Awaiting Wajub payment**. Its confirmation
e-mail waits until it is paid. While a Mobile Money payment waits for the payer's approval, the
order confirmation page says what to do on the phone, follows the payment and updates by itself.

A payment declined, cancelled or expired, or **Use another payment method**, cancels the order and
gives the cart back, with a message, unless a payment of the order may still go through.

### When a payment does not cover the order

A payment counts toward its order for what it collected. One that covers the total pays the order;
one short of it (an instalment payment closed early, an order changed after the payment started)
moves it to **Partially paid with Wajub**, and the customer pays the rest with **Pay the rest**, on
the order confirmation page and in **Order details** of their account. The new payment, named after
the order (`Order #XKBKNABJK-2`), asks for what is left only. An instalment payment still open is
resumed when it can be, or closed keeping what it collected.

The merchant reads on the order when a second payment went through, or a payment in another
currency.

### One-page checkout modules

The module is tested with PrestaShop's own checkout. A one-page checkout module redraws the payment
options after each change of address or carrier, and has a button of its own; the module copes as
long as the options keep PrestaShop's markup:

- each option keeps its `name="payment-option"` radio, and the Wajub box (`[data-wajub-fields]`)
  and form (`form[data-wajub-form]`) stay in their option's block;
- the module watches the page: options drawn again get their form mounted again, on the same
  prepared payment. A module may also call `window.WajubCheckout.sync()` once it has drawn them;
- whatever submits a Wajub option's form, PrestaShop's **Place order**, a module's button, or a
  script calling `submit()`, goes through the same steps.

When none of that holds, the payer pays on Wajub's payment page, and the order is placed all the
same.

## Orders in the back office

The **Wajub payment** panel of an order lists its payments (status, method, amount, what counted,
environment, a link to the Dashboard), its refunds and the module's notes. **Check status** reads
the payment from Wajub and settles the order as the webhook would, for an order still waiting for
its payment.

### Orders shipped in several packages

When the products of a cart go with different carriers, PrestaShop splits the order into several
orders under one reference. The module treats them as one: one Wajub payment for their total,
**Pay the rest** and **Check status** from any of them, a refund from any of them paid back from
their payments, one list of notes, and an unpaid order cancelled with all the others. The **Wajub
payment** panel of each order, and the customer's line of what is left to pay, say that their
amounts are those of all the orders of the reference. A refund made in the Dashboard becomes one
credit slip per order, each up to that order's total.

### A payment that cannot count yet

A payment that went through, but whose refunds could not be read at that moment, does not count
toward its order yet: the order stays as it is, is never cancelled as unpaid for it, and its
**Wajub payment** panel says so. The shop reads the payment again on its own until it counts:
there is nothing to set up.

## Refunds

A **Partial refund**, **Standard refund** or **Return products** on the order page is refunded
through Wajub before PrestaShop makes its credit slip, for what that slip gives back, tax included,
also for a customer whose group sees prices without tax. The payment made last is refunded first.

A refund that Wajub or the module refuses (more than the order's payments hold, an order paid in
the other environment or never paid in full) makes no credit slip, with debug mode on or off, and
the order page says why. When Wajub refunds a part, then refuses the rest, the part refunded gets a
credit slip of its own, and is never refunded twice. A refund given as a voucher gives no money
back: nothing goes to Wajub.

The module refunds after the other modules hooked to the refund forms, even one installed after
it, so that any of them can refuse the refund first. When another module's error stops PrestaShop
once the credit slip is made, the slip still records what Wajub refunded, and a note on the order
asks you to check the products' refunded quantities.

A refund made in the Dashboard is recorded on the order as a credit slip. A refund of everything the
order was paid moves it to **Refunded**.

## Unpaid orders and the scheduled task

An order left in **Awaiting Wajub payment** is cancelled after the delay set on the **Payment
methods** tab (60 minutes by default, 0 for never), its products back in stock, unless a payment of
it may still go through or Wajub holds money for it. The same task reads again the payments that
could not count yet, and sends the expiry e-mails. A refund made from the order page that no credit
slip records a quarter of an hour on (an error in another module stopped PrestaShop before the slip,
and no webhook came) is recorded on a credit slip of its own, once Wajub says it went through.

PrestaShop's **Cron tasks manager** module runs it every hour. Without it, have your host's cron
call the address on the **Payment methods** tab every 15 minutes: **Copy** copies it. It holds a
token: keep it private. The tab says when the task last ran, and the back office warns when it has
not run for three hours.

An order you cancel has its Wajub payments that nobody started paying cancelled with Wajub too, so
that nobody pays a cancelled order. One being paid (a phone yet to confirm, a card at its 3-D Secure
challenge), one that went through, or one paid in instalments is not: Wajub cannot stop a payment
the operator or the bank is confirming. Money it takes counts toward the order, which is paid again:
the order says so, for you to refund it if the customer is not to have the order.

## Back-office alerts

The module tells the employees who may change its settings what needs their attention, on the pages
where it matters, for the shop the shop selector is on. It reads only what is saved: a page of the
back office never waits on Wajub.

| Alert                                                                               | When                                                                                                                                                                         | Where                                                               | Can be hidden |
| ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- | ------------- |
| **Wajub takes no payments.**                                                        | The environment chosen has no key, while the other one has                                                                                                                   | Every page, and the top of the module's page                        | No            |
| **Your Wajub key expires soon.**, &#x2A;*Your Wajub key has expired.**              | A key from **Connect with Wajub**, from 14 days before its date                                                                                                              | Every page                                                          | No            |
| **Wajub is in Sandbox.**                                                            | A shop the public uses (debug mode off) takes test payments                                                                                                                  | Dashboard, module manager, orders, **Payment** pages, module's page | Yes           |
| **Finish setting up Wajub.**                                                        | No key is saved                                                                                                                                                              | Dashboard, module manager                                           | Yes           |
| **Wajub is not offered at checkout.**                                               | Mobile Money, Card and Other payment methods are all off, or nothing is ticked for Wajub in a list of **Payment**, **Preferences** (currencies, countries, groups, carriers) | Module's page, **Payment** pages                                    | Yes           |
| **Wajub's scheduled task has never run.*&#x2A;, or &#x2A;*… last ran 5 hours ago.** | The task has not run for three hours                                                                                                                                         | Dashboard, module manager, orders, **Payment** pages, module's page | Yes           |

The alerts link to what fixes them; the scheduled task's says how to run it, and gives its address
to copy. **Hide for 7 days** hides an alert from the employee who clicks it, and it comes back
afterwards if what it warns about has not changed.

## Multistore

Each shop, group of shops, or **All shops** can have its own connection: environment, keys and
webhook. The back office's shop selector chooses which one the page sets, the page says so, and the
**Connection** tab says where each key comes from. A shop that connected on its own goes back to
the shared connection with &#x2A;*Use the connection saved for …** (all shops, or its
group).

Each scope that registers a webhook has its own endpoint, whose address names it
(`…/module/wajub/webhook?scope=s2`), with its own signing secret. An order is settled with the
settings of its own shop, whichever address the delivery reached.

## Personal data

The module keeps the Wajub payments of each order (ids, amounts, status, method, environment, the
customer's id), their refunds and notes. Until an order is paid, it also holds its confirmation e-mail, with the
customer's name, e-mail address and addresses (30 days at most, deleted as soon as the module
cancels the order). Apart from that e-mail, it keeps no name, e-mail address, phone number or card. With PrestaShop's GDPR module, a customer's
data export includes their Wajub payments, and erasing a customer deletes the e-mails held and
unlinks their payments, which stay as the record of the orders' money.

## Uninstall the module

Uninstalling, or **Reset** in the module manager, deletes the module's settings, keys and signing
secrets, and the confirmation e-mails it held. It keeps your orders, their payments, and the record
of their Wajub payments, refunds and notes, in three tables: `wajub_payment`, `wajub_refund` and
`wajub_order_note`, with your shop's table prefix (`ps_wajub_payment` by default). PrestaShop's
confirmation before uninstalling names them. A new install finds that record again.

To erase that record too, delete the three tables after uninstalling. The orders and their
PrestaShop payments stay, but a new install no longer knows which Wajub payments paid them, and
cannot refund them through Wajub from the order page.

The keys **Connect with Wajub** created, and the webhook endpoints, stay in your Wajub account:
delete them from the Dashboard, **Developers**.

## Troubleshooting

| Symptom                                                               | Cause                                                                                                                                                                      | Fix                                                                                                            |
| --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| No Wajub option at checkout                                           | The environment chosen has no key, every method is off, nothing is ticked for Wajub in **Payment**, **Preferences**, or the account offers nothing for the cart's currency | Follow the [back-office alert](#back-office-alerts), or check your account's payment methods for that currency |
| Orders stay in **Awaiting Wajub payment**                             | The webhook is not registered, or its deliveries are refused                                                                                                               | **Webhooks** tab: register it, or follow the advice it shows                                                   |
| Unpaid orders are never cancelled                                     | The scheduled task does not run, as its [back-office alert](#back-office-alerts) says                                                                                      | Install PrestaShop's **Cron tasks manager** module, or have your host's cron call the address the alert gives  |
| The form does not load                                                | Embedding restricted in the Dashboard, or Wajub.js blocked                                                                                                                 | Add the store's address in **Settings**, **Checkout embedding**; the payer can still pay on Wajub's page       |
| A Mobile Money or card payer stays on their bank's or operator's page | The shop is not served over HTTPS                                                                                                                                          | **Shop Parameters**, **General**: enable SSL, and on PrestaShop 8, SSL on all pages                            |
| A credit slip on the order page shows more than Wajub refunded        | **Product(s) price, excluding amount of initial voucher**, for a customer whose group sees prices with tax: PrestaShop's order page lists the slip before the voucher      | Nothing to fix: Wajub refunded what the slip's PDF says, and the module counts that                            |
| **Your Wajub key has expired.**                                       | A 90-day key from **Connect with Wajub**                                                                                                                                   | **Connect with Wajub** again, and choose **No expiry**                                                         |

- [Webhooks](https://docs.wajub.com/developer-tools/webhooks): Signatures, retries and events.
- [Go live](https://docs.wajub.com/getting-started/going-live): Activate your account and switch to live keys.
