=== Bastehban Connector ===
Contributors: bastehban
Tags: woocommerce, shipping, tracking, tipax, iran
Requires at least: 5.8
Tested up to: 6.6
Requires PHP: 7.4
WC requires at least: 6.0
Stable tag: 1.2.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Sends WooCommerce orders to the Bastehban shipping hub and shows the tracking code it sends back to your customers.

== Description ==

Bastehban is a central shipping hub that runs in a separate panel. This plugin connects a WooCommerce shop to it:

* When an order reaches one of the chosen statuses (default: Processing) it is sent to Bastehban.
* Bastehban registers the parcel with the carrier and calls the shop back with the carrier and the tracking code.
* The tracking code is shown to the customer on the order page, in the e-mails and as a customer order note.
* Cancelling or refunding a sent order asks Bastehban to cancel it as well.

No external libraries, no JavaScript build. Compatible with WooCommerce High-Performance Order Storage (HPOS).

= Requirements =

* WordPress 5.8 or newer
* WooCommerce 6.0 or newer
* PHP 7.4 or newer
* An API key and a callback secret from the website page of your Bastehban panel

== Installation ==

1. Plugins > Add New > Upload Plugin: upload `bastehban-connector.zip` and activate it.
2. Go to WooCommerce > Bastehban.
3. Fill in the API address (already filled in when the plugin was downloaded from your Bastehban panel — for basteban.com `https://basteban.com/panel/api/bastehban/v1`, without a trailing slash), the API key (`bbk_...`) and the callback secret (`bbw_...`). Save.
4. Click "Test connection". The website id (`bbs_...`) is filled in from the answer.
5. Copy the "Callback address" shown on the page (`https://your-shop/wp-json/bastehban/v1/callback`) into the website page in the Bastehban panel.
6. Click "Test callback" to check that Bastehban can reach the shop.

== How it works ==

= Sending =

When an order changes to one of the configured statuses and has not been sent yet, it is sent to `POST /orders`. With Action Scheduler (part of WooCommerce) the call runs in the background, outside the checkout request. The order number is the `external_id`; Bastehban never creates the same order twice, so a retry is always safe.

The recipient is the shipping address (or the billing address when there is none); the mobile is the billing phone. The weight is the sum of the product weights. Carrier and service codes are optional: empty means the website's default service in Bastehban. A shipping method can have its own codes ("Carrier per shipping method"), e.g. the shop's own Post.

= Parcels the shop sends itself =

When Bastehban answers that the order's service is one the shop sends itself (`service.self_ship`, e.g. the shop's Post), the Bastehban box of the order shows a "Tracking code" field. Type the code and click "Update": it is sent to `POST /orders/{id}/tracking`, stored on the order and shown to the customer like any other tracking code. A refusal (e.g. the order is not approved yet) becomes an order note. The sender is taken from your contract in Bastehban unless you fill in the sender fields.

= Errors and retries =

* Validation errors, a wrong key or a disabled website are permanent: the message is stored on the order, an order note is added and the "Bastehban" column of the orders list shows "Error". Fix the order and use the order action "Send to Bastehban again".
* Network errors, "too many requests" and server errors are temporary: an hourly WP-Cron job retries up to 6 attempts in total (20 orders per run).
* The order actions "Send to Bastehban again" and "Refresh Bastehban status" are on the order edit screen.

= Callbacks =

Bastehban posts to `/wp-json/bastehban/v1/callback`. Every call is signed (`X-Bastehban-Signature: t=...,v1=...`, HMAC-SHA256 of `t.body` with the callback secret). Unsigned, wrongly signed or older than 5 minutes calls, and calls for another website id, are refused. A repeated event is answered "duplicate" and ignored. The order is found only by the order number the shop sent.

On a tracking event the carrier, tracking code, tracking link and shipment status are stored on the order and the customer gets one order note with the tracking code (never a second one for the same code). Optionally the order status is changed (setting "Order status after the tracking code arrives"). A rejected order gets a private note with the reason.

Logs: WooCommerce > Status > Logs, source "bastehban". The API key and the secret are never logged.

== Frequently Asked Questions ==

= Can an order be sent twice? =

No. The plugin sends an order only when it has no Bastehban id, and Bastehban recognises the order number: sending it again returns the same order ("duplicate").

= Which currency is sent? =

Bastehban works in whole rials. Shops in Iranian rial (IRR) are sent as is, shops in toman (IRT) are multiplied by 10. For any other currency, set "Rials per one unit of the store currency"; with 0, no amounts are sent at all (including the COD amount).

= What about cash on delivery? =

When the setting is on and the order was paid with WooCommerce's "Cash on delivery" method, the order total is sent as the COD amount; otherwise the COD amount is 0.

= Where does the customer see the tracking code? =

On the order details page of "My account", in the order e-mails sent after the code arrived, and as a customer order note (which WooCommerce e-mails to the customer).

= What happens when I uninstall the plugin? =

The settings are deleted. The Bastehban data stored on the orders (ref, tracking code, ...) is kept.

== Changelog ==

= 1.2.0 =
* Carrier per shipping method: each WooCommerce shipping method (e.g. "Post") can be sent with its own carrier / service code (settings page; filter bastehban_wc_carrier_service).
* Parcels the shop sends itself (a Bastehban service without a carrier's file, e.g. the shop's Post): the tracking code is typed in the Bastehban box of the order and sent to Bastehban on "Update" (POST /orders/{id}/tracking); the customer gets the usual note with the code.

= 1.1.0 =
* Packages: the list of packages and prices Bastehban set for the shop (settings page), a default package and a package per order (Bastehban box on the order page); sent as "package".
* The estimated and final shipping price on the order; a note when Bastehban reports a different final price (callback order.price_changed, action bastehban_wc_price_changed).

= 1.0.0 =
* First version: sending orders, retries, cancel, order actions, meta box, orders list column, signed callbacks with tracking codes, customer view and e-mails.

== راهنمای فارسی ==

این افزونه فروشگاه ووکامرس را به «بسته‌بان» وصل می‌کند.

* نصب: فایل zip را در «افزونه‌ها > افزودن > بارگذاری افزونه» بارگذاری و فعال کنید.
* در «ووکامرس > Bastehban» نشانی API، کلید API و رمز بازگشت (callback secret) را وارد و ذخیره کنید، سپس «Test connection» را بزنید.
* «نشانی بازگشت» نمایش داده‌شده را در صفحهٔ وب‌سایت در پنل بسته‌بان وارد کنید و «Test callback» را بزنید.
* سفارش‌ها با رسیدن به وضعیت انتخاب‌شده (پیش‌فرض: در حال انجام) خودکار ارسال می‌شوند و ارسال دوباره، سفارش تکراری نمی‌سازد.
* کد رهگیری پس از ثبت نزد شرکت حمل، در صفحهٔ سفارش مشتری، ایمیل‌ها و یادداشت سفارش نمایش داده می‌شود.
* مبالغ به ریال فرستاده می‌شوند؛ فروشگاه تومانی (IRT) خودکار ×۱۰ می‌شود.
* بسته‌ها: در صفحهٔ تنظیمات، فهرست بسته‌ها و قیمت‌هایی که بسته‌بان برای فروشگاه شما گذاشته دیده می‌شود؛ یک بستهٔ پیش‌فرض انتخاب کنید. در صفحهٔ هر سفارش (کادر Bastehban) می‌توانید پیش از ارسال بستهٔ دیگری انتخاب کنید.
* قیمت حمل: قیمت تخمینی روی سفارش ثبت می‌شود؛ اگر بسته‌بان قیمت نهایی متفاوتی ثبت کند، یادداشتی روی سفارش اضافه می‌شود.
* شرکت حمل برای هر روش ارسال (۱.۲): در تنظیمات، جلوی هر روش ارسال ووکامرس (مثلاً «پست») کد شرکت حمل و کد سرویس را بنویسید (مثلاً post و shop)؛ سفارش‌هایی که مشتری آن روش را انتخاب کرده با همان سرویس فرستاده می‌شوند.
* پستی که خود فروشگاه می‌فرستد (۱.۲): در کادر Bastehban همان سفارش، کد رهگیری پست را بنویسید و «به‌روزرسانی» را بزنید؛ کد به بسته‌بان می‌رود و به مشتری نشان داده می‌شود. اگر بسته‌بان نپذیرد، دلیلش یادداشت سفارش می‌شود.
