Skip to content

Commit

Permalink
Added separate input check method
Browse files Browse the repository at this point in the history
  • Loading branch information
andriussok committed Jul 19, 2024
1 parent f9a9ab4 commit 26f9750
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
# Experience Days Gateway

A simple WooCommerce payment gateway for handling Experience Days Vouchers.
A WooCommerce payment gateway that allows customers to checkout using Experience Days Vouchers.

- Allows customers to checkout with a voucher code.
- Creates orders with "on-hold" status for admin approval.
- Stores voucher details in order metadata and customer notes.
- Works with Classic Checkout and supports Gutenberg Blocks.
- **Voucher Checkout**: Customers can enter their Experience Days Voucher code during checkout.
- **Order Management**: Orders are created with "on-hold" status for admin approval and voucher verification.
- **Voucher Storage**: Stores voucher details in order metadata and customer notes.
- **Compatibility**: Works with Classic Checkout and Gutenberg Blocks.

## Usage

1. Install the plugin and configure it in `WooCommerce > Settings > Payments`.
2. Customers select Experience Days Gateway during checkout.
3. Orders are created with "on-hold" status for admin review and approval.
Download the latest release [experience-days-gateway.zip](https://github.com/andriussok/experience-days-gateway/releases)
1. Install the plugin via the WordPress admin panel.
2. Configure it in `WooCommerce > Settings > Payments`.
3. Customers select Experience Days Gateway during checkout and enter their voucher code.
4. Orders are created with "on-hold" status for admin review.

## Dependencies

- [WooCommerce](https://en-gb.wordpress.org/plugins/woocommerce/)

## Development

You can modify php files directly in the plugin.

To make changes for Gutenberg Blocks:
1. Clone the repository
2. `npm install` - install dependencies
3. `npm run start` - for development
4. `npm run build` for production
5. `npm run zip` for distribution

## References

- [@wordpress/scripts](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/)
2 changes: 1 addition & 1 deletion experience-days-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
Plugin Name: Experience Days Gateway
Description: A custom WooCommerce payment gateway for handling Experience Days Vouchers.
Version: 1.0.0
Version: 1.0.1
Author: Andrius Sok
Author URI: andriuss.lt
License: GPL-3.0-or-later
Expand Down
43 changes: 30 additions & 13 deletions includes/class-wc-experience-days-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function init_form_fields() {
'title' => __('Enable/Disable', 'experience-days-gateway'),
'type' => 'checkbox',
'label' => __('Enable Experience Days Gateway', 'experience-days-gateway'),
'default' => 'yes',
'default' => 'no',
),
'title' => array(
'title' => __('Title', 'experience-days-gateway'),
Expand Down Expand Up @@ -111,12 +111,33 @@ public function validate_fields() {
return true;
}

/**
* Process and sanitize voucher code from request
*/
public function get_sanitized_voucher_code() {
// Initialize voucher code variable
$voucher_code = '';

// Check if it's a block checkout request and sanitize the input
if (isset($_POST['experiencedaysvoucher'])) {
$voucher_code = sanitize_text_field($_POST['experiencedaysvoucher']);
} else {
// Fallback for classic checkout and sanitize the input
if (isset($_POST['experience_days_voucher'])) {
$voucher_code = sanitize_text_field($_POST['experience_days_voucher']);
}
}

return $voucher_code;
}


/**
* Save voucher code and add to customer notes
*/
public function save_voucher_code($order_id, $voucher_code) {
if (!empty($voucher_code)) {
// store value in metadata;
// Store value in metadata
update_post_meta($order_id, '_experience_days_voucher', $voucher_code);

// Add voucher code to customer notes if not already added
Expand All @@ -127,28 +148,24 @@ public function save_voucher_code($order_id, $voucher_code) {
if (!empty($existing_note)) {
$new_note = "$existing_note -- $new_note";
}
$order->set_customer_note ( $new_note );
$order->set_customer_note($new_note);
$order->save();
}
}
}


/**
* Process payment
*/
public function process_payment($order_id) {
$order = wc_get_order($order_id);

error_log(json_encode($_POST));

// Check if it's a block checkout request
if (isset($_POST['experiencedaysvoucher'])) {
$voucher_code = sanitize_text_field($_POST['experiencedaysvoucher']);
$this->save_voucher_code($order_id, $voucher_code);
} else {
// Fallback for classic checkout
$this->save_voucher_code($order_id, $_POST['experience_days_voucher'] ?? '');
}
// Get sanitized voucher code
$voucher_code = $this->get_sanitized_voucher_code();

// Save the sanitized voucher code
$this->save_voucher_code($order_id, $voucher_code);

// Mark the order as on-hold
$order->update_status('on-hold', __('Awaiting voucher verification', 'experience-days-gateway'));
Expand Down

0 comments on commit 26f9750

Please sign in to comment.