Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding error when trying to create a CVC2 code #167

Closed
1 of 2 tasks
mbernson opened this issue Nov 22, 2018 · 8 comments
Closed
1 of 2 tasks

Decoding error when trying to create a CVC2 code #167

mbernson opened this issue Nov 22, 2018 · 8 comments

Comments

@mbernson
Copy link
Contributor

mbernson commented Nov 22, 2018

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

  • Tested on 1.2.2
  • Sandbox
  • Production
@OGKevin
Copy link
Contributor

OGKevin commented Nov 22, 2018

@kojoru most likely invalid view definition.

@basst85
Copy link

basst85 commented Nov 22, 2018

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
Copy link

basst85 commented Nov 23, 2018

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

@OGKevin
Copy link
Contributor

OGKevin commented Nov 23, 2018

@basst85 you mean

$responseRaw = $apiClient->post(
vsprintf(
self::ENDPOINT_URL_CREATE,
[static::determineUserId(), $cardId]
),
[self::FIELD_TYPE => $type],
$customHeaders
);

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

return BunqResponseNull::castFromBunqResponse(
new BunqResponse(null, $responseRaw->getHeaders())
);

@OGKevin
Copy link
Contributor

OGKevin commented Nov 23, 2018

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

@mbernson
Copy link
Contributor Author

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
Copy link
Contributor

OGKevin commented Nov 23, 2018

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
Copy link

basst85 commented Nov 23, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants