Your plugin can take online payments for things that are not invoices, for example a payslip, a donation or a booking. It works with every payment method that has a gateway: Stripe, PayPal, Paytm or a gateway added by a plugin.
Step 1: Register a payment context
Call this from your plugin's index.php file:
register_payment_context("my_plugin_payslip", array(
"on_success" => function ($payment, $transaction) {
// mark payslip $payment["context_id"] as paid with $transaction["amount"] and $transaction["transaction_id"]
return "my_plugin/payslips/view/" . $payment["context_id"]; // optional page to open after payment
},
"can_pay" => function ($payment) {
return true; // optional, return false or an error message to stop the payment
}
));
- on_success runs once for each payment, after the provider confirms it and the amount and currency match.
- can_pay is optional. It runs before the payment starts.
Step 2: Start the payment
In your controller, first check that the user can pay for this record. Then start the payment:
$Payment_processor = new \App\Libraries\Payment_processor($this);
$result = $Payment_processor->create_online_payment(array(
"context" => "my_plugin_payslip",
"context_id" => $payslip_id,
"amount" => $amount, // from your database, never from the request
"currency" => "USD",
"payment_method_id" => $payment_method_id,
"title" => "Payslip #" . $payslip_id,
"redirect_to" => "my_plugin/payslips/view/" . $payslip_id,
"data" => array() // anything on_success needs later
));
The result has:
- success and checkout_url: send the buyer to this URL.
- success and checkout_form: post this form.
- success false and message: the payment can't start. Show the message.