| 1 |
<?php |
| 2 |
/** |
| 3 |
* Offline payment gateway class. |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Offline-Payment/Classes |
| 7 |
* @version 4.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
use LearnPress\Helpers\Config; |
| 11 |
use LearnPress\Helpers\Singleton; |
| 12 |
// Prevent loading this file directly |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
if ( ! function_exists( 'LP_Gateway_Offline_Payment' ) ) { |
| 16 |
/** |
| 17 |
* Class LP_Gateway_Offline_Payment. |
| 18 |
*/ |
| 19 |
class LP_Gateway_Offline_Payment extends LP_Gateway_Abstract { |
| 20 |
use Singleton; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var LP_Settings |
| 24 |
*/ |
| 25 |
protected $settings; |
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $id = 'offline-payment'; |
| 30 |
|
| 31 |
public function init() { |
| 32 |
// TODO: Implement init() method. |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor for the gateway. |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
|
| 41 |
$this->icon = $this->settings->get( 'icon', LP_PLUGIN_URL . 'inc/gateways/offline-payment/images/cod.png' ); |
| 42 |
$this->method_title = __( 'Offline Payment', 'learnpress' ); |
| 43 |
$this->method_description = __( 'Make a payment with cash.', 'learnpress' ); |
| 44 |
|
| 45 |
// Get settings |
| 46 |
$this->title = $this->settings->get( 'title', $this->method_title ); |
| 47 |
$this->description = $this->settings->get( 'description', $this->method_description ); |
| 48 |
$this->enabled = $this->settings->get( 'enable', 'yes' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Admin payment settings. |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function get_settings(): array { |
| 57 |
return Config::instance()->get( $this->id, 'settings/gateway', [ 'lp_gateway_offline_payment' => $this ] ); |
| 58 |
} |
| 59 |
/** |
| 60 |
* Payment form. |
| 61 |
*/ |
| 62 |
public function get_payment_form() { |
| 63 |
return wp_kses_post( LP_Settings::instance()->get( $this->id . '.description', $this->description ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Process the payment and return the result |
| 68 |
* |
| 69 |
* @param $order_id |
| 70 |
* |
| 71 |
* @return array |
| 72 |
* @throws Exception |
| 73 |
*/ |
| 74 |
public function process_payment( $order_id ) { |
| 75 |
|
| 76 |
$order = learn_press_get_order( $order_id ); |
| 77 |
|
| 78 |
// Mark as processing (payment won't be taken until delivery) |
| 79 |
$default_status = LP_ORDER_PROCESSING; |
| 80 |
|
| 81 |
/** |
| 82 |
* If sandbox mode is turn on then the order |
| 83 |
* should be completed immediately. |
| 84 |
* |
| 85 |
* @since 3.x.x |
| 86 |
*/ |
| 87 |
if ( LP_Settings::instance()->get( $this->id . '.sandbox', 'no' ) === 'yes' ) { |
| 88 |
$default_status = 'completed'; |
| 89 |
} |
| 90 |
|
| 91 |
$order->update_status( $default_status, __( 'Payment can be made upon delivery.', 'learnpress' ) ); |
| 92 |
|
| 93 |
// Remove cart |
| 94 |
LearnPress::instance()->cart->empty_cart(); |
| 95 |
|
| 96 |
// Return thank you redirect |
| 97 |
return array( |
| 98 |
'result' => 'success', |
| 99 |
'redirect' => $this->get_return_url( $order ), |
| 100 |
); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|