# PHP

Collecting online payments can be very easy with PayKun. You can provide multiple payment options to your customer and a great checkout experience. Also, they will get all sorts of payment option that will make them buy on the spot.

### Prerequisite

Following things will be required before you can begin integration:

* Merchant ID
* Access Token & API Secret ([See how to generate](https://docs.paykun.com/technical-guide/api-encryption-key-guide))
* PHP: >=5.3.0'

### **Installation**

Installation can be done using the following composer code or the required zip file. After that include \*\*Payment.php\*\* in your application and you can use the API as usual.

### **Composer**

```php
composer require paykun/checkout
```

**ZIP File download**

1. You can download the zip file from below given link if you are not using the composer code [PayKun\_PHP\_zip file](https://github.com/paykun-code/paykun-php)
2. Unzip the given file
3. Include Payment.php, Validator.php & Crypto.php in your project.

{% hint style="danger" %}
If you are using any framework or you have autoload setup in your project then you don’t have to include this files manually
{% endhint %}

### **Usage (Composer project)**

```php
use Paykun\Checkout\Payment;
 
$obj = new Payment('<merchantId>', '<accessToken>', '<encryptionKey>');
 
// Initializing Order
$obj->initOrder('<orderId>', '<Purpose or ProductName>', "<amount>", '<successUrl.example.com>',  '<failUrl.example.com>');
 
// Add Customer
$obj->addCustomer('<customerName>', '<customerEmail>', '<customerContactNo>');
 
// Add Shipping address
$obj->addShippingAddress('<country>', '<state>', '<city>', '<postalCode>', '<fullAddress>');
 
// Add Billing Address
$obj->addBillingAddress('<country>', '<state>', '<city>', '<postalCode>', '<fullAddress>');
 
echo $obj->submit();
 
/* Check for transaction status
 * Once your success or failed url called then create an instance of Payment same as above and then call getTransactionInfo like below
 *  $obj = new Payment('merchantUId', 'accessToken', 'encryptionKey');
 *  $transactionData = $obj->getTransactionInfo(Get payment-id from the success or failed url);
 *  Process $transactionData as per your requirement
 *
 * */
```

### **Usage (Non-composer project)**

```php
require 'src/Payment.php';
require 'src/Validator.php';
require 'src/Crypto.php';
 
/**
 *  Parameters requires to initialize an object of Payment are as follow.
 *  mid => Merchant Id provided by Paykun
 *  accessToken => Access Token provided by Paykun
 *  encKey =>  Encryption provided by Paykun
 *  isLive => Set true for production environment and false for sandbox or testing mode
 *  isCustomTemplate => Set true for non composer projects, will disable twig template
 */
 
$obj = new \Paykun\Checkout\Payment('<merchantId>', '<accessToken>', '<encryptionKey>', true, true);
 
// Initializing Order
$obj->initOrder('<orderId>', '<Purpose or ProductName>', "<amount>", '<successUrl.example.com>',  '<failUrl.example.com>');
 
// Add Customer
$obj->addCustomer('<customerName>', '<customerEmail>', '<customerContactNo>');
 
// Add Shipping address
$obj->addShippingAddress('<country>', '<state>', '<city>', '<postalCode>', '<fullAddress>');
 
// Add Billing Address
$obj->addBillingAddress('<country>', '<state>', '<city>', '<postalCode>', '<fullAddress>');
 
//Render template and submit the form
echo $obj->submit();
 
/* Check for transaction status
 * Once your success or failed url called then create an instance of Payment same as above and then call getTransactionInfo like below
 *  $obj = new Payment('merchantUId', 'accessToken', 'encryptionKey', true, true); //Second last false if sandbox mode
 *  $transactionData = $obj->getTransactionInfo(Get payment-id from the success or failed url);
 *  Process $transactionData as per your requirement
 *
 * */
```

### **Test your Integration (sandbox)**

{% hint style="info" %}
If you need it for testing purposes you can use our sandbox mode by sending “isLive” parameter to false.
{% endhint %}

If you need it for testing purposes you should login in Paykun dashboard using sandbox mode and get your testing [merchant Id](https://docs.paykun.com/technical-guide/api-encryption-key-guide) and [Device API key(access token).](https://docs.paykun.com/technical-guide/api-encryption-key-guide)

To test your integration in sandbox environment before going like, you can use our test environment (See how to create one) and use your test credentials, You just have to use **isLive**=**false** in your integration, You can use test card details provided [here ](https://docs.paykun.com/technical-guide/test-card-information)for testing purpose.

{% hint style="info" %}
Also change application Id from build.gradle file to "com.paykunsandbox" for testing purposes and for production use applicationId "com.paykunsandbox.live".
{% endhint %}

* [Log In](https://app.paykun.com/login) to your PayKun Dashboard
* ‘Go to’ the Merchant Account under the ‘Payment Gateway’ section (If you are first-time-login then you will see the ‘Become a Merchant’ option.)
* After the merchant account login, click on your profile picture.
* Select ‘Test mode’.
* Select "Generate Sandbox Account" to activate your sandbox account
* After activating sandbox account,you can click on "Sandbox Login" button to go to sandbox dashboard and use generated username and password login.
* Now you have to generate your Device API key from sandbox account from Setting-Security tab.
* You can get you Merchant Id clicking on profile icon from top right corner.
* Now you have to use this API key and Merchant Id in your Demo project for testing purposes. Note: Remember that you can login in Sandbox account from your Live account.

### **For Live environment**

If you want to do real transaction than set "**isLive**" parameter to **true** and use your Live MerchantId and AccessToken from Paykun dashboard.

### **License**

The Paykun PHP SDK is released under the MIT License.

### **For any Assistance**

In case of any query, please contact to <tech@paykun.com>
