Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/developer/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ By default, Pro PRs run `--grep @smoke`. Add `Suite: full` (or `Full-Suite: true
| `wp-config.php is missing` | Run the installer from inside the plugin directory (`wp-content/plugins/tutor`) of a WordPress install. |
| Database connection failed | Make sure MySQL is running and the `WP_TEST_DB_*` values in `.env` are correct. |
| Tests hit the live database | Check that the database routing block above is present in `wp-config.php`. |
| Need to undo `wp-config.php` changes | Restore `wp-config.php.bak`. |
| Need to undo `wp-config.php` changes | Restore `wp-config.php.bak`. |
72 changes: 65 additions & 7 deletions ecommerce/BillingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,12 @@ protected function validate( array $data ) {
'billing_first_name' => 'required',
'billing_last_name' => 'required',
'billing_email' => 'required|email',
'billing_phone' => 'required',
'billing_zip_code' => 'required',
'billing_address' => 'required',
'billing_country' => 'required',
'billing_state' => 'required',
'billing_city' => 'required',
);

foreach ( Settings::get_required_billing_fields() as $field => $is_required ) {
$validation_rules[ $field ] = $is_required ? 'required' : '';
}

// Countries without states have nothing to select.
if ( ! $this->model->country_has_states( wp_unslash( $data['billing_country'] ?? '' ) ) ) {
unset( $validation_rules['billing_state'] );
Expand All @@ -285,7 +283,16 @@ protected function validate( array $data ) {
}
}

return ValidationHelper::validate( $validation_rules, $data );
$validation = ValidationHelper::validate( $validation_rules, $data );

// Format checks run only on fields that passed the rules above.
$format_errors = array_diff_key( BillingModel::validate_field_formats( $data ), $validation->errors );
if ( ! empty( $format_errors ) ) {
$validation->errors = array_merge( $validation->errors, $format_errors );
$validation->success = false;
}

return $validation;
}

/**
Expand Down Expand Up @@ -354,4 +361,55 @@ public static function get_default_values() {

return $default_values;
}

/**
* Print HTML `pattern` and `title` attributes for a billing input.
*
* @since 4.3.0
*
* @param string $field Billing field name.
*
* @return void
*/
public static function render_pattern_attrs( string $field ): void {
$formats = BillingModel::get_field_formats();
if ( ! isset( $formats[ $field ] ) ) {
return;
}

printf(
' pattern="%s" title="%s"',
esc_attr( $formats[ $field ]['pattern'] ),
esc_attr( $formats[ $field ]['message'] )
);
}

/**
* Build the client-side validation rules passed to the form's register() call.
*
* @since 4.3.0
*
* @param string $field Billing field name.
* @param bool $required Whether the field is required.
*
* @return string JavaScript object literal, such as "{ required: true, pattern: {...} }".
*/
public static function get_client_rules( string $field, bool $required ): string {
$rules = array();
$formats = BillingModel::get_field_formats();

if ( $required ) {
$rules[] = 'required: true';
}

if ( isset( $formats[ $field ] ) ) {
$rules[] = sprintf(
"pattern: { value: /^(?:%s)$/u, message: '%s' }",
$formats[ $field ]['pattern'],
esc_js( $formats[ $field ]['message'] )
);
}

return '{ ' . implode( ', ', $rules ) . ' }';
}
}
24 changes: 15 additions & 9 deletions ecommerce/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,19 +622,20 @@ private function resolve_checkout_user_id() {
*/
protected function validate_pay_now_request( array $request, int $current_user_id = 0 ) {
// Reset on new validation request.
$errors = array();
$this->order_data = null;
$this->validated_consent = array();
$errors = array();
$this->order_data = null;
$this->validated_consent = array();
$billing_validation_rules = $this->billing_model->get_validation_rules();

$order_types_str = implode( ',', array( OrderModel::TYPE_SINGLE_ORDER, OrderModel::TYPE_SUBSCRIPTION, OrderModel::TYPE_RENEWAL ) );
$validation_rules = array_merge(
array(
'object_ids' => 'required|regex:/^[0-9]+(,[0-9]+)*$/',
'order_type' => "required|match_string:{$order_types_str}",
'payment_method' => 'required',
'payment_type' => 'if_input|match_string:automate,manual'
'payment_type' => 'if_input|match_string:automate,manual',
),
$this->billing_model->get_validation_rules()
$billing_validation_rules
);

$validation = ValidationHelper::validate( $validation_rules, $request );
Expand All @@ -644,8 +645,8 @@ protected function validate_pay_now_request( array $request, int $current_user_i
$errors = array_merge(
$errors,
$this->billing_model->validate_billing_location(
$request['billing_country'],
$request['billing_state']
$request['billing_country'] ?? '',
$request['billing_state'] ?? ''
)
);

Expand Down Expand Up @@ -722,7 +723,7 @@ protected function get_allowed_order_fields() {
* @return array An array of error messages.
*/
protected function save_billing_info( $user_id, $fields ) {
$billing_info = $this->billing_model->get_info( $user_id );
$billing_info = $this->billing_model->get_info( $user_id );

if ( $billing_info ) {
$update_billing = $this->billing_model->update( $fields, array( 'user_id' => $user_id ) );
Expand Down Expand Up @@ -879,7 +880,7 @@ public static function get_checkout_errors( int $user_id ): array {
*
* @return string
*/
protected static function prepare_checkout_alert_key( int $user_id ):string {
protected static function prepare_checkout_alert_key( int $user_id ): string {
return self::PAY_NOW_ALERT_MSG_TRANSIENT_KEY . $user_id;
}

Expand Down Expand Up @@ -1485,6 +1486,11 @@ public function pay_incomplete_order() {
exit;
}

$format_errors = BillingModel::validate_field_formats( $billing_fillable_fields );
if ( ! empty( $format_errors ) ) {
tutor_redirect_after_payment( OrderModel::ORDER_PLACEMENT_FAILED, $order_id, implode( ' ', array_merge( ...array_values( $format_errors ) ) ) );
}

if ( $order_id ) {
$order_model = new OrderModel();
$order_data = $order_model->get_order_by_id( $order_id );
Expand Down
13 changes: 13 additions & 0 deletions ecommerce/OptionKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ class OptionKeys {

const BUY_NOW = 'is_enable_buy_now';

/**
* Whether the billing address fields are required.
*
* @since 4.3.0
*/
const IS_BILLING_COUNTRY_REQUIRED = 'is_billing_country_required';
const IS_BILLING_STATE_REQUIRED = 'is_billing_state_required';
const IS_BILLING_CITY_REQUIRED = 'is_billing_city_required';
const IS_BILLING_ZIP_CODE_REQUIRED = 'is_billing_zip_code_required';
const IS_BILLING_PHONE_REQUIRED = 'is_billing_phone_required';
const IS_BILLING_ADDRESS_REQUIRED = 'is_billing_address_required';


/**
* Get billing field options
*
Expand Down
57 changes: 2 additions & 55 deletions ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Missing file doc comment

namespace Ollyo\PaymentHub\Payments\Paypal;

Expand All @@ -24,7 +24,7 @@
* @since 3.0.0
* @since 4.2.0 Uses Utils::isTotalAmountZero() instead of System::isTotalAmountZero().
*/
public static function getItems( &$data ): array {

Check failure on line 27 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Method name &quot;getItems&quot; in class Helper is not in snake case format, try &quot;get_items&quot;
$currency = $data->currency->code;
$data->subtotal = 0;

Expand All @@ -49,9 +49,9 @@
(array) $data->items
);

$minChargeApplicable = Utils::isTotalAmountZero( $data );

Check failure on line 52 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Variable &quot;$minChargeApplicable&quot; is not in valid snake_case format, try &quot;$min_charge_applicable&quot;

if ( $minChargeApplicable ) {

Check failure on line 54 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Variable &quot;$minChargeApplicable&quot; is not in valid snake_case format, try &quot;$min_charge_applicable&quot;
$items[] = array(
'name' => 'Minimum Charge',
'description' => 'Minimum charge to process the payment',
Expand Down Expand Up @@ -79,13 +79,13 @@
* @return array The formatted amount data including currency code, total price, and breakdown.
* @since 3.0.0
*/
public static function createAmountData( $data ): array {

Check failure on line 82 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Method name &quot;createAmountData&quot; in class Helper is not in snake case format, try &quot;create_amount_data&quot;
$returnData = array(

Check failure on line 83 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Variable &quot;$returnData&quot; is not in valid snake_case format, try &quot;$return_data&quot;
'currency_code' => $data->currency->code,
'value' => number_format( $data->total_price, 2, '.', '' ),
);

$extraCharges = array(

Check failure on line 88 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Variable &quot;$extraCharges&quot; is not in valid snake_case format, try &quot;$extra_charges&quot;
'shipping_charge' => 'shipping',
'tax' => 'tax_total',
'coupon_discount' => 'discount',
Expand All @@ -93,11 +93,11 @@
);

array_walk(
$extraCharges,

Check failure on line 96 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Variable &quot;$extraCharges&quot; is not in valid snake_case format, try &quot;$extra_charges&quot;
function ( $value, $key ) use ( $data, &$returnData ) {

Check failure on line 97 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Variable &quot;$returnData&quot; is not in valid snake_case format, try &quot;$return_data&quot;

if ( isset( $data->$key ) && ! empty( $data->$key ) ) {
$returnData['breakdown'][ $value ] = array(

Check failure on line 100 in ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Helper.php

View workflow job for this annotation

GitHub Actions / WPCS

Variable &quot;$returnData&quot; is not in valid snake_case format, try &quot;$return_data&quot;
'currency_code' => $data->currency->code,
'value' => number_format( $data->$key, 2, '.', '' ),
);
Expand Down Expand Up @@ -253,34 +253,6 @@
return $final_message;
}

/**
* Creates the shipping information array for a payment request.
*
* This method constructs and returns an array containing the shipping type, receiver's full name,
* and address for a payment request.
*
* @param object $shipping The shipping data containing the receiver's name and address.
* @return array The formatted shipping information including type, name, and address.
* @since 3.0.0
* @since 4.2.0 Uses Utils::splitAddress() instead of System::splitAddress().
*/
public static function getShippingInfo( $shipping ): array {
[$address1, $address2] = Utils::splitAddress( $shipping, 300 );

return array(
'type' => 'SHIPPING',
'name' => array( 'full_name' => $shipping->name ),
'address' => array(
'address_line_1' => $address1,
'address_line_2' => $address2,
'admin_area_2' => $shipping->city,
'admin_area_1' => $shipping->region,
'postal_code' => $shipping->postal_code,
'country_code' => $shipping->country->alpha_2,
),
);
}

/**
* Retrieves the URL from an array of links that matches a specified condition.
*
Expand Down Expand Up @@ -338,8 +310,7 @@
return null;
}

$paypal_source = $vault_details->payment_source->paypal;
$shipping_address = $paypal_source->shipping->address;
$paypal_source = $vault_details->payment_source->paypal;

return array(
'paypal' => array(
Expand All @@ -357,32 +328,10 @@
'experience_context' => array(
'payment_method_preference' => 'IMMEDIATE_PAYMENT_REQUIRED',
),
'address' => self::format_address( $shipping_address ),
),
);
}

/**
* Format a shipping address object into an associative array.
*
* @since 3.9.0
*
* @param object $shipping_address The PayPal shipping address object.
*
* @return array The formatted address as an associative array.
*/
private static function format_address( $shipping_address ): array {

return array(
'address_line_1' => $shipping_address->address_line_1,
'address_line_2' => $shipping_address->address_line_2,
'admin_area_1' => $shipping_address->admin_area_1 ?? '',
'admin_area_2' => $shipping_address->admin_area_2 ?? '',
'postal_code' => $shipping_address->postal_code,
'country_code' => $shipping_address->country_code,
);
}

/**
* Build a PayPal payment source array using order details.
*
Expand All @@ -402,8 +351,7 @@
return null;
}

$paypal_source = $order_details->payment_source->paypal;
$shipping_address = $order_details->purchase_units[0]->shipping->address;
$paypal_source = $order_details->payment_source->paypal;

return array(
'paypal' => array(
Expand All @@ -421,7 +369,6 @@
'experience_context' => array(
'payment_method_preference' => 'IMMEDIATE_PAYMENT_REQUIRED',
),
'address' => self::format_address( $shipping_address ),
),
);
}
Expand Down
21 changes: 4 additions & 17 deletions ecommerce/PaymentGateways/Paypal/src/Payments/Paypal/Paypal.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,14 @@ public function prepareData( $data ): array {
$items = 'one-time' === $type ? Helper::getItems( $data ) : null;
$amount = 'one-time' === $type ? Helper::createAmountData( $data ) : Helper::createAmountForRecurring( $data );
$tutor_metadata = Utils::prepareMerchantMetadata( $data->order_user_id, $this->config->get( 'mode' ) );
$description = implode(
',',
array_map(
fn ( $key, $value ) => "{$key}: {$value}",
array_keys( $tutor_metadata ),
$tutor_metadata
)
);

$returnData = array(
'purchase_units' => array(
array(
'custom_id' => $data->order_id,
'items' => $items,
'amount' => $amount,
'payee' => array( 'email_address' => $this->config->get( 'merchant_email' ) ),
'description' => $description,
'custom_id' => $data->order_id,
'items' => $items,
'amount' => $amount,
'payee' => array( 'email_address' => $this->config->get( 'merchant_email' ) ),
),
),
'intent' => 'CAPTURE',
Expand All @@ -176,10 +167,6 @@ public function prepareData( $data ): array {
Helper::getPaymentSourceForRecurring( $returnData, $this->previousPayload );
}

if ( isset( $data->shipping_address ) && ! empty( $data->shipping_address ) ) {
$returnData['purchase_units'][0]['shipping'] = Helper::getShippingInfo( $data->shipping_address );
}

return $returnData;
}

Expand Down
Loading
Loading