Your plugin can add a new online payment method (payment gateway). RISE handles the rest: the Pay button, checking the amount, recording the payment only once and adding it to the invoice or order.
Step 1: Register the gateway
Create a class that extends App\Libraries\Payment_gateways\Payment_gateway. Then register it for your payment method type in your plugin's index.php file:
register_payment_gateway("my_gateway", "\\MyPlugin\\Libraries\\My_gateway");
Add the settings of the payment method with the app_filter_payment_method_settings filter, like before.
A payment method with a registered gateway gets the normal Pay button on invoices. In the store's Payment > Order > Invoice flow, it gets a Confirm and pay with ... button on the checkout page. You don't need the app_hook_invoice_payment_extension hook.
Step 2: Write the two methods
create_checkout($payment) starts the payment with the provider. Return one of these:
- array("checkout_url" => ...) to send the buyer to the provider's page.
- array("checkout_form" => array("action" => ..., "fields" => array(...))) to post a form to the provider.
To save the provider's references, like a session ID, also add "gateway_data" => array(...).
If something goes wrong, throw an exception. The message is saved in the log file, and the buyer sees a general error. This way, setup problems (like a missing API key) are not shown to buyers. If the buyer can fix the problem (like a wrong quantity), throw \App\Libraries\Payment_gateways\Payment_buyer_exception. Its message is shown to the buyer.
verify_payment($payment, $gateway_data, $request_data) checks the payment with the provider. Return:
array(
"status" => "succeeded", //or "failed" or "pending"
"transaction_id" => ...,
"amount" => ...,
"currency" => ...
)
Always take the amount and currency from the provider, not from the request.
Line items
Some providers need the items of the payment, for example the price IDs of a Paddle checkout. $payment["items"] has the lines of the invoice, order or subscription:
array(
array(
"title" => ..., "description" => ..., "quantity" => ..., "unit_type" => ...,
"rate" => ..., "total" => ..., "item_id" => ...,
"plugin_data" => array("paddle" => array("price_id" => ...))
)
)
"plugin_data" holds the values that plugins saved with the item. See Add your own data to store items from a plugin - Item fields, buyer inputs and order item values.
Always charge $payment["amount"]. It can be less than the total of the items, for example when a client pays part of an invoice.
Your own payment page
If the gateway opens a payment page inside RISE (for example an inline checkout), you can show the same header as the store checkout. It has the back link, the title and the steps Cart, Order details and Payment.
echo view("orders/checkout_header", array(
"active_step" => "payment",
"back_url" => $payment["cancel_url"],
"back_text" => "Back to checkout",
"title" => "Payment",
"login_user" => $login_user
));
All values are optional. Without them it shows the checkout header of the store.
Step 3: Send the buyer back
After the payment, the provider must send the buyer back to $payment["return_url"] (payment_redirect/index/). It accepts GET and POST. RISE then checks the payment, compares the amount and currency, makes sure it is added only once and records it.
Webhooks
If the provider also sends a webhook, call complete_online_payment() from your webhook controller. It is safe to call it from the webhook and the return url, RISE records the payment only once.
$Payment_processor = new \App\Libraries\Payment_processor($this);
$result = $Payment_processor->complete_online_payment($code, array(), array("redirect" => false));
Answer the webhook by the "status" of the result:
| Status | Answer |
| completed (success is true), not_found | 200. The payment is recorded (not_found: the buyer's return finished it already). |
| pending | 200 or an error, as the provider needs. The payment is not paid yet. |
| processing | An error (ex: 503), so the provider sends it again later. Another request is recording it right now. |
| error | An error (ex: 500), so the provider sends it again later. It couldn't be saved now (ex: the database restarted). Nothing is saved. |
| failed | 200, and check the log. The payment doesn't match. |
If the server stops while a payment is recorded (ex: a restart), nothing is lost. RISE saves the order and its payment together, so a part is never saved alone. The next webhook or return of the same payment finishes it, without a second order. This is why the webhook must get an error answer when it is not finished: then the provider sends it again.
Check unpaid payments again
Sometimes the provider takes the payment, but the webhook and the buyer's return never arrive. For example, the webhook URL was wrong, or your site was down until the provider stopped trying. If your verify_payment() can ask the provider without the return request (it uses only $gateway_data), add this method to your gateway class:
public function can_recheck_payments() {
return true;
}
Then the cron job checks the unfinished payments of this method with verify_payment(), and records the paid ones like the webhook does. It starts 10 minutes after the payment was started. It waits longer each time (up to 12 hours) and stops after 30 days, or when verify_payment() returns "failed". It checks up to 20 payments in each cron run.
When the provider sends the invoice
Some providers always send their own invoice to the buyer, for example a merchant of record like Paddle. Add this method to your gateway class:
public function issues_invoices() {
return true;
}
Then RISE works like the Invoice by provider option of the payment method is always on:
- The method is not shown on invoices. It is shown on the store checkout page and for your own payments (create_online_payment()).
- A paid store order gets no invoice. The payment is saved on the order (payment_method_id, transaction_id, paid_amount, paid_at), and the order is released.
- Recurring items don't start RISE subscriptions, because the provider bills the renewals.
To decide for each payment, use the filter app_filter_create_invoice_after_payment. It gets true or false, and array("payment" => ..., "transaction" => ..., "payment_method_info" => ...). Return true to create the invoice.
app_hooks()->add_filter("app_filter_create_invoice_after_payment", function ($create_invoice, $data) {
return $create_invoice;
});
To start (or skip) the RISE subscriptions of an order, use the filter app_filter_create_subscriptions_of_order. It gets true or false, and array("order_info" => ...).
To show more on the payment card of the order, for example a link to download the provider's invoice, use the action hook app_hook_order_payment_info_extension. It gets array("order_info" => ...). It runs on the order page of the client and of the team members.
After a payment
The action hook app_hook_online_payment_completed runs after every completed online payment. It gets array("payment" => ..., "transaction" => ...).
Removed
- pay_invoice/get_paytm_checksum_hash and invoice_payments/get_paytm_checksum_hash. They signed amounts that came from the browser.
- Paytm::get_paytm_checksum_hash() now always returns false. Use create_online_payment() instead.
- The view invoices/_paytm_payment_form.php.
Payment_processor::init_online_payment() and process_successful_payment() still work for old code. Payments started before the update are still completed the old way.