SigalZ's avatar
Level 5

Need help implementing eCommerce with Guzzle

Hello,

This is the first time I'm using GuzzleHttp and a payment gateway.

I'm trying to implement it on an old Laravel site version 5.7.

Please excuse me if my question is unclear, but I'm very confused.

I am sending a payment request to the payment gateway, from my local machine.

I set the "notify_url" to https://mysite.co.za/payfast-notify

I create this route in web.php:

Route::post('payfast-notify', 'PayfastController@payfastNotify');

Their documentation says this: (https://developers.payfast.co.za/docs#step_4_confirm_payment)

Confirm payment is successful Before the customer is returned to the URL you specified in the “return_url” parameter, Payfast will send a notification to your “notify_url” page.

4.1. Setting up your notification page:

On receiving the payment notification from Payfast, return a header 200 to prevent further retries. If no 200 response is returned the notification will be re-sent immediately, then after 10 minutes and then at exponentially longer intervals until eventually stopping.

Their code example:

<?php
// Tell Payfast that this page is reachable by triggering a header 200
header( 'HTTP/1.0 200 OK' );
flush();

define( 'SANDBOX_MODE', true );
$pfHost = SANDBOX_MODE ? 'sandbox.payfast.co.za' : 'www.payfast.co.za';
// Posted variables from ITN
$pfData = $_POST;

// Strip any slashes in data
foreach( $pfData as $key => $val ) {
    $pfData[$key] = stripslashes( $val );
}

// Convert posted variables to a string
foreach( $pfData as $key => $val ) {
    if( $key !== 'signature' ) {
        $pfParamString .= $key .'='. urlencode( $val ) .'&';
    } else {
        break;
    }
}

$pfParamString = substr( $pfParamString, 0, -1 );

I am not sure how do I return 200 response? How can I test this?

I created a controller called: PayfastController.

In it I have this method:

After sending the request from my local machine, I check the logs on the live server, but no log file was created.

Not sure how to implement this procedure and how to test.

I read Laravel documentaion about Fake, but I don't really understand it, I'm not sure if that can help me test the response that should go to the nofiy_url?

As I said, very confused.

Please can someone help?

0 likes
2 replies
martinbean's avatar

I am not sure how do I return 200 response? How can I test this?

@sigalz You just return a response from your controller. It will have a 200 OK status code by default unless you explicitly specify a different one:

class PayfastController extends Controller
{
    public function payfastNotify()
    {
        return response('Request received.'); // Will be 200 OK response by default
    }
}

After sending the request from my local machine, I check the logs on the live server, but no log file was created.

You say you’re sending a payment request from your local machine. Your callback URL won’t be hit by the payment gateway if your website is not publicly accessible. You will need to specify a public URL. From their docs:

If you are testing locally you will need to have a publicly accessible URL (notify_url) in order to receive the notifications, consider using tools such as NGROK or Expose to expose your local development server to the Internet.

Also, If the payment gateway is also half-decent, then they should have a dashboard where you can see any webhook attempts, and the response received from each attempt.

SigalZ's avatar
Level 5

@martinbean Thank you very much.

You say you’re sending a payment request from your local machine. Your callback URL won’t be hit by the payment gateway if your website is not publicly accessible. You will need to specify a public URL. From their docs:

I am calling the payment request from my local machine but the callback URL (notify_url) I supply is on my live site.

Please or to participate in this conversation.