javascript - 2checkout Payment API 没有给我成功回调

标签 javascript php 2checkout

您好,我正在尝试创建一个使用 2checkout API 的付款方式 我按照 2co 在他们的文档中提供的说明进行操作,一切似乎都正常,但我从未收到订单已完成的确认消息,我在沙盒上创建了一个帐户,并使用了那里的信息,但仍然没有运气。

现在看到这是第一个包含表单和 2co.js 文件的代码

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Example Form</title>
  <script type="text/javascript" src="https://www.2checkout.com/checkout/api/2co.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<form id="myCCForm" action="test3.php" method="post">
  <input name="token" type="hidden" value="" />
  <div>
    <label>
      <span>Card Number</span>
      <input id="ccNo" type="text" value="" autocomplete="off" required />
    </label>
  </div>
  <div>
    <label>
      <span>Expiration Date (MM/YYYY)</span>
      <input id="expMonth" type="text" size="2" required />
    </label>
    <span> / </span>
    <input id="expYear" type="text" size="4" required />
  </div>
  <div>
    <label>
      <span>CVC</span>
      <input id="cvv" type="text" value="" autocomplete="off" required />
    </label>
  </div>
  <input type="submit" value="Submit Payment" />
</form>

<script>
    // Called when token created successfully.
    var successCallback = function(data) {
        var myForm = document.getElementById('myCCForm');

        // 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
        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: "901249656",
            publishableKey: "0A0C4A4D-FE71-41D0-A960-7C637F347785",
            ccNo: $("#ccNo").val(),
            cvv: $("#cvv").val(),
            expMonth: $("#expMonth").val(),
            expYear: $("#expYear").val()
        };

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

    $(function() {
        // Pull in the public encryption key for our environment
        TCO.loadPubKey('sandbox', function() {
            // Execute when Public Key is available
        });​

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

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

</script>
</body>
</html> 

我得到了我的sellerId: "901249656",和我的publishableKey: "0A0C4A4D-FE71-41D0-A960-7C637F347785",来 self 的沙盒演示帐户。

现在这是其他页面“test3.php

<?php
require_once("2checkout-php-master/lib/Twocheckout.php");
Twocheckout::privateKey('4D67BA12-CE09-4F1D-AB20-0133F24E3472');
Twocheckout::sellerId('901249656');
Twocheckout::sandbox(true);  #Uncomment to use Sandbox

try {
    $charge = Twocheckout_Charge::auth(array(
        "merchantOrderId" => "123",
        "token" => 'Y2U2OTdlZjMtOGQzMi00MDdkLWJjNGQtMGJhN2IyOTdlN2Ni',
        "currency" => 'USD',
        "total" => '10.00',
        "billingAddr" => array(
            "name" => 'Testing Tester',
            "addrLine1" => '123 Test St',
            "city" => 'Columbus',
            "state" => 'OH',
            "zipCode" => '43123',
            "country" => 'USA',
            "email" => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed99889e9984838a99889e99889faddf8e82c38e8280" rel="noreferrer noopener nofollow">[email protected]</a>',
            "phoneNumber" => '555-555-5555'
        ),
        "shippingAddr" => array(
            "name" => 'Testing Tester',
            "addrLine1" => '123 Test St',
            "city" => 'Columbus',
            "state" => 'OH',
            "zipCode" => '43123',
            "country" => 'USA',
            "email" => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb9f8e989f82858c9f8e989f8e99abd98884c5888486" rel="noreferrer noopener nofollow">[email protected]</a>',
            "phoneNumber" => '555-555-5555'
        )
    ), 'array');
    if ($charge['response']['responseCode'] == 'APPROVED') {
        echo "Thanks for your Order!";
    }
} catch (Twocheckout_Error $e) {
    $e->getMessage();
}

我从他们提供的链接下载了Twocheckout.php

现在的问题是 它假设如果页面上有任何错误,它会给我“未经授权”。 因此,如果没有错误并且一切正常,它应该给我“授权”。 发生的情况是,我转到“test3.php”并停在那里,没有任何错误或注释只是白色页面,当尝试刷新时,它会给出“重新发送”

请帮忙,如何做到这一点? 我的错误是什么? 我假设当我提交此信息时,我应该看到有人在我的演示页面“沙盒帐户”中下了一个新订单

最佳答案

请检查是否https://www.2checkout.com/checkout/api/2co.min.js是否已完全加载然后只需要调用

TCO.loadPubKey('sandbox', function() {
            // Execute when Public Key is available
});​

使用此代码

$.getScript('https://www.2checkout.com/checkout/api/2co.min.js', function() {
                    try {
                            // Pull in the public encryption key for our environment
                            TCO.loadPubKey('sandbox');
                        } catch(e) {
                            alert(e.toSource());
                        }
                });

关于javascript - 2checkout Payment API 没有给我成功回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23782448/

相关文章:

javascript - 无法设置 cuctom html5 验证消息

javascript - 2checkout未经授权的错误Laravel

javascript - 从 html() 变量中删除所有空格和制表符

javascript - 如何允许iframe访问Element.prototype

javascript - 启用脚本调试后无法在 Visual Studio 中运行 angularJS 应用程序 (1.6)

php - 我的服务中的渲染 View

php - 特色图片未从前端帖子上传

php - Paypal IPN 不断重试,但通知 url 甚至没有得到回调

php - 2结账退款问题