PHP code example of becopay / invoice_sdk_php

1. Go to this page and download the library: Download becopay/invoice_sdk_php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

becopay / invoice_sdk_php example snippets


use Becopay\PaymentGateway;

try {
    $payment = new PaymentGateway(
        'api service url',
        'api key',
        'mobile'
    );
} catch (Exception $e) {
	//Add yours exception handling
    echo $e->getMessage();
}

try {
    /*
     * This function is used to create an invoice.
     * Return result data type is object
	 *
	 *	Note: default value of `merchantCur`and `currency` currency is 'IRR'  
     */ 
    $invoice = $payment->create('order id','price','description','currency','merchantCur');
    if($invoice)
    {
        /*
        * Save invoice id in your database 
        * For checking the invoice status you need invoice id
        * Then redirect user to gateway url for doing the payment process
        */
       
		//echo the result
  		echo json_encode($invoice);

        //Get invoice id and insert to database
        /*
        	$invoiceId = $invoice->id;
       		echo 'invoice id:'.$invoiceId.'<br>';
        */

        //Get gateway url
        /*
        	$redirectUrl = $invoice->gatewayUrl;
        	echo 'gateway url'.$invoiceId.'<br>';
        */
    }else{
    	//Add your error handling
    	echo $payment->error;
    }
} catch (Exception $e) {
	//Add your exception handling
    echo $e->getMessage();
}

check($invoiceId) // Set the Becopay InvoiceId

      /*
       * Use this function to check the invoice status.
       * This function gets invoice id (which has been created by `create()` function) as parameter
       * and returns status of that
       */
try {
      /*
       * This function will be used on your callback page or when you want to check the invoice status.
       * 
       * First you must get orderId from path or query string using $_GET
       * then find related becopay Invoice id from your database
       * and use it to check the invoice status
       */
    
      //Check invoice with InvoiceId
      $fetchedInvoice = $payment->check($invoiceId);
      
      if($fetchedInvoice)
      {
      	/*
         * Insert your code here for updating order status
         */
    	if($fetchedInvoice->status == "success")
        {
        	// success msg
        }else{
        	//error msg
        }
        
        
		//echo the result
      	echo json_encode($fetchedInvoice);
        
        
      }else{
        //Add your error handling
        echo $payment->error;
      } 
} catch (Exception $e) {
	//Add your exception handling
    echo $e->getMessage();
}

checkByOrderId($orderId) // Set the your orderId

      /*
       * Use this function to check the invoice status.
       * This function gets invoice id (which has been created by `create()` function) as parameter
       * and returns status of that
       */
try {
      /*
       * This function will be used on your callback page or when you want to check the invoice status.
       * 
       * You must get orderId from path or query string using $_GET
       * and use it to check the invoice status
       */
    
      //Check invoice with OrderId
      $fetchedInvoice = $payment->checkByOrderId($orderId);
      
      if($fetchedInvoice)
      {
      	/*
         * Insert your code here for updating order status
         */
    	if($fetchedInvoice->status == "success")
        {
        	// success msg
        }else{
        	//error msg
        }
        
        
		//echo the result
      	echo json_encode($fetchedInvoice);
        
        
      }else{
        //Add your error handling
        echo $payment->error;
      } 
} catch (Exception $e) {
	//Add your exception handling
    echo $e->getMessage();
}
json
    {
        "ay/invoice_sdk_php": "1.*"
        }
    }
composer