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
});
$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
}
}
//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);
//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: 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));
}
//...