2checkout未授权错误Laravel

时间:2018-08-23 12:17:53

标签: javascript php jquery laravel 2checkout

我正在尝试使用Sandbox 2checkout API,但遇到了一些问题。 我正在使用Laravel作为php框架和2co.min.js来生成令牌。 当我填写输入内容并按提交时,我得到了“未授权”。

我从我的沙箱帐户和公共私钥中获得了卖方ID。 API,并检查了它们,但仍然出现此错误:

卖家编号,公钥和私钥

The seller id , public and private keys

HTML代码

    <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>

JS代码

 <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;
    });
});

我需要帮助的人

1 个答案:

答案 0 :(得分:0)

您需要在脚本中更改senderId和publishableKey。

将tokenRequest函数更改为此:

var tokenRequest = function () {
    // Setup token request arguments
    var args = {
        sellerId: "901389630",
        publishableKey: "A4B8A470-61A2-470E-9DCB-013A033FD206",
        ccNo: $("#ccNo").val(),
        cvv: $("#cvv").val(),
        expMonth: $("#expMonth").val(),
        expYear: $("#expYear").val(),
        billingAddr: $("#billingAddr").val()
    };

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

就像@LahiruTM一样,将TCO.loadPubKey('production');更改为TCO.loadPubKey('sandbox');