Saturday 22 August 2020

2checkout - Cannot generate token with sandbox mode

I am using 2checkout payment gateway in my PHP project and facing issue in generating token with 'sandbox' mode. Here's the code:

 <form action="" method="post" id="frmSignup" name="frmSignup">

<input type="hidden" value="" name="token" id="token" />
<script type="text/javascript" src="https://www.2checkout.com/checkout/api/2co.min.js"></script>
<script>
  // Called when token created successfully.
  var successCallback = function(data) {
    console.log("success::"+data);
    var myForm = document.getElementById('frmSignup');

    // Set the token as the value for the token input
    myForm.token.value = data.response.token.token;

    // IMPORTANT: Here we call `submit()` on the form element directly instead of using jQuery to prevent and infinite token request loop.
    myForm.submit();
  };

  // Called when token creation fails.
  var errorCallback = function(data) {
    // Retry the token request if ajax call fails
    console.log("error::"+data.errorMsg);
    if (data.errorCode === 200) {
       // This error code indicates that the ajax call failed. We recommend that you retry the token request.
    } else {
      alert(data.errorMsg);
    }
  };

  var tokenRequest = function() {
    // Setup token request arguments
    var args = {
      sellerId: "<?php echo accountnumber_checkout; ?>",
      publishableKey: "<?php echo publickey_checkout; ?>",
      ccNo: $("#creditcardno").val(), //input field
      cvv: $("#billingcvv").val(), //input field
      expMonth: $("#expiryMonth").val(), //input field
      expYear: $("#expiryYear").val() //input field
    };

    // Make the token request
    TCO.requestToken(successCallback, errorCallback, args);
  };

  $(function() {
    // Pull in the public encryption key for our environment
    TCO.loadPubKey('sandbox');

    $("#frmSignup").submit(function(e) {
      // Call our token request function
      tokenRequest();

      // Prevent form from submitting
      return false;
    });
  });

</script>

</form>

Facing this error: ( in 2co.min.js 2:1628 )

Uncaught ReferenceError: tokenRequestUrl is not defined

If I use 'production' in TCO.loadPubKey('production') instead of 'sandbox', then the token is generated. But, in production mode, I am not able to use test cards: Test Payment Methods



from 2checkout - Cannot generate token with sandbox mode

No comments:

Post a Comment