Payment API
In order to add new payment method to WPJB, you need to do few things. Create new WordPress plugin, create class that will extend Wpjb_Payment_Interface and apply filter that will register new payment method.
Below you can download working example that will add Payment by cash to WPJobBoard, the only thing you need to do is upload it to wp-content/plugins directory and activate “WPJobBoard Mod” plugin in wp-admin/plugins.php section.
If everything will be ok, in Add Job form “Payment Method” field should appear with options “PayPal” and “Pay by cash”.
Implementing new payment class
At the begining of the article i mentioned that the new payment method should implement Wpjb_Payment_Interface, this basically comes down to having class with 5 mandatory methods:
- getEngine()
(string) payment engine name this method should return lowercase a-z string uniquely identyfying your payment engine - getTitle()
(string) shuld return title that will be visible to end user - getForm()
(string) class name of an object that is responsible for generating payment method configuration - render()
(string) should return payment button or text informing how to make payment - processTransaction(array $post, array $get)
(null) validates if the payment was correct. If the payment is incorrect then processTransaction() should throw an error. This method is executed when the payment gateway you are integrating with received a payment and send a request to your site to check if everything is ok
This is a basic scheme for payment integration class
class Wpjb_Payment_MyPaymentGateway extends Wpjb_Payment_Abstract { public function __construct(Wpjb_Model_Payment $data = null) { $this->_data = $data; } public function getEngine() { return "my_payment_gateway"; } public function getTitle() { return "My payment gateway"; } public function getForm() { return "MyPaymentGatewayForm"; } public function bind(array $post, array $get) { // this is a good place to set $this->data $this->setObject(new Wpjb_Model_Payment($get["id"])); parent::bind($post, $get); } public function processTransaction() { $post = $this->_post; // $_POST $get = $this->_get; // $_GET if($post["id"] < 1) { throw new Exception("Incorrect something"); } // payment verification vary for each payment engine // you will need to figure this one on your own // note that as a parameters you receive $post and $get // which are the same as $_POST and $_GET // this should allow you to validate payment } public function render() { $html = ""; $arr = array("action"=>"wpjb_payment_accept", "engine"=>$this->getEngine()); $data = $this->_data; /* @var $data Wpjb_Model_Payment */ // online payment gateways usually require you to pass notify url $html .= "Notify URL: " . admin_url('admin-ajax.php')."?".http_build_query($arr) . "<br/>"; // and complete URL that is url when user should be redirected after payment $html .= wpjb_link_to("step_complete", $this->_data) . "<br/>"; // and of course amount to pay and currency $html .= "To pay: " . ($this->_data->payment_sum-$this->_data->payment_paid) . " " .$this->_data->payment_currency; if($this->conf("message")) { // we are adding now message from payment configuration form // that will be created below $html .= "<div>".$this->conf("message")."</div>"; } $html .= "<br/><br/>"; return $html; } }
Please note that the class has also __construct() and getObject() methods which you can simply copy and paste them into your own class.
Below is minimal version of the configuration form
class MyPaymentGatewayForm extends Wpjb_Form_Abstract_Payment { public function init() { parent::init(); $this->addGroup("my", __("My Group", "wpjobboard")); // adding custom textare field to the form $e = $this->create("message", "textarea"); $e->setValue($this->conf("message")); $e->setLabel(__("Message", "wpjobboard")); $this->addElement($e, "my"); } }
Registering your payment method
Now all you need to do is register your newly created payment integration using wpjb_payment_list filter
function wpjb_payment_mypaymentgateway($list) { global $wpjobboard; $cash = new Wpjb_Payment_MyPaymentGateway; // registers new payment method $list[$cash->getEngine()] = get_class($cash); return $list; } add_filter('wpjb_payments_list', 'wpjb_payment_mypaymentgateway');
If you wonder where to put this code. You can either put it in your theme functions.php file or for more experienced users you can build it as a new plugin like the Payment API example does (this is actually recommended approach although harder to complete).
Hi, Is it possible for the job to be posted immediately once “paid by cash” is selected? out of the box, it only allows the job poster to post their job and then the admin has to approve it. I want to let the job be posted automatically as the job poster will then receive an email from the admin (manually)
Thanks
Hi, since version 4.1.0 in wp-admin / Settings (WPJB) / Job Board Options panel you can select which jobs (free, paid or imported) will be moderated.
I’m trying to figure out how to get Authorize.net hooked up to WPJB and I’m having a tough time. Authorize.net seems to require Woo Commerce in order to connect, but I can’t figure out how to get it to show up on the WPJB Configurations. I’m really new to payment gateways, any help would be much apprecaited!
Hi,
you do not need WooCommerce for that, in order to create Authorize.net payment gateway you would can start by customizing this https://dl.dropboxusercontent.com/u/30001999/snippets/wpjb-payment-cash.zip payment gateway, as far as the Authorize.net API goes i am afraid i can’t help with that as i am not realy familiar with it.
Any chance you can fix the file download for this example please
How can I debug function processTransaction()?
Hi,
hmm, i suppose it depends on what problem you are having? If you would like to see what data are you receiving then most likely it will be best to print the debug information to a file and check the file later as usually the processTransaction() method is called by the payment gateway server, ie. it happens in the background and you cannot show any debug information in the browser.
THANK YOU so much for responding, and so quickly. 🙂
How can the payment gateway server call a function on my server?
I feel like I understand the big picture (with a few exceptions) by discecting the PayPal and Stripe files with these same functions, but I’m just not seeing how processTransaction() is triggered (except in /plugins/wpjobboard/application/libraries/Module/AjaxNopriv/Payment.php, line 31) but even if I try to var_dump $result, I see nothing.
I assume these comments in function processTransaction() …
// payment verification vary for each payment engine
// you will need to figure this one on your own
// note that as a parameters you receive $post and $get
// which are the same as $_POST and $_GET
// this should allow you to validate payment
… mean that this is where I need to put the (heavily modified) API code I received from Plugnpay (our payment gateway), correct?
From Stripe.php and PayPal.php, I see processTransaction() returns an array of data (external_id, paid). Where does this data “go”? How can I see that data?
I think I’m getting closer to figuring this out.
– Strip embeds an iFrame with their own back end on this.
– PayPal does an SSL socket connection to a paypal URL
But other than the jQuery visibility inside the wpjb-grid DIV and the placeorder function in frontend-payment.js, what else happens? How/when is processTransaction() actually triggered?
Note: I need to do a Curl call to Plugnpay. I’ve figured out how to pass the data to admin-ajax.php and see the data there, but my Curl job never seems to run. [I’ve got debugging set up around my Curl job as well.]
Your proccessTransaction() will be called when the Plugnpay will send a confirmation/notification request to /wp-admin/admin-ajax.php?action=wpjb_payment_accept
This request will call the Wpjb_Module_AjaxNopriv_Payment:acceptAction() in wpjobboard/application/libraries/Module/AjaxNopriv/Payment.php file.
THANK YOU for sticking with me here while I try to understand.
Does the application have to go away to the (third party) Payment Provider site before returning to our site with ?action=wpjb_payment_accept?
Or is there a way I can somehow do the Curl request in one of the functions (above), and if so, where would I put my Curl request to the Payment Provider (with which I would return upon success ?action=wpjb_payment_accept)?
You do not need to send the user to a third-party page, but ideally once the payment is processed the payment provider should send a confirmation back to ?action=wpjb_payment_accept
How to do a cURL request depends on your payment gateway i suppose, to get started you can check the wpjobboard/public/js/wpjb-stripe.js file it unbinds the default action when “Place Order” button is clicked and runs the Stripe code
jQuery(function($) {
$(".wpjb-place-order").unbind("click").bind("click", WPJB.stripe.placeOrder);
});
So you could for example bind the place-order button to perform an AJAX request which will create the payment and run your cURL function.
An easier method would be to use the wpjb_payment_created action, which is executed after clicking the “Place Order” button and once the payment object is saved in the database
add_action( “wpjb_payment_created”, function( $payment, $form, $gateway ) {
// do cURL request here
}, 10, 3 );
add_action( “wpjb_payment_created”, function( $payment, $form, $gateway ) {
// do cURL request here
}, 10, 3 );
Super cool – I’ve got it sending my CURL requests now.
Once I get those debugged and a “success” response from CURL, do I just mark the order as being paid from within that function (above)?
Or do I still need to send a confirmation/notification request to /wp-admin/admin-ajax.php?action=wpjb_payment_accept, and if so, what do I need to “return” from that function (above)?
If you know that the payment was approved then i think you can mark it as completed in your function.
If you have access to the $payment variable you can do that like this
$payment->payment_paid = "100"; // actual amount paid here
$payment->external_id = rand(0,100); // some unique reference id from your payment gateway
$payment->paid_at = current_time('mysql', true);
$payment->status = 2;
$payment->save();
$payment->accepted();
$payment->log(__("Payment verified manually.", "wpjobboard"));
If you would like to see how the WPJB itself is handing the completed payment you can check in the wpjobboard/application/libraries/Module/AjaxNopriv/Payment.php file the acceptAction().