2

Hi i am trying to integrate adyen payment gateway in my project. I have used following PHP SDK Adyen PHP SDK

i have followed the steps mentioned in the sdk to generate client side encryption and passed those encrypted value to server side to perform payment. But i was stuck with a error message "Unable to Decrypt data"

Following is my sample code,

Frontend :

<script type="text/javascript" src="https://test.adyen.com/hpp/cse/js/MY_LIBRARY_TOKEN.shtml"></script>
  <form method="POST"  id="adyen-encrypted-form">
    <input placeholder="number" type="text" size="20" data-encrypted-name="number" value="2223520443560010" />
    <input placeholder="holderName" type="text" size="20" data-encrypted-name="holderName" value="Ashok" />
    <input placeholder="expiryMonth" type="text" size="2" data-encrypted-name="expiryMonth" value="10" />
    <input placeholder="expiryYear" type="text" size="4" data-encrypted-name="expiryYear" value="2020" />
    <input placeholder="cvc" type="text" size="4" data-encrypted-name="cvc" value="737" />
    <input type="hidden" value="<?php echo date('Y-m-d\TH:i:sO'); ?>" data-encrypted-name="generationtime"/>
    <input type="submit" value="Pay"/>
 </form>

<script>
    // The form element to encrypt.
    var form = document.getElementById('adyen-encrypted-form');

    var options = {};
    // Bind encryption options to the form.
    var encryptedBlobFieldName = "myFieldName";
    options.name = encryptedBlobFieldName;
    options.onsubmit = function(e) {
        var encryptedData = form.elements[encryptedBlobFieldName].value;
        // Encrypted form detials
        console.log(encryptedData);
        e.preventDefault();
    };
var result = adyen.createEncryptedForm(form, options);
</script>

I get the encrypted value from above submit action with the test deails as i have populated in the form. (i.e) encryptedData

Server Code :

$client = new \Adyen\Client();
$client->setApplicationName("Adyen PHP Api Library Example");
$client->setUsername("WS_USERNAME");
$client->setPassword("WS_USER_PASSWORD");
$client->setEnvironment(\Adyen\Environment::TEST);
$service = new \Adyen\Service\Payment($client);
$result = $service->authorise($params);

Following is the values i pass to authorise method,

Array(
[amount] => Array
    (
        [value] => 19
        [currency] => GBP
    )

[reference] => payment-test
[merchantAccount] => MERCHANT_ACCOUNT_CODE
[additionalData] => Array
    (
        [card.encrypted.json] => 'ENCRYPTED_DATA_FROM_FRONT_END'
    )
)

Am not sure what am i doing wrong, from the server side i receive a error as unable to decrypt the data. Please assist me to resolve this issue

Ashok
  • 437
  • 4
  • 9
  • The issue is likely to lie in the JavaScript code. You are trying to encrypt `myFieldName`, which doesn't exist... – Niraj Shah Dec 12 '17 at 09:03
  • @NirajShah i have referred adyen documentation from this [link](https://docs.adyen.com/developers/ecommerce-integration?ecommerce=ecommerce-integration). Adyne will create a field dynamically with the given encryptedBlobFieldName and return encrypted data in that field, so there is nothing with wrong that name. I also tried without adding that name and the same issue appear. – Ashok Dec 12 '17 at 11:55
  • Does `console.log(encryptedData);` output the encrypted data? – Niraj Shah Dec 12 '17 at 14:41
  • Yes it does, i get the encrypted data. But while doing payment in server side i get the issue as unable to decrypt. Am not sure what am i doing wrong – Ashok Dec 12 '17 at 14:53

3 Answers3

1

Are you trying to make a payment without a page refresh? In that case you should indeed create the field to encrypt yourself, in your case you called it 'myFieldName'. If you add that to your form you should be good to go. Let me know when that doesn't work.

Tim
  • 46
  • 4
  • Yes am trying to create encryption data without page refresh. I have tried by adding the field `myFieldName` in my form as like this `` when i add this field i don't get any encrypted data. I have added a submit event manually and checked the value in the `myFieldName` it returned empty in my case. Also in the adyne documentation there isn't anything mentioned like, need to add the field manually so only i haven't add the new field into my form. – Ashok Dec 13 '17 at 05:56
1

You need to be using a offset with a colon seperating hour and minute.

Change your date generation from

echo date('Y-m-d\TH:i:sO');

to

echo date('Y-m-d\TH:i:sP');
luke_b
  • 667
  • 6
  • 14
  • i have update my date format as you have mentioned, but still the same error. Do i need to update anything else in my server side code ? – Ashok Dec 13 '17 at 05:29
1

I found the problem. The problem is after generating the token from frontend via CSE i have passed those values to server side via GET api. So there is some additional characters added in the encrypted string. (the data passed in url was encrypted using url encryption) .

I have changed the server API method to post and it worked perfectly.

Ashok
  • 437
  • 4
  • 9