billowapp / payfast

Basic Laravel Package For ITN Payments through Payfast
MIT License
27 stars 37 forks source link

Production Environment Hosts #30

Open dr-digital opened 2 years ago

dr-digital commented 2 years ago

Hi,

I'm having an issue on my production environment when validating the hosts and getting the "Not a valid host Exception'.

I see that all other environments return true when running validateHost, and the actual check only takes place in the production environment.

I've ensured the hosts in config match what Payfast provides, I've even added https:// to the hosts in the config (knowing full well this won't work), but no joy at all.

The payfast documentation uses HTTP_REFERER but this package is using REMOTE_ADDR. Could this be the issue, or is there something else at work here that I'm not seeing?

public function validateHost($request)
    {
        // alow local testing
        if (env('APP_ENV') !== 'production') {
            return true;
        }

        $hosts = $this->getHosts();

        if( !in_array( $request->server('REMOTE_ADDR'), $hosts ) )
        {
            throw new Exception('Not a valid Host');
        }

        return true;
    }

    public function getHosts()
    {
        $hosts = [];

        foreach(config('payfast.hosts') as $host) {
            $ips = gethostbynamel($host);
            if(count($ips) > 0) {
                foreach($ips as $ip) {
                    $hosts[] = $ip;
                }
            }
        }
        return array_unique($hosts);
    }
    'hosts' => [
        'www.payfast.co.za',
        'sandbox.payfast.co.za',
        'w1w.payfast.co.za',
        'w2w.payfast.co.za',
    ],