strangerstudios / pmpro-vat-tax

Calculate VAT tax at membership checkout with Paid Memberships Pro. Allows customers with a VAT Number to avoid the tax.
https://www.paidmembershipspro.com/add-ons/vat-tax/
8 stars 20 forks source link

Never show errors when debug is NOT set. Write on the logs. #60

Closed mircobabini closed 9 months ago

mircobabini commented 9 months ago

Fix: Working on the code I noticed that private $option should be $_options instead.

Enhance: The class will never write publicly anymore, except when debugging. It should respect what's defined by debug options, to avoid weird messages to the website users.

Enhance: Also tracing if web service request failed. And marking as _failed for further detection. Useful for developers who don't want to set that VAT number as invalid, if the request failed/timeout/etc.

Enhance: Ensure previous results are cleared when looking up more than one vat number.

mircobabini commented 9 months ago

Use case scenario; once a day going trough all the renewal orders to check if vat number is STILL valid. If not, if the request failed, sending an email to myself to investigate further. Otherwise, sending an email to myself to double-check the order and fix the gross/net/tax values accordingly.


$vat_number = pmprovat_vat_number_for_orders_csv( $order );
if ( ! empty( $vat_number ) ) {
    $vies_ok = get_pmpro_membership_order_meta( $order->id, 'vies_ok', true );

    if ( '' === $vies_ok ) {
        $vies_ok = pmprovat_verify_vat_number( $order->billing->country, $vat_number );
        if ( $vatValidation->isFailed() ) {
            Logger::get()->emailLog( 'cron/' . __FUNCTION__, [
                'message' => "Vies check failed. Double-check $order->code for user_id = $order->user_id. Vat number not valid: {$order->billing->country}$vat_number. In case, wait for next cron run."
            ] );
        } else {
            $vies_ok = intval( $vies_ok );
            update_pmpro_membership_order_meta( $order->id, 'vies_ok', $vies_ok );

            if ( ! $vies_ok ) {
                Logger::get()->emailLog( 'cron/' . __FUNCTION__, [
                    'message' => "Vies not ok. Double-check $order->code for user_id = $order->user_id. Vat number not valid: {$order->billing->country}$vat_number. In case, fix vat tax values."
                ] );
            }
        }
    }
}