get_id()], 'ng-postcode'); } } add_action(VERIFY_ACTION, static function ($order_id): void { $order = wc_get_order($order_id); $code = $order ? order_postcode($order) : null; if ($code === null || !verify_enabled()) { return; } // Level 1 is free and returns no personal data. $found = send(Api::lookup($code, 1), [Api::class, 'decodeLookup']); if ($found instanceof ApiError) { $status = 'unconfirmed'; /* translators: 1: a postcode, 2: an error code */ $note = sprintf(__('Postcode %1$s could not be checked with NIPOST (%2$s).', 'adeniyikayode-nigerian-postcode-for-woocommerce'), $code, $found->code); } elseif ($found['valid']) { $status = 'assigned'; /* translators: %s: a postcode */ $note = sprintf(__('NIPOST confirms postcode %s belongs to a building.', 'adeniyikayode-nigerian-postcode-for-woocommerce'), $code); } else { $status = 'unassigned'; /* translators: %s: a postcode */ $note = sprintf(__('NIPOST has no building for postcode %s. Check the address with the customer.', 'adeniyikayode-nigerian-postcode-for-woocommerce'), $code); } $order->update_meta_data('_ng_postcode_status', $status); $order->add_order_note($note); $order->save(); }); /** The Nigerian postcode an order is delivered to, or billed to when nothing is shipped. */ function order_postcode(\WC_Order $order): ?Postcode { foreach (['shipping', 'billing'] as $address) { if ($order->{"get_{$address}_country"}() === COUNTRY) { $code = Postcode::parse((string) $order->{"get_{$address}_postcode"}()); if ($code instanceof Postcode) { return $code; } } } return null; }