pay-now / paynow-php-sdk

PHP Library for Paynow API
https://docs.paynow.pl
MIT License
15 stars 10 forks source link

Laravel 9 - empty array on getPaymentMethods #65

Closed m-pastuszek closed 2 years ago

m-pastuszek commented 2 years ago

Hello,

I'm integrating Paynow with Laravel 9 project / PHP 7.4.

public function availablePaymentMethods()
    {
        $client = new Client(config('paynow.api_key'), config('paynow.signature_key'), Environment::SANDBOX);
        try {
            $payment = new Payment($client);
            $paymentMethods = $payment->getPaymentMethods();
            $availablePaymentMethods = $paymentMethods->getAll();
            return response()->json($availablePaymentMethods);
        } catch (PaynowException $exception) {
            return response()->json('Error occurred', 400);
        }
    }

API returns 200, but it's content is only empty elements:

[
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {}
]

Http Client:

httpClient: Paynow\HttpClient\HttpClient {#499 ▼
    #client: Symfony\Component\HttpClient\Psr18Client {#503 ▼
      -client: Symfony\Component\HttpClient\CurlHttpClient {#506 ▶}
      -responseFactory: Nyholm\Psr7\Factory\Psr17Factory {#504}
      -streamFactory: Nyholm\Psr7\Factory\Psr17Factory {#505}
    }
emilleszczak2 commented 2 years ago

Have you tried to call the REST API from the postman or curl for your credentials?

m-pastuszek commented 2 years ago

@emilleszczak2, calling API directly from postman returns data correctly.

m-pastuszek commented 2 years ago

It was a problem on my side with array to JSON conversion. I've made it this way:

public function availablePaymentMethods(): \Illuminate\Http\JsonResponse
    {
        $client = new Client(config('paynow.api_key'), config('paynow.signature_key'), Environment::SANDBOX);
        try {
            $payment = new Payment($client);
            $paymentMethods = $payment->getPaymentMethods();
            $availablePaymentMethods = $paymentMethods->getOnlyCards();

            $responseTable = array();
            foreach ($availablePaymentMethods as $availablePaymentMethod) {
                $responseTable[] = [
                    'id'                    => $availablePaymentMethod->getId(),
                    'type'                  => $availablePaymentMethod->getType(),
                    'name'                  => $availablePaymentMethod->getName(),
                    'description'           => $availablePaymentMethod->getDescription(),
                    'image'                 => $availablePaymentMethod->getImage(),
                    'status'                => $availablePaymentMethod->getStatus(),
                    'authorizationType'     => $availablePaymentMethod->getAuthorizationType()
                ];
            }
            return response()->json($responseTable);
        } catch (PaynowException $exception) {
            return response()->json('Error occurred: '. $exception->getMessage(), 400);
        }
    }

We can close the issue.