The Gateway API is responsible for allowing your users to buy points for real money. These purchases are either done manually or using online payment processors, such as PayPal or Netbilling. To buy points, a user must select the amount of points they want to buy and the payment processor they want to use. You can select to use just one particular payment gateway or multiple and let the user decide which one they want to use.
myCred comes with several built-in gateways located in the
The buyCred module handles your installed gateways, built-in or custom ones, manages your pending payments and logs of purchases. The module will construct and load a gateway in the front-end when:
- A logged in user visits any front-end page *.
- If a new purchase request is detected.
- If a Callback from a payment processor is detected.
* If the gateway needs to detect when a buyer returns to the website in order to process a payment (not using callback), the gateway will be loaded by buyCred on each front-end page load where the website is visited by someone who is logged in.
In order to buy points, four pieces of information needs to be submitted to the gateway API:
- The Amount of points to purchase
- The point type
- The gateway ID selected to handle the payment and
- A security token.
If the purchase is made as a gift, the recipients ID is also needed. Once the security token is verified, the requested gateway is constructed and the gateways
buy() method is called upon to handle the purchase request. If the request is made by a user that is excluded or if the security token has expired, the gateway will not be loaded and the page will simply refresh.
A logged in user requests to purchase points using the mycred_buy or mycred_buy_form shortcodes.
The user clicks on the purchase link or submits the purchase form.
The request is validated and minimum amount is enforced. A pending payment is saved.
The checkout page is loaded to manage local payments or redirect to external servers.
The buyCRED add-on supports three types of payment gateways:
The buyers payment information is collected locally and sent to the payment processor. The buyer never leaves your website.
The buyer is redirected to an external payment processor where they pay and are redirected back to your website.
The user makes a purchase request but no payment information is collected. Payments are manually handled.
Example: Bank Transfer
Each gateway is a class based of the myCred_Gateway abstract class. A gateway class must have a constructor and the buy class method. All other methods are optional:
||Yes||Identifies the gateway and it's settings.|
||Yes||Handles new or returning purchase requests. Also responsible for loading the checkout page.|
||No||In case the gateway has settings that needs to be accessed in the admin area.|
||No||If the gateway has settings, this method should be used to parse and sanitize the gateways settings.|
||No||Optional method to handle Callbacks (if needed).|
||No||Optional method to detect and act upon returns from an external payment processor.|
Creating a new gateway for buyCred is relatively easy if you feel comfortable with PHP and understand how the payment processor you want to use, works. In order for your gateway to work with myCred, your gateway must be using the myCred_Gateway abstract class. This class will provide you with a large pallet of helpful functions to make construction as easy as possible.
Basic buyCRED Gateway example.
First we need to construct our gateway using the myCred_Gateway class. This will register our gateway settings and apply defaults to existing settings.
While our class constructor only uses one variable; the gateway module settings, the parent constructor uses two:
- The Gateways arguments – This holds the gateways ID and settings.
- The buyCred add-on settings that were passed down.
The following arguments are available for Gateways:
||string||Yes||A unique ID for the gateway.|
||string||Yes||Gateway label used in the admin area.|
||array||Yes||Array of the default settings for this gateway. Can not be empty.|
||string||No||Image url for the logo to show on the checkout page.|
The buy() class method is called when a user makes a purchase request and this method is expected to parse the request, construct arguments needed for the payment processor and load the checkout page.
You can access your gateway settings using $this->prefs and the myCred_Gateway abstract class provides a large set of methods that you can access to help handle requests. Here are some of the most used methods:
||Returns the point type the user selected to buy. This method also makes sure users can only buy point types you have set to sell.|
||Returns the cost of the amount on points a user wants to buy based on the exchange rate you set. Also takes into account any custom exchange rate you might have set for a particular user.|
||Returns the user ID of the recipient of the purchase. This might be the buyers ID or if you have gifting enabled, the recipients user ID.|
||Saved a new pending payment request and returns the unique purchase request ID. This ID is used to identify the request in the admin area or in callbacks from payment processors.|
||Returns the URL set for the cancellation page in your buyCRED settings.|
||Returns the URL set for the thank you page in your buyCRED settings.|
||Returns the callback URL a gateway must use (if the gateway supports callbacks).|
Once a purchase request has been saved, we need to either collect a users payment details, show them how to complete their payment (manual gateways) or redirect them to the payment processor.
This is done using the “Checkout” page which is a page override that buyCRED generates before your theme’s style / page layout is loaded. This is mainly to prevent third-party conflicts and to prevent other scripts and styles from being used. In future versions of myCred, this might however change, and instead use a dedicated checkout page.
The checkout page is rendered using the get_page_header() and get_page_footer() class methods. The content part can either be added manually via your class or use one of the built-in content render methods:
||Renders a billing form with built-in support for MarketPress and WooCommerce billing details.|
||Renders the order table showing the order name, point amount and cost.|
||Renders a form populated with fields you provide. Will automatically submit the form once the page has loaded or if the user clicks on the continue link.|
||Renders debug details. Intended for testing only!|
Example: The Bank Transfer gateway inserts it’s own custom checkout page content.
Example: The PayPal gateway uses the built-in redirection on the checkout page.
Example showing how the buy() method in the gateway class can be used when a user needs to be redirected to a remote payment processor.
buyCRED Callbacks refer to the instances when a remote payment processor contacts your website in order to inform of updates regarding a pending payment, e.g. the payment being completed / captured. Callbacks are optional of course, if your gateway does not support it, you do not have to use it.
All callbacks have to use the proper callback identification, and you can use the
callback_url() method to get this URL for your gateway.
Once a valid callback is received, the gateway in question is loaded and the
process() class method is called upon to handle the call. Most payment processors offer a callback or “postback” feature and how this call is processed varies.
||Retrieves the pending payment object.|
||Completes a pending payment by paying out the actual points to a users account.|
||Trashes a pending payment. Used mainly when payment is completed.|
||If enabled by the user, this method is used to log events associated with a pending payment request. For example when a pending payment could not be paid out. Uses WordPress comments.|
||Used to create a unique transaction id for a pending payment. This ID is used when the transaction is handled remotely and we need to find the pending payment on callbacks.|
All gateway must be registered using the mycred_setup_gateways filter. All installed gateways are added in an associative array where the gateways unique ID is used as key and the value being an array of gateway details.
Custom gateways should be loaded using the mycred_buycred_load_gateways action hook. By the time this action fires, the abstract gateway is available and it ensures that your gateway is loaded before buyCRED processes any payment requests.