VivaPayments / API

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

After payment completed response url contains always failure #2440

Closed Bado-Obly closed 10 months ago

Bado-Obly commented 11 months ago

Hello, i'm encountering issues during the implementation of Vivawallet. After authenticating an access token and initiating the creation of an order, upon its completion, the redirect URL (set in the settings panel on the VW website) results in a "failure."

This does not occur when the same procedure is tested with Postman, (url contains "success").

I am available for any additional information needed, below are excerpts of the relevant code.

Client side functions: `document.getElementById("viva-btn").addEventListener("click", () => {

    let access_token = null;

    /* AUTH */
    $.ajax({
        type: "POST",
        url: "../gestione/fn/vivaPagamento.fn.php",
        data: { function_name: "autenticate"},
        dataType: 'json',
        'async': false,
        success: function (response) {
            console.log("CREATING ACCESS TOKEN");
            access_token = JSON.parse(response).access_token;
            console.log(access_token);
        },
        error: function (error) {
            console.log("error");
            console.log(error);
        }
    });

    /* CREATING ORDER */
    $.ajax({
        type: "POST",
        url: "../gestione/fn/vivaPagamento.fn.php",
        data: {
            function_name: "createVivaOrder",
            access_token: access_token,
            email: $("#txtEmail").val(),
            fullname: $("#txtNome").val() + " " + $("#txtCognome").val(),
            phone: $("#txtTelefono").val(),
            amount: parseFloat($("#idImporto").val()) + parseFloat($("#commissioni").val()),
        },
        dataType: 'json',
        async: true,
        success: function (response) {
            const orderCode = JSON.parse(response).orderCode;
            console.log("CREATING VIVA ORDER");
            console.log(JSON.parse(response).orderCode);

            /* return; */
            // Redirect to SmartCheckout
            /* https://www.vivapayments.com/web/checkout?ref=${orderCode} */ // Production
            /* https://demo.vivapayments.com/web/checkout?ref=${orderCode} */ // Demo
            const redirectUrl = `https://www.vivapayments.com/web/checkout?ref=${orderCode}`;
            window.location.replace(redirectUrl);

            // let risposta = sendData();
            // console.log(risposta);
        },
        error: function (xhr, status, error) {
            console.log("Error: " + status);
            console.log(error);
        }
    });
});`

Functions Server side: `if (isset($_POST['function_name']) && $_POST['function_name'] == 'autenticate') { header('Content-Type: application/json'); echo json_encode(autenticate()); }

if (isset($_POST['function_name']) && $_POST['function_name'] == 'createVivaOrder') { header('Content-Type: application/json'); echo json_encode(createVivaOrder()); }

// VivaWallet [Autenticate] function autenticate() {
$url = 'xxxxx'; // Production $clientId = 'xxxxx'; $clientSecret = 'xxxxx';

$data = [
    'grant_type' => 'client_credentials'
];

$headers = [
    'Content-Type: application/x-www-form-urlencoded',
    'Authorization: Basic ' . base64_encode($clientId . ':' . $clientSecret)
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    return 'Error: ' . curl_error($ch);
}
curl_close($ch);

return $response;

}

// VivaWallet [CreateOrder] function createVivaOrder() { $array = array( CURLOPT_URL => 'https://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 => '{ "amount": 100, "customerTrns": "string", "customer": { "email": "' . $_POST['email'] . '", "fullName": "' . $_POST['fullname'] . '", "phone": "' . $_POST['phone'] . '", "countryCode": "", "requestLang": "" }, "paymentTimeout": 999999, "preauth": true, "allowRecurring": false, "maxInstallments": 0, "paymentNotification": true, "tipAmount": 1, "disableExactAmount": false, "disableCash": true, "disableWallet": true, "sourceCode": "6076", "merchantTrns": "string", "tags": ["string"], }', CURLOPT_HTTPHEADER => array( 'Authorization: Bearer ' . $_POST['access_token'], 'Content-Type: application/json' ), );

$curl = curl_init();
curl_setopt_array($curl, $array);
$response = curl_exec($curl);
curl_close($curl);

return $response;

}`

VivaWalletCustomerSupport commented 11 months ago

Hello @Bado-Obly,

Thank you for your message. Kindly note that when creating a payment order for a pre-authorization (with preauth = true), tipAmount should only be set when capturing the pre-authorization, and not when creating the initial payment order.

Kind regards, Viva Wallet E-commerce Support Team

Bado-Obly commented 11 months ago

Thank you! I will try asap and let you know the results!

lucabosso commented 11 months ago

Hello, same issue here, tried your fix but not working, can you help us?

Bado-Obly commented 11 months ago

Hello! After trying your adjustments, it still seems that the online version works while the sandbox version doesn't. Please let me know what specific information you may require to help or if there are any further steps i can try.

Thank you Bado

VivaWalletCustomerSupport commented 10 months ago

Hello @Bado-Obly, @lucabosso,

Thank you for your message. It is working on your Live account but not on your Demo account? Can you please clarify that? Also, can you please send us the code you used?

Kind regards, Viva Wallet E-commerce Support Team

VivaWalletCustomerSupport commented 10 months ago

Hello @Bado-Obly,

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

Bado-Obly commented 10 months ago

Hello! Thank you for your message and the assistance you have provided. After opening the ticket, I spent some time examining the issue and found that it no longer occurred after recreating the code from scratch. At the moment, the problem seems to be resolved.

I apologize for not responding to your previous messages; I wanted to ensure that the issue was indeed resolved before providing a response. I greatly appreciate your support and promptness in responding.

I still don't know why with live keys worked and with demos not.

Thank you again