0

Trying to integrate 2checkout API but facing some problems

Here is form.html:

<!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="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<form id="myCCForm" action="ProcessPayment.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: "202861652",
            publishableKey: "02C0DF42-4FAC-49AA-8CD5-913C0EKKK00A",
            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
        $.getScript('https://www.2checkout.com/checkout/api/2co.min.js', function() {
                    try {
                            // Pull in the public encryption key for our environment
                            TCO.loadPubKey('02C0DF42-4FAC-49AA-8CD5-913C0EKKK00A');
                        } catch(e) {
                            alert(e.toSource());
                        }
                });

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

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

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

Here is PHP file:

<?php
require_once("2checkout-php-master/lib/Twocheckout.php");
Twocheckout::privateKey('2C461002-281D-43F6-B477-6C61CFD21D33');
Twocheckout::sellerId('201691652');
//Twocheckout::sandbox(true);  #Uncomment to use Sandbox

try {
    $charge = Twocheckout_Charge::auth(array(
        "merchantOrderId" => "123",
        "token" => $_POST['token'], 
        "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!";
    }
    else{
        echo "Sorry! failed";
    }
} catch (Twocheckout_Error $e) {
    $e->getMessage();
}

1) Its not displaying anything at the end of processing except blank page.

2) Is TCO.loadPubKey('02C0DF42-4FAC-49AA-8CD5-913C0EKKK00A'); means public key?

Please advise

Thanks in anticipation

LifeSaver
  • 138
  • 4
  • 10
  • blank page in most cases mean u have display errors off and some errors in your script enable error reporting and display errors to see your errors. can be everything namespaceing problems or filenot found or missing variables. maybe the exception is thrown so better var_dump the exception to ee the details – di3 May 02 '16 at 09:59
  • I written echo '
    ';
     print_r($data);
     echo '
    '; but same
    – LifeSaver May 02 '16 at 15:58
  • as said enable error repoting and display errors or you will have whitepages until the end of your live – di3 May 02 '16 at 17:30
  • I turned display_error=on in php.ini and now its showing this error: `Warning: require_once(/2/checkout-php-master/lib/Twocheckout.php): failed to open stream: No such file or directory in /var/www/vhosts/domain.com/httpdocs/2/ProcessPayment.php on line 2 Fatal error: require_once(): Failed opening required '/2/checkout-php-master/lib/Twocheckout.php' (include_path='.:/usr/share/pear:/usr/share/php') in ` – LifeSaver May 02 '16 at 19:53
  • yes then read the error.... it say the file you require is eiter not readable or doesnt exist – di3 May 02 '16 at 20:22
  • Thanks the problem fixed – LifeSaver May 03 '16 at 06:12

0 Answers0