PHP code example of cubes-doo / nestpay

1. Go to this page and download the library: Download cubes-doo/nestpay 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/ */

    

cubes-doo / nestpay example snippets


use Cubes\Nestpay\MerchantService;

//...

$nestpayMerchantService = new MerchantService([
    'clientId' => '********',
    'storeKey' => '********',
    'storeType' => '3D_PAY_HOSTING',
    'okUrl' => 'http://localhost:8082/examples/success.php', //this could be configured later
    'failUrl' => 'http://localhost:8082/examples/failed.php', //this could be configured later
    '3DGateUrl' => 'https://testsecurepay.eway2pay.com/fim/est3Dgate',

    //API
    'apiName' => '********',
    'apiPassword' => '********',
    'apiEndpointUrl' => 'https://testsecurepay.eway2pay.com/fim/api'
]);


$nestpayMerchantService->setPDO($pdo); //$pdo is instanceof \PDO

$nestpayMerchantService->setPDO($pdo, 'your_table'); //'your_table' is name of the table for payments

$merchantService->onFailedPayment(function ($payment) {
    //$payment is instance of \Cubes\Nestpay\Payment

    //send an email for failed payment attempt
    // $email = $payment->getProperty(\Cubes\Nestpay\Payment::PROP_EMAIL);
    // $customerName = $payment->getProperty(\Cubes\Nestpay\Payment::PROP_BILLTONAME);

})->onSuccessfulPayment(function($payment) {
    //$payment is instance of \Cubes\Nestpay\Payment

    //send an email for successful payment
    // $email = $payment->getProperty(\Cubes\Nestpay\Payment::PROP_EMAIL);
    // $customerName = $payment->getProperty(\Cubes\Nestpay\Payment::PROP_BILLTONAME);

    //do stuff related to the siccessfull payment
});

 

$requestParameters = $nestpayMerchantService->paymentMakeRequestParameters([
    'amount' =>  123.45,
    'currency' => \Cubes\Nestpay\Payment::CURRENCY_RSD,
    'lang' => 'sr',
    //set transaction type to PreAuth or Auth
    \Cubes\Nestpay\Payment::PROP_TRANTYPE => \Cubes\Nestpay\Payment::TRAN_TYPE_PREAUTH,
    //this is email of the customer
    \Cubes\Nestpay\Payment::PROP_EMAIL => '[email protected]',
    
    //below are optional parameters
    \Cubes\Nestpay\Payment::PROP_INVOICENUMBER => '123456789', //must be numeric!!
    \Cubes\Nestpay\Payment::PROP_BILLTONAME => 'John Doe',\Cubes\Nestpay\Payment::PROP_BILLTOSTREET1 => 'BillToStreet1',
    \Cubes\Nestpay\Payment::PROP_BILLTOSTREET2 => 'BillToStreet2',
    \Cubes\Nestpay\Payment::PROP_BILLTOCITY => 'BillToCity',
    \Cubes\Nestpay\Payment::PROP_BILLTOSTATEPROV => 'BillToStateProv',
    \Cubes\Nestpay\Payment::PROP_BILLTOPOSTALCODE => 'BillToPostalCode',
    \Cubes\Nestpay\Payment::PROP_BILLTOCOUNTRY => 'RS',
    \Cubes\Nestpay\Payment::PROP_SHIPTOCOMPANY => 'ShipToCompany',
    \Cubes\Nestpay\Payment::PROP_SHIPTONAME => 'ShipToName',
    \Cubes\Nestpay\Payment::PROP_SHIPTOSTREET1 => 'ShipToStreet1',
    \Cubes\Nestpay\Payment::PROP_SHIPTOSTREET2 => 'ShipToStreet2',
    \Cubes\Nestpay\Payment::PROP_SHIPTOCITY => 'ShipToCity',
    \Cubes\Nestpay\Payment::PROP_SHIPTOSTATEPROV => 'ShipToStateProv',
    \Cubes\Nestpay\Payment::PROP_SHIPTOPOSTALCODE => 'ShipToPostalCode',
    \Cubes\Nestpay\Payment::PROP_SHIPTOCOUNTRY => 'RS',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA1 => 'DimCriteria1',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA2 => 'DimCriteria2',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA3 => 'DimCriteria3',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA4 => 'DimCriteria4',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA5 => 'DimCriteria5',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA6 => 'DimCriteria6',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA7 => 'DimCriteria7',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA8 => 'DimCriteria8',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA9 => 'DimCriteria9',
    \Cubes\Nestpay\Payment::PROP_DIMCRITERIA10 => 'DimCriteria10',
]);


$payment = $nestpayMerchantService->paymentProcess3DGateResponse($_POST);
//DO NOT SEND EMAIL HERE OR DO SOME ACTION ON SUCCESSFUL PAYMENT, JUST SHOW RESULT!
//USE $nestpayMerchantService->onSuccessfulPayment INSTEAD!!!
//display results of the payment:


//second parameter (true) indicates that this processing is on fail url
$payment = $nestpayMerchantService->paymentProcess3DGateResponse($_POST, true); 

//display resultsu of the payment:

//$oid is the OID of some unprocessed payment (WHERE `processed` != 1)
$payment = $nestpayMerchantService->paymentProcessOverNestpayApi($oid); 

//DO NOT SEND EMAIL HERE OR DO SOME ACTION ON SUCCESSFUL PAYMENT!
//USE $nestpayMerchantService->onSuccessfulPayment INSTEAD!!!


//$oid is the OID of the payment
$result = $nestpayMerchantService->postAuthorizationOverNestpayApi($oid); 

//$oid is the OID of the payment
//$amount should not be greated than the orginal amount reserved in PreAuth
$result = $nestpayMerchantService->postAuthorizationOverNestpayApi($oid, $amount); 

//$oid is the OID of the payment
$result = $nestpayMerchantService->voidOverNestpayApi($oid); 


//$payment is instance of \Cubes\Nestpay\Payment 
$payment = $nestpayMerchantService->getWorkingPayment();

//get some of the payment properties
$email = $payment->getProperty(\Cubes\Nestpay\Payment::PROPERTY_EMAIL);

//for some important properties there are getters
$email = $payment->getEmail();


use \Cubes\Nestpay\PaymentDao;
use \Cubes\Nestpay\Payment;

class MyPaymentDao implements PaymentDao
{
    /**
     * Fetch payment by $oid
     * 
     * @return \Cubes\Nestpay\Payment
     * @param scalar $oid
     */
    public function getPayment($oid)
    {
        //return payment by oid
    }
    
    /**
     * Saves the payment
     * 
     * @param \Cubes\Nestpay\Payment $payment
     * @return \Cubes\Nestpay\Payment
     */
    public function savePayment(Payment $payment)
    {
        //save existing payment
    }

    /**
     * Creates new payment
     *
     * @param array $properties
     * @return \Cubes\Nestpay\Payment
     */
    public function createPayment(array $properties)
    {
        //create new payment
    }
}

$nestpayMerchantService->setPaymentDao(new MyPaymentDao());


//THIS IS config/app.php

return [
    
    //got to providers key
    // ...

    'providers' => [
        //...

        \Cubes\Nestpay\Laravel\NestpayServiceProvider::class
    ],

    'aliases' => [
        //...
        // optinally add alias for facade
        'Nestpay' => \Cubes\Nestpay\Laravel\Facade::class,
    ],
];

namespace App\Http\Controllers;

use \Cubes\Nestpay\MerchantService;

class TestController extends Controller
{
    public function index(MerchantService $merchantService)
    {

    }
}


//Using facade
\Nestpay::paymentProcess3DGateResponse($request->all());

//using service container with "nestpay" key
app('nestpay')->paymentProcessOverNestpayApi($nestpayPayment->oid);

php artisan vendor:publish --provider="Cubes\\Nestpay\\Laravel\\NestpayServiceProvider"


//file: config/nestpay.php
return [
    'merchant' => [/* the mercant configuration*/],
   
    //change this if you want to use some other class for payment model
    //Object of paymentModel class is going to be returned when calling MerchantService::getWorkingPayment
     'paymentModel' => \App\Models\NestpayPayment::class 
    //...
];

//file: routes/web.php

\Nestpay::routes();


//file: app/Providers/EventServiceProvider.php

    protected $subscribe = [
        'App\Listeners\NestpayEventsSubscriber',
    ];

//file: database/migrations/2020_03_27_144802_create_nestpay_payments_table.php

public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->tinyInteger('processed')->default(0)->comment('1-processed; 0-not_processed');
            $table->char('oid', 64)->comment('Unique identifier of the order');

            // add your application specific fields like user_id or order_id etc..

            //DO NOT REMOVE ANY OF EXISTING COLUMNS!!!

//file: app\Migrations\NestpayPayment

namespace App\Models;

use Cubes\Nestpay\Laravel\PaymentModel as Model;

class NestpayPayment extends Model
{
    protected $table = 'nestpay_payments';

    protected $fillable = [

        //DO NOT REMOVE ANY FILLABLES JUST ADD NEW ONE FOOUR APPLICATION
        'processed',
        'oid',
        'trantype', 
        //...

class NestpayController extends Controller
{
    ...

    //You should definitively start from this point
    //customize how to read amount, currenct customer email and other stuff from your application
    protected function getPaymentData()
    {
        //...
    }
}

//file: app/Http/Middleware/VerifyCsrfToken.php

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        '/nestpay/success', //change this if you have customized routes
        '/nestpay/fail', //change this if you have customized routes
    ];
}

//file: app/Console/Kernel.php

class Kernel extends ConsoleKernel
{
    //...

    
    protected function schedule(Schedule $schedule)
    {
        //...
        $schedule->command('nestpay::handle-unprocessed-payments')->everyFiveMinutes();
    }

    //...


//file: app/Listeners/NestpayEventsSubscrber
class NestpayEventsSubscriber
{
    /**
     * Successfull payment
     */
    public function nestpayPaymentProcessedSuccessfullyEvent(NestpayPaymentProcessedSuccessfullyEvent $event) {
        $payment = $event->getPayment();

        //CUSTOMER HAS PAID, DO RELATED STUFF HERE

        //$payment is instanceof Eloquent Model which implements \Cubes\Nestpay\Payment interface

        //sending email
        \Mail::to(
            $payment->getProperty(Payment::PROP_EMAIL),
            $payment->getProperty(Payment::PROP_BILLTONAME)
        )->send(new NestpayPaymentMail($payment));
    }

    //...

resources   
│
└───views
│   │
│   └───vendor
│       │   
│       │───nestpay
│       |       │
│       |       │    confirm.blade.php
│       |       │    email.blade.php
│       |       │    result.blade.php