thephpleague / omnipay

A framework agnostic, multi-gateway payment processing library for PHP 5.6+
http://omnipay.thephpleague.com/
MIT License
5.91k stars 925 forks source link

How to set a http proxy server ? #609

Closed MircoBabin closed 3 years ago

MircoBabin commented 3 years ago

Dear Omnipay,

I installed the default settings of Omnipay with composer require league/omnipay:^3 omnipay/multisafepay.

I want to use Telerik Fiddler as proxy server to debug an issue I have with the multisafepay gateway. But I don't know how to set a proxy server. I know the requests are HTTPS requests, but that's not a problem, Fiddler can act as an HTTPS man-in-the-middle proxy and intercept anything.

This is the code I'm currently using in my project. I want to implement the commented line.

    protected function gateway()
    {
        $gateway = Omnipay::create('MultiSafepay_Rest');

        $gateway->initialize([
             'apiKey' => $this->apikey,
             'locale' => $this->locale,
             'testMode' => $this->testaccount,
         ]);

         //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
         //This is what I wan't to achieve
         //$gateway->getHttpClient()->setProxy('http://localhost:8888');
         //
         //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        return $gateway;
    }

    //For clarity, this is how the payment link is created
    public function pay($urlOrigin,
                        $transactionId,
                        $amountCurrency, $amount,
                        $description)
    {
        $parameters = [
            'transactionId' => $transactionId,
            'type' => 'redirect',
            'currency' => $amountCurrency,
            'amount' => number_format($amount, 2, '.', ''),
            'description' => $description,
            'notifyUrl' => $urlOrigin.'multisafepay/notification/'.rawurlencode($transactionId),
            'returnUrl' => $urlOrigin.'multisafepay/redirection/'.rawurlencode($transactionId).'/completed',
            'cancelUrl' => $urlOrigin.'multisafepay/redirection/'.rawurlencode($transactionId).'/canceled',
            'close_window' => 'false',
        ];

        $request = $this->gateway()
            ->purchase($parameters);

        $response = $request->send();

        if (! $response->isRedirect()) {
            return [
                'success' => false,
                'message' => 'Redirect url was not provided by MultisafePay.'."\r\n".print_r($response->getData(), true),
            ];
        }

        return [
            'success' => true,
            'message' => '',
            'redirect' => ['method' => $response->getRedirectMethod(),
                                'url' => $response->getRedirectUrl(),
                                'content-type' => '',
                                'data' => $response->getRedirectData(),
                               ],
            'notifyUrl' => $parameters['notifyUrl'],
        ];
    }

Kind Regards, Mirco Babin

MircoBabin commented 3 years ago

I had to think the other way around, providing a HttpClient with the correct settings.

This solution works:

use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Omnipay\Common\Http\Client as OmnipayHttpClient;

    protected function gateway()
    {
        $config = [
            'proxy' => 'localhost:8888',
            'verify' => false, //UNSAFE: ignore SSL certificate errors
        ];
        $adapter = GuzzleAdapter::createWithConfig($config);
        $client = new OmnipayHttpClient($adapter);

        $gateway = Omnipay::create('MultiSafepay_Rest', $client);

        $gateway->initialize([
             'apiKey' => $this->apikey,
             'locale' => $this->locale,
             'testMode' => $this->testaccount,
         ]);

        return $gateway;
    }