Jump to content
ffub

Trouble using hooks with a PayPal module I'm writing

Recommended Posts

Hi all,

I'm writing a utility module to send basic payments to PayPal and then to validate the IPN response that PayPal returns. The module is a simple wrapper around their Website Payments Standard api. You can view the module here:

https://gist.github.com/4493175

When you initiate a payment the customer is redirected to PayPal where they complete the transaction, and then returned to the "thank you" page as set in the module settings. PayPal sends a copy of the transaction and its status (authorised|cancelled|refund etc) to a specified listener. Once your site receives this copy, you validate its authenticity by sending it back to PayPal who confirm that they sent it to you. So far so good.

The problem I am having is adding a hook to the method that processes a successful payment validation from another module.

I'm trying to use this code to hook the method but having no luck:
 

/**
 * Initialise the module
 *
 */
public function init() {
	
	// Add a hook after to update member role and expiry date
	$this->addHookAfter('PaymentGatewayPayPal::processIpn', $this, 'processPayment');
		
}

/**
 * Process the successful payment
 *
 */
public function processPayment($payment) {

	mail($myEmailAddress, "Test payment processing", "This ran");

}


Can anyone suggest where my hook might be going wrong? I'm also keen on any general tips on how the module could be made more robust before I release it.

Best regards,
Stephen

  • Like 1

Share this post


Link to post
Share on other sites

Have you prefixed procedsIpn method with three underscores?

Share this post


Link to post
Share on other sites

Ah, sorry just now noticed your gist. I'm on mobile so cannot take good look now. Can you provide more code about this module that is using the hook? It should be autoload, but paymentGateway module problem shouldn't.

Share this post


Link to post
Share on other sites

Both modules are singular and autoload. The reason I'm autoloading the PayPal module is because it listens out for IPN responses. This could be done with a separate template that loads the module but I preferred the neatness of not having to set up any templates on install. 

Share this post


Link to post
Share on other sites

 <guess>

Looks like you are asking PW to hook a static method but the method isn't declared static nor is it called as static. Could you try adding the hook directly to your singular module's processIpn() method instead?

Like so...

$this->modules->get("PaymentGatewayPayPal")->addHookBefore("processIpn" , ... );

 </guess>

Aside: Paypal IPNs are interesting beasts. You still need to apply business logic after you get a valid result back from the IPN service though -- particularly checking the transaction ID hasn't been used before so as to avoid potential replay attacks.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks, netcarver! Sound advice and I believe you've helped me solve it.

The function isn't static, no. I didn't realise that method of attaching a hook was only for static functions. I've not looked into how PW actually makes the module methods hookable but I had sort of assumed that it creates its own methods without the underscores which call the method and associated hooks. If so, I guess these are static. I have pretty rudimentary knowledge of both object orientated PHP and PW's internals though.

By zero-ing on line 53 you've made me realise my error. The function was getting called but as I was calling it with the underscores it was not calling the associated hooks. I've removed the underscores and the hooks are now working.

Much of the business logic you'd use to verify the payments is outside of the scope of the module as it stands, such as checking the email and postal address of the transaction against an order, or checking the orderID matches that returned by PayPal. That said, checking the transaction ID is certainly something that could be done if I expand it to keep track of all payments it receives. Any other suggestions welcome!

Stephen

  • Like 2

Share this post


Link to post
Share on other sites

Hi Stephen, Have you made any progress with this? I am really interested in utilising this module along with Apeisa's ShoppingCart/checkout module.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...