Warning: chmod(): Operation not permitted in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [0] in function chmod in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [1] in function include_once in /var/www/hopeinstoughton_www/wp-settings.php on line 560 [2] in function require_once in /var/www/hopeinstoughton_www/wp-config.php on line 85 [3] in function require_once in /var/www/hopeinstoughton_www/wp-load.php on line 50 [4] in function require_once in /var/www/hopeinstoughton_www/wp-blog-header.php on line 13 [5] in function require in /var/www/hopeinstoughton_www/index.php on line 17
| Server IP : 94.177.8.99 / Your IP : 216.73.217.165 Web Server : Apache/2.4.58 (Ubuntu) System : Linux aries 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : www-data ( 33) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/kjvdictionary_store/modules/storePaypal/ |
Upload File : |
<?php
namespace modules\storePaypal;
class main extends \moduleMain {
use \modules\input\traits {
\modules\input\traits::__construct as __ConstructInput;
}
use \modules\database\traits {
\modules\database\traits::__construct as __ConstructDatabase;
}
use \modules\output\traits {
\modules\output\traits::__construct as __ConstructOutput;
}
private $css;
private $view;
private $model_paypal;
private $model_order;
const PAYMETH = 'paypal';
public function __construct() {
parent::__construct(__DIR__);
$this->__ConstructInput();
$this->__ConstructDatabase();
$this->__ConstructOutput();
}
public function Activate() {
hook_add('html.prepare', [$this, '__head']);
hook_add('template.store.checkout.COL2', [$this, '__column2']);
hook_add('template.store.fulfill.COL1', [$this, '__column1'], 10); // process the paypal payment
hook_add('template.store.fulfill.COL3', [$this, '__column3']);
hook_add('order.json.full', [$this, '__order_json'], 90); // return the json for the full checkout page
hook_add('order.json.delivery', [$this, '__order_delivery'], 90); // return the json for the delivery panel on checkout page
hook_add('order.json.confirm', [$this, '__order_confirm'], 90);
hook_add('order.update.freight', [$this, '__order_update_freight'], 90); // must be after priority 50
hook_add('order.payment.paypal', [$this, '__order_payment']); // capture full/partial payment
hook_add('order.approve.paypal', [$this, '__order_approve']); // click in Confirm Order
// to do: attach tracking number(s) to an order
$this->view = $GLOBALS['loader']->getView('paypal_view', __DIR__);
$this->model_paypal = $this->loadModel('paypal_model');
// address model not loaded here - we use paypal deets instead
$this->model_order = $this->loadModel('order_model', 'store');
hook_add('uri', [$this, '__intercept_checkout'], 90);
}
public function __column3(&$html) { // payment method
$text = $this->view->Column3Text();
$this->view->UpdateHTML($html, 'COL3', $text);
}
public function __column1(&$html) {
$text = $this->view->Column1Text();
$this->view->UpdateHTML($html, 'COL1', $text);
}
public function __column2(&$html) {
$text = $this->view->Column2Text();
$this->view->UpdateHTML($html, 'COL2', $text);
}
private function isLive() {
return ($this->settings['mode'] ?? '') == 'live';
}
private function isActive() {
if ($this->isLive()) {
return !empty($this->settings['clientid']) && !empty($this->settings['secret']);
} else {
return !empty($this->settings['clientidx']) && !empty($this->settings['secretx']);
}
}
public function __head(&$theme) {
if ($this->isActive()) {
if ($this->isLive()) {
$clientid = rawurlencode($this->settings['clientid']);
} else {
$clientid = rawurlencode($this->settings['clientidx']);
}
$currency = hook_execute('store.currency', '');
$intent = empty($this->settings['intent']) ? 'capture' : 'authorize';
// same javascript is for live or sandbox. difference is client_id
$this->includeFile("https://www.paypal.com/sdk/js?client-id={$clientid}¤cy={$currency}&intent={$intent}", 'paypal.js');
$base = $this->StaticUriModule(__DIR__);
$this->includeFile("$base/paypal_checkout.js")->depends('paypal.js');
}
}
private function _loadInternalOrder($order_id) {
$this->model_order->OrderId($order_id);
$items = $this->model_order->AsHandleBars_Items();
// add current product details - upc, sku, etc
$items = hook_execute('product.overlay', $items);
$order_data = $this->model_order->AsHandleBars();
$order_data['order_id'] = $this->model_order->OrderId();
$order_data['items'] = $items;
// get the currency
$currency = hook_execute('store.currency', '');
$isLive = $this->isLive();
$this->model_paypal
->Currency($currency)
->OrderData($order_data)
->SandBox(!$isLive);
if ($isLive) {
$this->model_paypal
->ClientId($this->settings['clientid'])
->Secret($this->settings['secret']);
} else {
$this->model_paypal
->ClientId($this->settings['clientidx'])
->Secret($this->settings['secretx']);
}
}
// start a paypal order
final function order_create() {
// get the current order from the Store
$order_id = $_SESSION['order'];
$intent = !empty($this->settings['intent']);
$this->_loadInternalOrder($order_id);
$data = $this->model_paypal->createOrder($intent);
header('Content-type: application/json');
die(json_encode($data, JSON_UNESCAPED_SLASHES));
}
// update the freight charges once paypal user has logged in, but not before order is finalized
final function order_change() {
$paypal_id = $_POST['orderID'];
$address = $_POST['shippingAddress']; // this does not contain street
$this->model_paypal->PaypalId($paypal_id);
$order_id = $this->model_paypal->OrderId();
// calculate a new freight charge based on zip
$this->_loadInternalOrder($order_id);
// get freight options
$params = $this->model_order->getFreightParameters($address['postalCode']); // country is extracted from postalCode
$services = $params->services;
$first = [];
while(count($services)) {
$first = array_shift($services);
if ($first['cost']) break;
}
// update the order
if ($this->model_order->getPhysicalFlag()) {
$this->model_paypal->UpdateShipping($first['cost']);
} else {
$this->model_paypal->UpdateShipping(0);
}
// update our copy of the order using $first
$this->model_order->updateOrderFreight($first['key'], $params->targetZip, $params->targetCtry);
// update our Subtotal panel
$res = [
'html' => [
[
'field' => 'subtotals',
'template' => 'store_panel_subtotal',
]
],
];
$data = $this->model_order->AsHandleBars();
$res = array_merge($res, $data);
header('Content-type: application/json');
die(json_encode($res, JSON_UNESCAPED_SLASHES));
}
// paypal user has clicked on "Process Order". We create an Authorize intent - to be authorized/finalized laterd
final function order_finalize() {
$paypal_id = $_POST['orderID'];
$this->model_paypal->PaypalId($paypal_id);
$order_id = $this->model_paypal->OrderId();
$this->_loadInternalOrder($order_id);
// calculate a new freight charge based on zip, authorize hold of payment
$intent = !empty($this->settings['intent']);
$this->model_paypal->HoldOrder($intent);
// PARTIAL SETTLEMENT: For Digital orders
// https://developer.paypal.com/docs/api/payments/v2/#authorizations_capture
$this->TableName('store_order')
->InsertOrUpdate(['so_order' => $order_id], [ 'so_paymeth' => static::PAYMETH ]);
$post = [
'o_terms' => 1, // agree to terms
];
// if indent = true: the order has not been charged yet. Should update status to hold
$settings = [];
$res = hook_execute('order.update.confirm', [], $order_id, $post, $settings);
header('Content-type: application/json');
die(json_encode($res, JSON_UNESCAPED_SLASHES));
}
public function __order_json(&$json, $order_id, $u_id) {
$this->model_order->OrderId($order_id);
if ($this->model_order->Status() == 'paypal') { // is this a paypal order?
$json['ro_account'] = 1;
$json['ro_address'] = 1;
$json['single'] = 1; // only ever a single address. But which one?
$json['can_add'] = 0; // addresses only added via paypal
$json['hide_paypal'] = 1;
$json['hide_payment'] = 1;
// default to freight? ...
$json['accordion'] = 'freight';
$json['method'] = 'paypal';
}
}
public function __order_delivery(&$json, $order_id, $u_id) {
$this->model_order->OrderId($order_id);
if ($this->model_order->Status() == 'paypal') { // is this a paypal order?
$json['ro_address'] = 1;
$json['single'] = 1; // only ever a single address. But which one?
$json['can_add'] = 0; // addresses only added via paypal
// get specified address
$special = [];
foreach($json['list_address'] as $addr) {
if ($addr['guid'] == $json['default']) {
$special = $addr;
break;
}
}
$json = array_merge($json, $special);
}
}
public function __order_confirm(&$json, $order_id, $u_id) {
$this->model_order->OrderId($order_id);
if ($this->model_order->Status() == 'paypal') { // is this a paypal order?
$json['method'] = 'paypal';
}
}
public function __order_update_freight(&$res, $order_id, $post, &$settings) {
$this->model_order->OrderId($order_id);
if ($this->model_order->Status() == 'paypal') { // is this a paypal order?
// get freight charge
$info = $this->model_order->AsHandleBars();
$amount = $info['freightamt'];
// get PaypalId
$paypal_id = $this->model_paypal->fromOrderId($order_id);
$this->model_paypal
->Currency('USD')
->ClientId($this->settings['clientid'])
->Secret($this->settings['secret'])
->PaypalId($paypal_id)
// update paypal
->UpdateShipping($amount);
}
}
final function finalize() {
$order_id = hook_execute('nonce.verify', FALSE, 'order_id', FALSE, $this->param('auth', ''));
if ($order_id === FALSE) {
return [ 'result' => 72005, 'message' => 'invalid or expired authorization token' ];
}
// get our order
$this->model_order->OrderId($order_id);
$paypal_id = $this->model_paypal->fromOrderId($order_id);
$error = $this->model_paypal
->Currency('USD')
->ClientId($this->settings['clientid'])
->Secret($this->settings['secret'])
->PaypalId($paypal_id)
->finalizeOrder(); // process the payment
if (is_string($error)) {
return [
'result' => 72006,
'message' => 'Paypal error - ' . $result,
];
}
return [
'result' => -9999,
'message' => 'Yippee! Payment has gone through',
// redisplay
];
}
// if $params->partial is true, expect another call to __order_payment at some point
public function __order_payment($result, $params) {
$this->_loadInternalOrder($params->order_id);
$this->model_paypal->ProcessPayment($params);
echo "__order_payment\n";
var_dump($result);
var_dump($params);
}
// https://developer.paypal.com/docs/checkout/standard/customize/authorization/ ??
public function __order_approve($result, $params) {
$this->_loadInternalOrder($params->order_id);
// authorize the order
}
public function __intercept_checkout(&$uri) {
$myuri = $uri;
if (($p = strpos($myuri, '?')) !== FALSE) {
$param = substr($myuri, $p);
$myuri = substr($myuri, 0, $p);
} else {
$param = '';
}
if ($myuri == '/store/checkout' && $this->isActive() && intval($this->settings['checkout'])) {
$uri = '/store/checkout-paypal';
}
}
}
/*
PHP message: PHP Warning: Undefined array key "order" in /var/www/kjvdictionary_store/modules/storePaypal/main.php on line 97;
PHP message: PHP Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /var/www/kjvdictionary_store/modules/store/model/order_model.php:404
Stack trace:
#0 /var/www/kjvdictionary_store/modules/storePaypal/main.php(84): StoreOrder_model->AsHandleBars()
#1 /var/www/kjvdictionary_store/modules/storePaypal/main.php(98): modules\\storePaypal\\main->_loadInternalOrder()
#2 /var/www/kjvdictionary_store/modules/pkpCore/model/destination.php(219): modules\\storePaypal\\main->order_create()
#3 /var/www/kjvdictionary_store/modules/pkpCore/main.php(292): Destination->Execute()
#4 /var/www/kjvdictionary_store/public/index.php(70): modules\\pkpCore\\main->run()
#5 {main}
thrown in /var/www/kjvdictionary_store/modules/store/model/order_model.php on line 404'
*/