I'm trying to use Sandbox 2checkout API but I'm facing some issues. I am using Laravel as php framework and 2co.min.js to generate the token. When I fill the inputs and press submit I got 'unauthorized'.
I get the seller id from my sandbox account and the public, private key for the API and checked them but I still get this error:
The seller id , public and private keys
The HTML CODE
<form id="myCCForm" class="col-md-6" action="{{ route('pay') }}" method="post">
@csrf
<input id="token1" name="token1" type="hidden" value="">
<div>
<label>
<span>Card Number</span>
</label>
<input id="ccNo" type="text" size="20" value="" autocomplete="off" required/>
</div>
<div>
<label>
<span>Expiration Date (MM/YYYY)</span>
</label>
<input type="text" size="2" id="expMonth" required/>
<span> / </span>
<input type="text" size="2" id="expYear" required/>
</div>
<div>
<label>
<span>CVC</span>
</label>
<input id="cvv" size="4" type="text" value="" autocomplete="off" required/>
<input type="text" id="billingAddr" name="billingAddr" placeholder="Billing Adr ">
</div>
<input type="submit" value="Submit Payment">
</form>
The JS Code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://www.2checkout.com/checkout/api/2co.min.js"></script>
<script>
// Pull in the public encryption key for our environment
// Called when token created successfully.
var successCallback = function (data) {
var myTokenInput = document.getElementById('token1');
// Set the token as the value for the token input
myTokenInput.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) {
if (data.errorCode === 200) {
console.log(data);
} else {
console.log(data);
}
};
var tokenRequest = function () {
// Setup token request arguments
var args = {
sellerId: "203840804",
publishableKey: "ACC02BE7-70FC-4AEF-9F75-D592E299DEDA",
ccNo: $("#ccNo").val(),
cvv: $("#cvv").val(),
expMonth: $("#expMonth").val(),
expYear: $("#expYear").val(),
billingAddr: $("#billingAddr").val()
};
// Make the token request
TCO.requestToken(successCallback, errorCallback, args);
};
$(function () {
// Pull in the public encryption key for our environment
TCO.loadPubKey('production');
$("#myCCForm").submit(function (e) {
e.preventDefault();
// Call our token request function
tokenRequest();
// Prevent form from submitting
return false;
});
});