ffub Posted January 9, 2013 Share Posted January 9, 2013 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/4493175When 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 1 Link to comment Share on other sites More sharing options...
apeisa Posted January 9, 2013 Share Posted January 9, 2013 Have you prefixed procedsIpn method with three underscores? Link to comment Share on other sites More sharing options...
apeisa Posted January 9, 2013 Share Posted January 9, 2013 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. Link to comment Share on other sites More sharing options...
ffub Posted January 9, 2013 Author Share Posted January 9, 2013 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. Link to comment Share on other sites More sharing options...
netcarver Posted January 9, 2013 Share Posted January 9, 2013 <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. 1 Link to comment Share on other sites More sharing options...
netcarver Posted January 9, 2013 Share Posted January 9, 2013 Also, are you sure line #53 of your gist is getting called? 1 Link to comment Share on other sites More sharing options...
ffub Posted January 9, 2013 Author Share Posted January 9, 2013 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 2 Link to comment Share on other sites More sharing options...
NooseLadder Posted February 27, 2013 Share Posted February 27, 2013 Hi Stephen, Have you made any progress with this? I am really interested in utilising this module along with Apeisa's ShoppingCart/checkout module. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now