PHP code example of twohill / silverstripe-pxpay

1. Go to this page and download the library: Download twohill/silverstripe-pxpay 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/ */

    

twohill / silverstripe-pxpay example snippets



class MyPageController extends PageController
{
    private static $allowed_actions = [
        'thankyou',
        'pay_order',
        'unsuccessful',
    ];
    
    public function thankyou(HTTPRequest $request)
    {
        $content = '';
        if ($request->getSession()->get('OrderID')) {
            $order = Order::get()->byID($request->getSession()->get('OrderID'));

            if ($order) {
           
                $payment = $order->Payment();

                if ($payment && $payment->TxnId) {
                    if ($payment->Processed) {
                        $sendEmail = false;
                    } else {
                        $payment->Processed = true;
                        $payment->write();
                    }
                } else {
                    $this->redirect($this->Link('pay-order/submit'));
                }
                $content = $this->ThankYouForPayingContent; // From MyPage $db
            }

            $request->getSession()->clear("OrderID");
            return $this->customise(new ArrayData([
                'Content' => DBField::create_field('HTMLFragment', $content),
                'Form' => ''
            ]));
        }
        return $this->redirect($this->Link());
    }
    /**
     * Process the payment
     *
     * @param HTTPRequest $request
     * @return PxPaymentController
     * @throws ValidationException
     */
    public function pay_order(HTTPRequest $request)
    {
    
        // Load the payment details somehow
        $payment = null;

        if ($request->getSession()->get('OrderID')) {
           
            $order = Order::get()->byID($request->getSession()->get('OrderID'));

            if ($order) {
                $payment = new PxPayment();
                $payment->TxnType = "Purchase";
                $payment->MerchantReference = $order->InvoiceNumber;
                $payment->TxnData1 = $order->CompanyName;
                $payment->TxnData2 = $order->Address;
                $payment->TxnData3 = $order->City;
                $payment->EmailAddress = $order->Contact()->Email;
                $payment->AmountInput = $order->Total;
                $payment->CurrencyInput = "NZD";
                $payment->InvoiceID = $order->ID;
                $payment->write();
                }
            }
        }
        return new PxPaymentController($this, $request, $payment, $this->Link("thankyou"), $this->Link("unsuccessful"));
    }

    /** 
     * Action when payment is unsuccessful. 
     */
    public function unsuccessful(HTTPRequest $request)
    {
        if ($request->getSession()->get('OrderID')) {
            $order = Order::get()->byID($request->getSession()->get('OrderID'));
            if ($order) {
                return $this->customise([
                    'Content' => DBField::create_field('HTMLFragment', $this->UnsuccessfulContent),
                    'Form' => ''
                ]);
            }
        }
        return $this->redirect($this->Link());
    }


}