Youcanpay API php issue

Viewed 50

Here is theSDK of youcanpay payment gateway https://github.com/NextmediaMa/youcan-payment-php-sdk

I have followed the documentation described in the above link. And the payment form is displaying fine but I need to generate a token to proceed with the payments. But in the token generating step php not able to find youcanpay class intances which is used as static method YouCanPay::setIsSandboxMode(true);. I simply want to be sure I didn't do anything wrong. May be there was an issue with my folder structure here it's screenshot https://i.imgur.com/uqdXgmr.png. Or I have to change php namcspace? use YouCan\Pay\YouCanPay;

Form display HTML


  <div id="error-container"></div>
    <div id="payment-card"></div>
    <button id="pay">Pay</button>

** php **
use YouCan\Pay\YouCanPay;

class ExamplePayment
{
    
    /**
     * Return a token to make payment for an order, this token is required to make payment with JS script.
     *
     * @return string
     */
    public function createToken()
    {

        // Enable sandbox mode, otherwise delete this line.
        YouCanPay::setIsSandboxMode(true);

        // Create a YouCan Pay instance, to retrieve your private and public keys login to your YouCan Pay account
        // and go to Settings and open API Keys.
          $youCanPay = YouCanPay::instance()->useKeys('pri_sandbox_56cfe571-f671-42a6-a231-d58ec', 'pub_sandbox_a62be70f-d585-4e88-9c5b-563f2');
        // $youCanPay = YouCanPay::instance()->useKeys('pri_sandbox_56cfe571-f671-42a6-a231-d58ec', 'pub_sandbox_a62be70f-d585-4e88-9c5b-563f2');

        // Data of the customer who wishes to make this purchase.
        // Please keep these keys.
    
        // Create the order you want to be paid
        $token = $youCanPay->token->create(
            // String orderId (required): Identifier of the order you want to be paid.
            "order-id",
            // Integer amount (required): The amount, Example: 25 USD is 2500.
            "2000",
            // String currency (required): Uppercase currency.
            "USD",
            // String customerIP (required): Customer Address IP.
            "175.107.236.174",
            // String successUrl (required): This URL is returned when the payment is successfully processed.
            "https://smmpanelauto.com/orders-status/success",
            // String errorUrl (required): This URL is returned when payment is invalid.
            "https://smmpanelauto.com/orders-status/error"
        );

        echo $token->getId();
    }
    
}
********** JS Code **********
   <script type="text/javascript">
        // Create a YouCan Pay instance.
        const ycPay = new YCPay('pub_sandbox_a62be70f-d585-4e88-9c5b-563f2', {
            formContainer: '#payment-card',
            locale: 'en',
            isSandbox: true,
            errorContainer: '#error-container',
            // token: 'token_x6gf0_....'
        });

        // render the form
        ycPay.renderCreditCardForm();
        
        // start the payment on button click
        var el = document.getElementById('pay');
        if(el){
            <?php   
                 $a = new ExamplePayment; 
                ?>
            el.addEventListener('click', function(){
                  // execute the payment
                  ycPay.pay("<?php $a->createToken(); ?>") //using token
                    .then(successCallback)
                    .catch(errorCallback);
                });
        }
        function successCallback(response) {
          console.log(rsponse);
        }
        
        function errorCallback(response) {
            console.log(rsponse);
        }
    </script>

When I use <?php $a->createToken(); ?> the form is not showing. otherwise. it's shows the following error There are invalid fields in your request, please verify your inputs and try again But all inputs are filled.

1 Answers

When you call the createToken() method it causes the PHP fatal error, and the server responds with 500 HTTP status or the blank page. To see the exact error you may check the logs, of for the dev environment just add these lines in the beginning of the PHP code:

error_reporting(E_ALL);
ini_set('display_errors', 1)

So that all the PHP errors are sent to the output and displayed ob the page.

Looking into the file structure I assume all the PHP/HTML/JS code is located in the single index.php file. So make sure the YouCanPay SDK's file are included, i.e. you should have the line like this in the beginning if the file (or anywhere before teh actual usage of the YouCanPay class):

require_once 'vendor/autoload.php';
Related