PHP code example of morningtrain / wp-nets-easy

1. Go to this page and download the library: Download morningtrain/wp-nets-easy 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/ */

    

morningtrain / wp-nets-easy example snippets


\Morningtrain\WpNetsEasy\NetsEasy::init('live-secret-key-abcdefghijklmnopqrstuvwxyz123456789');


    \Morningtrain\WP\Database\Database::migrate();

use Morningtrain\WpNetsEasy\Classes\Payment\Payment;
use Morningtrain\WpNetsEasy\Classes\Payment\Customer;
use Morningtrain\WpNetsEasy\Classes\Payment\Address;
use Morningtrain\WpNetsEasy\Classes\Payment\Item;

// Create payment and set payment information and urls
$payment = Payment::create()
    ->setReference($orderId)
    ->setCustomer(
        Customer::create()
            ->setReference($customer->id)
            ->setEmail($customer->email)
            ->setPhone($customer->phone)
            ->setName($customer->firstName, $customer->lastName)
            ->setCompanyName($customer->companyName)
            ->setShippingAddress(
                Address::create()
                    ->setAddressLine1($customer->address1)
                    ->setAddressLine2($customer->address2)
                    ->setPostalCode($customer->zipCode)
                    ->setCity($customer->city())
            )
        )
    ->setTermsUrl(get_post_permalink($termsPageId))
    ->setReturnUrl(Route::route('payment-success', ['token' => $order->token]))
    ->setCancelUrl(Route::route('payment-cancel', ['token' => $order->token]));

// Add items to payments
foreach($order->items as $item) {
    $payment->addItem(
        Item::create($item->sku)
            ->setName($item->name)
            ->setQuantity($item->quantity)
            ->setUnitPriceInclusiveTax($item->price)
    );
}

// Persist payment in NETS Easy
$response = $payment->createRequest();

if(wp_remote_retrieve_response_code($response) !== 201) {
    // Error handling when something was wrong with the payment
    wp_redirect($checkoutUrl);
    exit();
}

// Save payment reference to order
$order->setPaymentId($payment->getPaymentId());

// Redirect to payment page
wp_redirect($payment->getPaymentPageUrl());
exit();

$payment->autoCharge()

$payment = Payment::getByPaymentId($order->payment_id);

$payment->terminate()

$payment->isReserved()

$payment->isCharged()

$payment->charge()