bunq / sdk_php

PHP SDK for bunq API
MIT License
84 stars 54 forks source link

Decoding error when trying to create a CVC2 code #167

Closed mbernson closed 4 years ago

mbernson commented 5 years ago

Steps to reproduce:

  1. Set up Tinker and run the following piece of code that generates a CVC code:
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use bunq\tinker\BunqLib;
use bunq\tinker\SharedLib;
use bunq\Model\Generated\Endpoint\Card;
use bunq\Model\Generated\Endpoint\CardGeneratedCvc2;

$allOption = getopt('', SharedLib::ALL_OPTION_KEY);
$environment = SharedLib::determineEnvironmentType($allOption);

$bunq = new BunqLib($environment);

const CARD_ID = 42; // Put an ID belonging to a MasterCard debit card here
$activeCard = Card::get(CARD_ID)->getValue();

echo vsprintf("Using %s\n", [$activeCard->getSecondLine()]);
$cvc1 = CardGeneratedCvc2::create($activeCard->getId())->getValue();

What should happen:

  1. According to doc.bunq.com it should return a single ID belonging to the newly generated CVC2 Code.

What happens:

  1. The SDK gets ~an array~ a response with a single CardGeneratedCvc2 object in it, tries to process it as an ID and fails.

Traceback

Fatal error: Uncaught Error: Call to a member function getId() on null in /Users/mathijsb/code/tinker/vendor/bunq/sdk_php/src/Model/Core/BunqModel.php:332 Stack trace: 0 /Users/mathijsb/code/tinker/vendor/bunq/sdk_php/src/Model/Generated/Endpoint/CardGeneratedCvc2.php(121): bunq\Model\Core\BunqModel::processForId(Object(bunq\Http\BunqResponseRaw)) 1 /Users/mathijsb/code/tinker/tinker/get-cvc.php(30): bunq\Model\Generated\Endpoint\CardGeneratedCvc2::create(42) 2 {main} thrown in /Users/mathijsb/code/tinker/vendor/bunq/sdk_php/src/Model/Core/BunqModel.php on line 332

SDK version and environment

OGKevin commented 5 years ago

@kojoru most likely invalid view definition.

basst85 commented 5 years ago

CardGeneratedCvc2::create() creates a new CVC2-code, but i think it returns this error because the response data is not as expected?

But you can get the generated CVC2 with CardGeneratedCvc2::listing()

basst85 commented 5 years ago

$responseRaw in the create function from CardGeneratedCvc2.php is an empty stream, so $apiClient->post(..) returns no response body.

OGKevin commented 5 years ago

@basst85 you mean https://github.com/bunq/sdk_php/blob/e9511e1c158a2c8d768d167bc05a66d7d88ea5d5/src/Model/Generated/Endpoint/CardGeneratedCvc2.php#L111-L118

? if so then this method must return https://github.com/bunq/sdk_php/blob/cc52274dc6400a38573f3bab17da3e5920c202fb/src/Model/Generated/Endpoint/BunqResponseNull.php as shown here https://github.com/bunq/sdk_php/blob/cc52274dc6400a38573f3bab17da3e5920c202fb/src/Model/Generated/Endpoint/Session.php#L39-L41

OGKevin commented 5 years ago

its a weird convention tho that a POST returns an empty body :(

mbernson commented 5 years ago

I made an error in the issue, the POST call just returns the newly created CardGeneratedCvc2 object, not an array. I've edited my SDK to fix the problem like so:

diff --git a/src/Model/Generated/Endpoint/CardGeneratedCvc2.php b/src/Model/Generated/Endpoint/CardGeneratedCvc2.php
index 180366b..04cb91c 100644
--- a/src/Model/Generated/Endpoint/CardGeneratedCvc2.php
+++ b/src/Model/Generated/Endpoint/CardGeneratedCvc2.php
@@ -102,9 +102,9 @@ class CardGeneratedCvc2 extends BunqModel
      *                          GENERATED.
      * @param string[] $customHeaders
      *
-     * @return BunqResponseInt
+     * @return CardGeneratedCvc2
      */
-    public static function create(int $cardId, string $type = null, array $customHeaders = []): BunqResponseInt
+    public static function create(int $cardId, string $type = null, array $customHeaders = []): BunqResponseCardGeneratedCvc2
     {
         $apiClient = new ApiClient(static::getApiContext());
         $apiClient->enableEncryption();
@@ -117,8 +117,8 @@ class CardGeneratedCvc2 extends BunqModel
             $customHeaders
         );

-        return BunqResponseInt::castFromBunqResponse(
-            static::processForId($responseRaw)
+        return BunqResponseCardGeneratedCvc2::castFromBunqResponse(
+            static::fromJson($responseRaw, self::OBJECT_TYPE_GET)
         );
     }
OGKevin commented 5 years ago

yup, so indeed @kojoru invalid view definition. Normal behavior for POST response is to return the id of the created item. In this case, the actual response is the created object. This needs to be explicitly defined so that the generator generates this accordingly :)

basst85 commented 5 years ago

So looks like #164 , maybe this can be fixed at the same time @kojoru ?