Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving error handling for invoice generation #165

Merged
merged 1 commit into from
Jul 15, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ public static function set_delete_inactive_products( bool $value ): bool {
* Get wether to make a credit invoice when an order is labelled as refunded
*/
public static function get_make_credit_invoice(): bool {
return (bool) get_option( 'make_credit_invoice', false );
return (bool) get_option( '1984_woo_dk_make_credit_invoice', false );
}

/**
Expand All @@ -653,7 +653,7 @@ public static function get_make_credit_invoice(): bool {
* false to disable it.
*/
public static function set_make_credit_invoice( bool $value ): bool {
return update_option( 'make_credit_invoice', (int) $value );
return update_option( '1984_woo_dk_make_credit_invoice', (int) $value );
}

/**
Expand Down
32 changes: 29 additions & 3 deletions src/Export/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class Invoice {
public static function create_in_dk(
WC_Order $wc_order
): string|false|WP_Error {
if ( ! ExportCustomer::is_in_dk( OrderHelper::get_kennitala( $wc_order ) ) ) {
if (
! ExportCustomer::is_in_dk(
OrderHelper::get_kennitala( $wc_order )
)
) {
Customer::create_in_dk_from_order( $wc_order );
}

Expand All @@ -58,7 +62,16 @@ public static function create_in_dk(
}

if ( $result->response_code !== 200 ) {
return false;
if ( property_exists( $result->data, 'Message' ) ) {
$error_message = $result->data->Message;
} else {
$error_message = '';
}
return new WP_Error(
'http_' . (string) $result->response_code,
$error_message,
$result->data
);
}

if ( property_exists( $result->data, 'Number' ) ) {
Expand Down Expand Up @@ -93,6 +106,10 @@ public static function reverse_in_dk(
return false;
}

if ( ! empty( self::get_dk_credit_invoice_number( $wc_order ) ) ) {
return false;
}

$date = new DateTime();
$formatted_date = $date->format( 'Y-m-d' );

Expand All @@ -108,7 +125,16 @@ public static function reverse_in_dk(
}

if ( $result->response_code !== 200 ) {
return false;
if ( property_exists( $result->data, 'Message' ) ) {
$error_message = $result->data->Message;
} else {
$error_message = '';
}
return new WP_Error(
'http_' . (string) $result->response_code,
$error_message,
$result->data
);
}

if ( property_exists( $result->data, 'Number' ) ) {
Expand Down
73 changes: 62 additions & 11 deletions src/Hooks/WooOrderStatusChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public static function maybe_send_invoice_on_payment(
return;
}

if (
! empty(
$wc_order->get_meta(
'1984_dk_woo_invoice_creation_error',
true
)
)
) {
return;
}

$kennitala = OrderHelper::get_kennitala( $wc_order );

if (
Expand Down Expand Up @@ -136,17 +147,30 @@ public static function maybe_send_invoice_on_payment(
);
}
}
} elseif ( ! $invoice_number ) {
} elseif ( $invoice_number instanceof WP_Error ) {
$wc_order->update_meta_data(
'1984_dk_woo_invoice_creation_error',
$invoice_number->get_error_code()
);
$wc_order->update_meta_data(
'1984_dk_woo_invoice_creation_error_message',
$invoice_number->get_error_message()
);
$wc_order->update_meta_data(
'1984_dk_woo_invoice_creation_error_data',
$invoice_number->get_error_data()
);
$wc_order->add_order_note(
__(
'An invoice could not be created in DK due to an error. The most common reason is the SKU for the WooCommece product does not match the ‘Item Code’ for the corresponding product in DK.',
'Unable to create an invoice in DK: ',
'1984-dk-woo'
)
) . $invoice_number->get_error_code()
);
} elseif ( $invoice_number instanceof WP_Error ) {
$wc_order->save();
} else {
$wc_order->add_order_note(
__(
'Unable to establish a connection with DK to create an invoice.',
'An invoice could not be created in DK due to an unhandled error.',
'1984-dk-woo'
)
);
Expand All @@ -169,7 +193,22 @@ public static function maybe_send_credit_invoice_on_refund( int $order_id ): voi

$wc_order = new WC_Order( $order_id );

if ( ! empty( ExportInvoice::get_dk_credit_invoice_number( $wc_order ) ) ) {
if (
! empty(
ExportInvoice::get_dk_credit_invoice_number( $wc_order )
)
) {
return;
}

if (
! empty(
$wc_order->get_meta(
'1984_dk_woo_credit_invoice_creation_error',
true
)
)
) {
return;
}

Expand Down Expand Up @@ -204,17 +243,29 @@ public static function maybe_send_credit_invoice_on_refund( int $order_id ): voi
);
}
}
} elseif ( ! $credit_invoice_number ) {
} elseif ( $credit_invoice_number instanceof WP_Error ) {
$wc_order->update_meta_data(
'1984_dk_woo_credit_invoice_creation_error',
$credit_invoice_number->get_error_code()
);
$wc_order->update_meta_data(
'1984_dk_woo_credit_invoice_creation_error_message',
$credit_invoice_number->get_error_message()
);
$wc_order->update_meta_data(
'1984_dk_woo_credit_invoice_creation_error_data',
$credit_invoice_number->get_error_data()
);
$wc_order->add_order_note(
__(
'Connection was established to DK but a credit invoice was not created due to an error.',
'Unable to create an invoice in DK: ',
'1984-dk-woo'
)
) . $credit_invoice_number->get_error_code()
);
} elseif ( $credit_invoice_number instanceof WP_Error ) {
} else {
$wc_order->add_order_note(
__(
'Unable to establish a connection with DK to create a credit invoice.',
'A credit invoice could not be created in DK due to an unhandled error.',
'1984-dk-woo'
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public static function rest_api_callback(
);
}

if ( property_exists( $rest_json, 'email_credit_invoice' ) ) {
if ( property_exists( $rest_json, 'make_credit_invoice' ) ) {
Config::set_make_credit_invoice( $rest_json->make_credit_invoice );
}

Expand Down
2 changes: 1 addition & 1 deletion views/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class="regular-text api-key-input"
<tbody>
<?php foreach ( ( new WC_Payment_Gateways() )->payment_gateways as $p ) : ?>
<?php
if ( 'no' === $p->enabled ) {
if ( $p->enabled === 'no' ) {
continue;
}
$payment_map = Config::get_payment_mapping( $p->id );
Expand Down
Loading