Mangopay / mangopay2-php-sdk

PHP SDK for MANGOPAY
https://packagist.org/packages/mangopay/php-sdk-v2
MIT License
122 stars 133 forks source link

How to create a new card ? #506

Closed Geoffray-buhler closed 3 years ago

Geoffray-buhler commented 3 years ago

Hi so i want to create a card for make a payin but i don't understande how i supposed to do

in first time i create a Cardregistration with this API like that

Controller : if ($form->isSubmitted() && $form->isValid()) { $montant = $form->getData(); $montant = $montant['Montant']; $formdata = $form->getData(); $card = new CardRegistration; $card->UserId = $this->getUser()->getMangopayId(); $card->CardType = $formdata['Cardtype']; $card->Currency = $formdata['currency']; $result = $mangoservices->CreateCard($card); $PreregistrationData = $result->PreregistrationData; $CardRegistrationURL = $result->CardRegistrationURL; $CardNumber = $formdata['Number']; $CardCVV = $formdata['CVV']; $CardDate = '';

Services mangopay : public function CreateCard($card) { $cardInfo = $this->mangoPayApi->CardRegistrations->Create($card); return $cardInfo; }

after i create a form to send my data to tokanisation's server like this

Twig : `

    <input type="hidden" name="accessKeyRef" value="{{infos['AccessKey']}}">
    <input type="hidden" name="cardNumber" value="{{infos['CardNumber']}}">
    <input type="hidden" name="cardExpirationDate" value="{{infos['CardDate']}}">
    <input type="hidden" name="cardCvx" value="{{infos['CardCVV']}}">
    <input type="hidden" name="returnURL" value="{{ url('app_paymentres') }}">
    <input type="submit" name="submit" value="Accepter">
</form>`

and i get it to update my card

Controller : $Token = $request->query->get('data'); $data = $mangoservices->GetCard($card->getCardId()); $data->RegistrationData = $Token; $data->ResultMessage = ''; $data->Status = 'CREATED'; $result = $mangoservices->UpdateCard($data);

Services mangopay : public function GetCard($card_id) { $cardInfo = $this->mangoPayApi->CardRegistrations->Get($card_id); return $cardInfo; } `` public function UpdateCard($card) { $cardInfo = $this->mangoPayApi->CardRegistrations->Update($card); return $cardInfo; }

i know, something is wrong, but i really don't know how to do it

thank you

fredericdelordm commented 3 years ago

Hello @Geoffray-buhler,

I would suggest to use the following tests as an example :

H4wKs commented 3 years ago

Hi @Geoffray-buhler ,

You have a working example in the demos directory. Checkout out: https://github.com/Mangopay/mangopay2-php-sdk/blob/master/demos/paymentDirect/non_js.php https://github.com/Mangopay/mangopay2-php-sdk/blob/master/demos/paymentDirect/payment.php Cheers,

Geoffray-buhler commented 3 years ago

Thank you so much. That work now but i have a new error and i don't understand why but my card is register in mangopay Bdd ^^ 👍 👍 👍 image But in my App image

fredericdelordm commented 3 years ago

@Geoffray-buhler the error does not say much. What are you trying to do? Could you share your code?

Geoffray-buhler commented 3 years ago

So i just follow this docs

https://github.com/Mangopay/mangopay2-php-sdk/blob/master/demos/paymentDirect/non_js.php https://github.com/Mangopay/mangopay2-php-sdk/blob/master/demos/paymentDirect/payment.php

After the creation of card i get it and

Controller : ` $user = $this->getUser();

    $cardRegister = $mangoservices->GetCardRegistrations($_SESSION['cardRegisterId']);
    $cardRegister->RegistrationData = isset($_GET['data']) ? 'data=' . $_GET['data'] : 'errorCode=' . $_GET['errorCode'];

    $updatedCardRegister = $mangoservices->UpdateCard($cardRegister);

`

ServiceMangopay : public function UpdateCard($card) { $cardInfo = $this->mangoPayApi->CardRegistrations->Update($card); return $cardInfo; } i just want to update my card without error ^^

H4wKs commented 3 years ago

@Geoffray-buhler,

So it look like there may be an issue with the SDK parsing the error, but that's not the problem here anyway, something must be wrong with the data you submitted.

Can you provide a dump of $cardInfo just before you return it. Maybe you can add a try / catch when you calling CardRegistrations->Update and provide a dump of the exception itself.

Also, I wouldn't use $_GET and $_SESSION as you are using Symfony, I would rather go by $request->query->get('data') and use the session interface to store / get the session info

Geoffray-buhler commented 3 years ago

Honestly thank you for your time ! i use $_GET and $_SESSION it's just for debug and understand ^^ so this is the data submitted

image

i can't dump $cardInfo because they is a error before that .

H4wKs commented 3 years ago

You need to dump the exception to really understand what is wrong ServiceMangopay :


public function UpdateCard($card) { 
   try {
       $cardInfo = $this->mangoPayApi->CardRegistrations->Update($card); 
       } catch (Exception $e) {
          $cardInfo = null;
          dump($e);
       } 
       return $cardInfo;
   }
Geoffray-buhler commented 3 years ago

i replaced my code with yours but i have the same thing

image

result : image

i don't catch any Exeptions

H4wKs commented 3 years ago

I never had this issue with the SDK, but same problem as here https://github.com/Mangopay/mangopay2-php-sdk/issues/500 I would probably dump $error inside the SDK and / or comment the foreach just to understand both why your ApiCall end up on a 400 instead of 200 code, and at the same time give feedback on what is wrong in the handling of the error at the SDK level.

Geoffray-buhler commented 3 years ago

Ok thank you i will try to get $error and dump it. So i'm beginner ^^

Geoffray-buhler commented 3 years ago

Ho i dump $error

Result: image

So my card is already processed that is good so i must fix this possibility ^^

Thank you for your help and your time @H4wKs and @fredericdelordm

Best regards

H4wKs commented 3 years ago

You're welcome @Geoffray-buhler

What version of PHP are you running ?

Geoffray-buhler commented 3 years ago

I use PHP 7.4.2 actually.

H4wKs commented 3 years ago

I submitted a PR #508 to fix the warning issued in RestTools when the $error->Errors is null like in your case, that should help in the future :)

Geoffray-buhler commented 3 years ago

I saw your patch, i apply it but i have new error.

image

Geoffray-buhler commented 3 years ago

No it's ok i don't know why but it's ok ^^ image