sumup / sumup-ecom-php-sdk

SumUp eCom PHP SDK
Other
41 stars 18 forks source link

Insistent error when posting checkout request #10

Closed saberonline closed 5 years ago

saberonline commented 5 years ago

Uncaught GuzzleHttp\Exception\ClientException: Client error:POST https://api.sumup.com/v0.1/checkoutsresulted in a400 Bad Requestresponse: [{"message":"Validation error","error_code":"MISSING","param":"checkout_reference"},{"message":"Validation error","error (truncated...)

However, a checkout_reference has been set..

lyubomir-sumup commented 5 years ago

Can you provide example code because otherwise we cannot help much?

saberonline commented 5 years ago

Yep. Sorry. Well, first I throw a function that successfully gets the token:

public function get_auth_code(){

    try {
        $sumup = new \SumUp\SumUp([
            'grant_type' => 'client_credentials',
            'app_secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx',
            'app_id'     => 'XXXXXXXXXXXXXX',
            'scope'      => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly']
        ]);
        $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();
    }

}

Then I want the token to be used for generating a checkout, like this:

public function get_checkout(){

    $code_auth = $this->get_auth_code();
    $headers = [
        'Authorization' => 'Bearer ' . $code_auth,      
        'Accept'        => 'application/json',
    ];
    $query = [
            "checkout_reference" => $this->reference,
            "amount" => 1,
            "currency" => "EUR",
            "pay_to_email" => "mymail@mail.com",
            "description" => "Teste de Integração"
        ];
    $response = $this->client->request('POST', 'https://api.sumup.com/v0.1/checkouts', [ 'headers' => $headers ], [ 'query' => $query ]);

    $response->then(
        function (ResponseInterface $res) {
            echo $res->getStatusCode() . "\n";
        },
        function (RequestException $e) {
            echo $e->getMessage() . "\n";
            echo $e->getRequest()->getMethod();
        }
    );        

}

The method connects to the API, but get a response 401 or 400, as per "missing a checkout_reference".

saberonline commented 5 years ago

Here's the error message I got:

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://api.sumup.com/v0.1/checkouts resulted in a 400 Bad Request response: [{"message":"Validation error","error_code":"MISSING","param":"checkout_reference"},{"message":"Validation error","error (truncated...)

lyubomir-sumup commented 5 years ago

The parameter checkout_reference should actually be some ID from your database. For example in your application you might have a database table checkouts and the checkout_reference is just an id from a record. This is how you link the actual order with the payment information. So the accepted value is a string that have to be unique for your application.

Also you can simplify your code with something like this:

try {
    $checkoutService = $sumup->getCheckoutService();
    $checkoutResponse = $checkoutService->create($amount, $currency, $checkoutRef, $payToEmail, $description);
//  use the variable $checkoutResponse->getHttpResponseCode() or $checkoutResponse-> getBody()
} catch(\SumUp\Exceptions\SumUpSDKException $e) {
    echo 'SumUp SDK error: ' . $e->getMessage();
}

You can read more here: https://github.com/sumup/sumup-ecom-php-sdk/tree/master/docs

saberonline commented 5 years ago

That clarifies a lot - what I was trying to do was integrating that before setting up the Woocommerce plugin, but let me try to install and use the order ID instead, and then it shall be working properly. Cheers and thanks for your time!