2checkout Payment API don't give me Success Callback

我与影子孤独终老i 提交于 2021-02-08 04:43:20

问题


Hi I am trying to create a Payment which will use the 2checkout API I followed the instructions that 2co provided in they documents and everything seems working but I never get the confirmation message that my order is done, I made an account on sandbox and I used the information from there but still no luck.

now see this is the first code that has the form and the 2co.js file included

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

I got my sellerId: "901249656", and my publishableKey: "0A0C4A4D-FE71-41D0-A960-7C637F347785", from my sandbox demo account.

now this is other page "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" => 'testingtester@2co.com',
            "phoneNumber" => '555-555-5555'
        ),
        "shippingAddr" => array(
            "name" => 'Testing Tester',
            "addrLine1" => '123 Test St',
            "city" => 'Columbus',
            "state" => 'OH',
            "zipCode" => '43123',
            "country" => 'USA',
            "email" => 'testingtester@2co.com',
            "phoneNumber" => '555-555-5555'
        )
    ), 'array');
    if ($charge['response']['responseCode'] == 'APPROVED') {
        echo "Thanks for your Order!";
    }
} catch (Twocheckout_Error $e) {
    $e->getMessage();
}

I downloaded the Twocheckout.php from the provided link they given.

now the problem is it assume that if there is any error on the page it give me 'unauthorized'. So if there is no error and everything is OK it should give me 'authorized'. what happen is that I go to 'test3.php' and stops there with out any error or notes just white page and when try to refresh it gives 'resend'

any help please how this can be done? what is my mistaking? and I assume that when I submit this info I should see that someone has made a new order in my demo page "sandbox account"


回答1:


Please check whether https://www.2checkout.com/checkout/api/2co.min.js is loaded completely or not then only you need to call

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

Use this code

$.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());
                        }
                });



回答2:


In test3.php server side, you need to change the value for 'token' in the try block.

'token' => 'Y2U2OTdlZjMtOGQzMi00MDdkLWJjNGQtMGJhN2IyOTdlN2Ni', //wrong value.

Change it to

'token' => $_POST['token'], //correct value.

Hope that helps.




回答3:


I've learned (the hard way) that you need a "sandbox" account to run this example code successfully.




回答4:


screenshot

i faced same problem, at last i got solution... i was putting credential of my live account... whenever i put "sandbox" in code... so if you are using for sandbox then make sure your 2checkout account is sandbox... please check attached screenshot.



来源:https://stackoverflow.com/questions/23782448/2checkout-payment-api-dont-give-me-success-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!