VivaPayments / API

Index of Viva Wallet open source projects on GitHub.
139 stars 163 forks source link

Issues with Payment Order Authentication #2404

Closed soulgeo closed 1 year ago

soulgeo commented 1 year ago

Hi,

I am testing the creation of payment orders for my client's website on the demo environment but I haven't managed to get a bearer token for my API calls. When trying to generate a bearer token with OAuth 2.0, the responses I get look like: "error: invalid_client" or "error: unsupported_grant_type". I then attempted to make payment orders with Basic Auth using my demo account's Merchant ID and API Key but got no response back from the API. Could I get some assistance please?

Also, for how long are bearer tokens valid once generated?

Thanks in advance.

VivaWalletCustomerSupport commented 1 year ago

Hello @soulgeo,

Thank you for your message. Can you please send us the full code of the API request you are making? Thank you in advance.

Kind regards, Viva Wallet E-commerce Support Team

soulgeo commented 1 year ago

This is the code for the request for OAuth 2.0 bearer token:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://demo-accounts.vivapayments.com/connect/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Authorization: Basic [ Client_ID:Client_Secret Base64-encoded ]';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

And this is what the code for the payment order with basic auth looks like:

$curl = curl_init();
$postData = [
    "amount" => 1,
    "customerTrns" => "string",
    "customer" => [
        "email" => "string", 
        "fullName" => "string", 
        "requestLang" => "en-GB"
    ],
    "sourceCode" => XXXX,
    "paymentNotification" => true
];

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://demo-api.vivapayments.com/checkout/v2/orders',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode($postData),
    CURLOPT_HTTPHEADER => array(
        'Authorization: Basic [ Merchant_ID:API_Key Base64-endoded ]',
        'Content-Type: application/json'
    ),
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
    echo 'Error:' . curl_error($curl);
}
curl_close($curl);

echo $response;
VivaWalletCustomerSupport commented 1 year ago

Hello @soulgeo,

Thank you for your reply. Kindly note that in the body of the request of the Oauth 2.0 authentication the parameter grant_type=client_credentials" should be used. That request will generate an Access token like the following example:

https://developer.vivawallet.com/integration-reference/oauth2-authentication/#step-3-receive-access-token

This access token should be used in the Authorization header of the create payment order API request. An example code of our create payment order API request can be found here.

Kind regards, Viva Wallet E-commerce Support Team

soulgeo commented 1 year ago

Hi,

Thank you for your response. Unfortunately after including the parameter in the OAuth 2.0 request:

$payload = ['grant_type = client_credentials'];
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));

I still get back {"error":"invalid_client"} as a response. I also tried making the request without php in the cmd as such:

 curl -L -X POST https://demo-accounts.vivapayments.com/connect/token ^
 -H Content-Type:application/x-www-form-urlencoded ^
 -H Authorization:Basic  [ Client_ID:Client_Secret Base64-encoded ] ^
 --data-urlencode "grant_type=client_credentials"

The response is:

{"error":"invalid_client"}curl: (3) URL using bad/illegal format or missing URL
VivaWalletCustomerSupport commented 1 year ago

Hello @soulgeo,

Thank you for your message. Please check your code and make sure you used the correct quotation marks. It should look like this:

curl -L -X POST https://demo-accounts.vivapayments.com/connect/token -H 'Content-Type:application/x-www-form-urlencoded' -H 'Authorization:Basic [ Client_ID:Client_Secret Base64-encoded ]' --data-urlencode 'grant_type=client_credentials'

Kind regards, Viva Wallet E-commerce Support Team

soulgeo commented 1 year ago

Hi,

I corrected the syntax in the way you provided but the response I get back is the same. Do you have any other suggestions I could try please?

Kind regards

VivaWalletCustomerSupport commented 1 year ago

Hello @soulgeo,

Thank you for your message. Can you please clarify which credentials were used?

Kind regards, Viva Wallet E-commerce Support Team

soulgeo commented 1 year ago

Hi,

I used my demo account's Client (Smart Checkout) credentials. Now I also tried to use a live account's credentials, the result is the same.

Kind regards

VivaWalletCustomerSupport commented 1 year ago

Hello @soulgeo,

Thank you for your prompt response. Kindly note that this error appears when incorrect credentials were used or there is syntax error in the code. We would recommend checking your implementation once again.

Kind regards, Viva Wallet E-commerce Support Team

VivaWalletCustomerSupport commented 1 year ago

Hello @soulgeo,

We just wanted to follow up on the reply we sent yesterday. We were simply wondering if you read our response.

In any case this ticket will remain active for at least 24 hours, so that you can come back if you wish.

However, if you have any questions, please don't hesitate to reply here and we'd be happy to help!

Kind regards, Viva Wallet E-commerce Support Team

VivaWalletCustomerSupport commented 1 year ago

Hello @soulgeo,

This ticket will close for now. Please don't hesitate to raise a new ticket if you need further assistance.

Kind regards, Viva Wallet E-commerce Support Team