When you’re gateway is called upon to process an order, you’ve got some work to do. Payment Gateways are responsible for collecting payments from the customer and verifying that the payments have gone through. When the payments have been completed, they need to mark the order as completed so that the theme can take over and make sure the customer gets what they paid for.
Process Orders
When your gateway is called on to process an order, the process()
method of your gateway will be called. The process method will be given two arguments: the order being processed, and the configuration options generated from your form()
‘s response.
In short, the order object will contain all the information you need to process an order. It contains data like the total purchase amount, currency, items purchased, etc.
get_id()
Returns the Order ID.
get_author()
Returns the User ID of the creator of the order
get_ip_address()
Returns the IP Address used to create the order
get_return_url()
Returns the URL to redirect to for processing the order.
get_cancel_url()
Returns the URL to redirect to for selecting a new gateway.
get_currency()
Returns the current currency’s code.
get_total()
The total price.
Here is a quick example you can use to get familiar with the order object.
public function process( $order, $options ){ echo 'Order Details'; echo $order->get_description(); echo 'Total: ' . $order->get_total(); } |
You can use this information to inform your payment processor about the required payment.
Redirecting Back and Forth
If for some reason you need to send the user to another site, like your payment processors website, in order to continue with the payment, its easy to get them back.
Whenever the user visits the URL returned by $order->get_return_url()
, your gateway’s process method will be ran again. This means that you can send them to another site, do work over there (authenticate their user credentials, collect payment information, etc), and then send them back to your payment gateway afterwards.
Cancelling out of Your Gateway
Some times you want to provide your customer with a way to select a different gateway. Maybe they thought they had valid credentials to use your gateway, but it turns out they don’t. Maybe something else happened that is stopping the user from using your gateway. Never fear, help is here.
To let a user select a new gateway, simply redirect them to the URL returned by $order->get_cancel_url()
. They will sent back to their Order Summary page and be given a chance to use a different payment method.
Completing the Order
After you’ve collected a payment, its time to give the customer their purchase. To do this, simply call the $order->complete()
method. This will notify the theme that the customer has paid, and it is time to activate their purchase.
After you call the complete()
method, you’re done.
More Resources
Now that you know how to create a Payment Gateway, you should check out the rest of our Payments API. You never know what you might create.