sumup / sumup-ecom-php-sdk

SumUp eCom PHP SDK
Other
42 stars 19 forks source link

error creating a checkout in a php page #15

Closed noffy83 closed 4 years ago

noffy83 commented 4 years ago

hello every one im trying to create a simple php page that includes your card widget, so from my website i redirect the users to this page to make the payment online.

i started to get the authentication code and then the checkout id code but i keep getting this error:

Notice: Undefined property: stdClass::$access_token in E:\Inetpub\wwwroot\secure.lelepetshop.it\test\vendor\sumup\sumup-ecom-php-sdk\src\SumUp\Services\Authorization.php on line 119

Notice: Undefined property: stdClass::$token_type in E:\Inetpub\wwwroot\secure.lelepetshop.it\test\vendor\sumup\sumup-ecom-php-sdk\src\SumUp\Services\Authorization.php on line 119

Notice: Undefined property: stdClass::$expires_in in E:\Inetpub\wwwroot\secure.lelepetshop.it\test\vendor\sumup\sumup-ecom-php-sdk\src\SumUp\Services\Authorization.php on line 119

Notice: Undefined property: stdClass::$refresh_token in E:\Inetpub\wwwroot\secure.lelepetshop.it\test\vendor\sumup\sumup-ecom-php-sdk\src\SumUp\Services\Authorization.php on line 119 Authentication error: access token required

here's the code i use as a test:

`require_once("vendor/autoload.php");

function get_auth_code(){
try { $sumup = new \SumUp\SumUp([ 'grant_type' => 'client_credentials', 'app_id' => 'xxxxxxxxxxxxxxxxxxxxxx', 'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'scope' => ['payments'] ]); $accessToken = $sumup->getAccessToken(); $value = $accessToken->getValue(); return $value; // pass the $chekoutId to the front-end to be processed } catch (\SumUp\Exceptions\SumUpAuthenticationException $e) { echo 'Authentication error: ' . $e->getMessage(); } catch (\SumUp\Exceptions\SumUpResponseException $e) { echo 'Response error: ' . $e->getMessage(); } catch(\SumUp\Exceptions\SumUpSDKException $e) { echo 'SumUp SDK error: ' . $e->getMessage(); } }

try { $code_auth = get_auth_code(); $sumup = new \SumUp\SumUp([ 'app_id' => 'xxxxxxxxxxxxxxxxxx', 'app_secret' => 'xxxxxxxxxxxxxxxxxx', 'code' => $code_auth ]);

$amount = "1";
$currency = "EUR";
$checkoutRef ="1";
$payToEmail="fornitori@webmitalia.net";

$checkoutService = $sumup->getCheckoutService();
$checkoutResponse = $checkoutService->create($amount, $currency, $checkoutRef, $payToEmail);
$checkoutId = $checkoutResponse->getBody()->id;

// pass the $chekoutId to the front-end to be processed } catch (\SumUp\Exceptions\SumUpAuthenticationException $e) { echo 'Authentication error: ' . $e->getMessage(); } catch (\SumUp\Exceptions\SumUpResponseException $e) { echo 'Response error: ' . $e->getMessage(); } catch(\SumUp\Exceptions\SumUpSDKException $e) { echo 'SumUp SDK error: ' . $e->getMessage(); }`

lyubomir-sumup commented 4 years ago

Hi, @noffy83 Are you sure you have permissions for payment scope. As you can see here it is not active by default.

noffy83 commented 4 years ago

i'll send and email to the sumup integration team asking if they gav me permissions yet or not. hope this is all the priobleme. i'll update this issue as soon as i get a response. thx

noffy83 commented 4 years ago

Hi @lyubomir-sumup , sorry for taking too long, but the sumup team replyed to the email just now

so now i have the scope payment activated, but the result its still the same, same errors and wornings. any idea on what may cause it? any help whould be appreciated. thx very much

lyubomir-sumup commented 4 years ago

Hi @noffy83 I reviewed your example code and there is a bug. The function get_auth_code() return an access token. You can fix your code by replacing this: 'code' => $code_auth with 'access_token' => $code_auth and it should work The other way to fix your code involves a little refactoring so that you reuse your variable $sumup. You should initialize ($sumup = new \SumUp\SumUp) it only once and then you can call something like this $sumup->getCheckoutService()

noffy83 commented 4 years ago

thank you very much. now i get the code working and i get the chekoutid to use to implement the card widget. i'll move on to the next step, if i have any problem implementing this, i'll write it here :)

only one concirn. the checkout ref its a unique number, i thought it could be the order number from the shopping chart, but if the payments goes wrong somehow, i cant use the same code, it gives me error. its supposed to do so, or im doing something wrong?

lyubomir-sumup commented 4 years ago

The checkout_ref is unique and cannot be used for creating second checkout. If there were some problems processing this checkout you can try to process it again. But if you need to update the checkout (e.g. amount changes) then you should create a new checkout.

noffy83 commented 4 years ago

Hi @lyubomir-sumup , sorry for bothering you again, but i still have problems, hope you can help me. i followd the guide for mounting the credit card widget, and i successfully did it, but when I hit the pay button, nothing happens. i 'll post the complete code in my page here, do you have any idea if im missing something?

`<?php require_once("vendor/autoload.php");

function get_auth_code(){
try { $sumup = new \SumUp\SumUp([ 'grant_type' => 'client_credentials', 'app_id' => 'xxxxxxx', 'app_secret' => 'xxxxxxxxxxxx', 'scope' => ['payments'] ]); $accessToken = $sumup->getAccessToken(); $value = $accessToken->getValue(); return $value; // pass the $chekoutId to the front-end to be processed } catch (\SumUp\Exceptions\SumUpAuthenticationException $e) { echo 'Authentication error: ' . $e->getMessage(); } catch (\SumUp\Exceptions\SumUpResponseException $e) { echo 'Response error: ' . $e->getMessage(); } catch(\SumUp\Exceptions\SumUpSDKException $e) { echo 'SumUp SDK error: ' . $e->getMessage(); } }

try {

$code_auth = get_auth_code();

$sumup = new \SumUp\SumUp([
    'app_id'     => 'xxxxxxx',
    'app_secret' => 'xxxxxxxxxxx',
    'access_token'       =>  $code_auth
]);

//$amount = $_POST['amount'];
$amount ="0.01";
$currency = "EUR";
$checkoutRef =$_POST["id"] . "_" . uniqid();
$payToEmail="xxxxxx";

$checkoutService = $sumup->getCheckoutService();
$checkoutResponse = $checkoutService->create($amount, $currency, $checkoutRef, $payToEmail);
$checkoutId = $checkoutResponse->getBody()->id;

// pass the $chekoutId to the front-end to be processed } catch (\SumUp\Exceptions\SumUpAuthenticationException $e) { echo 'Authentication error: ' . $e->getMessage(); } catch (\SumUp\Exceptions\SumUpResponseException $e) { echo 'Response error: ' . $e->getMessage(); } catch(\SumUp\Exceptions\SumUpSDKException $e) { echo 'SumUp SDK error: ' . $e->getMessage(); } ?>

`

<script type="text/javascript"> SumUpCard.mount({ checkoutId: '<?php echo $checkoutId; ?>', amount: '<?php echo $amount?>', currency: '<?php echo $currency?>', locale: 'it-IT', onResponse: function(type, body) { console.log('Type', type); console.log('Body', body); } }); </script>

lyubomir-sumup commented 4 years ago

Hi, @noffy83 you should look for output in your browser's console. You need to modify the onResponse callback function according to your flow.

noffy83 commented 4 years ago

Hi @lyubomir-sumup, can you pls give me some sort of example? cos i have no idea on how to write it..its the first time for me approching to this sort of thing..

the payment works, i've tryed with a credit card and the money are taken, but i dont know how to get the response and do something. like a redirect

i tryed this

` SumUpCard.mount({ checkoutId: '<?php = $checkoutId; ?>', amount: '<?php = $amount?>', currency: '<?php = $currency?>', locale: 'it-IT', onResponse: function(type, body) { console.log('Response from client', res); alert( JSON.stringify(data) );

    sumupCard.unmount(); 
    }
}); but it doesnt show anything

` can you give me pls a hint? im googoling for hours but cant find a solution

lyubomir-sumup commented 4 years ago

Keep in mind that the onResponse callback function is called more than once as described in the documentation. When you have successful response you have to send it to your server for verification before showing the success screen for the user. I can't help you further because I don't know your technology stack, use cases, flow and so on.