=== PayCard WooCommerce ===
Contributors: paycard
Tags: paycard, topup, codes, gift cards, game credits, fulfillment
Requires at least: 6.0
Tested up to: 6.6
Requires PHP: 7.4
WC requires at least: 7.0
WC tested up to: 9.0
Stable tag: 1.3.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Connect WooCommerce to PayCard: map products, auto-submit paid orders for direct top-up or code delivery, and sync status back automatically.

== Description ==

**PayCard WooCommerce** connects your WooCommerce store to your PayCard merchant account:

* **Product mapping** — link any WooCommerce product to a PayCard product (live list with your tier price).
* **Direct top-up** — customer enters their Player/Account ID (and server when required by the brand) on the product page; the order is submitted to PayCard automatically once paid.
* **Code delivery** — delivered codes are attached to the order, shown in My Account and included in the order email.
* **Status sync** — polling via WP-Cron keeps order statuses in sync; optional signed webhook for instant updates.
* **Safe retries** — transient failures retry automatically with backoff; hard failures stop immediately.
* **Duplicate protection** — a PayCard order is never created twice for the same WooCommerce order (local idempotency, since the API has no idempotency key).
* **HPOS compatible** and built for modern WooCommerce.

== Installation ==

1. Upload the `paycard-woocommerce` folder to `/wp-content/plugins/` (or install the ZIP via Plugins → Add New → Upload).
2. Activate **PayCard WooCommerce** (WooCommerce 7.0+ required).
3. Go to **WooCommerce → Settings → PayCard**.
4. Paste your **API token** (create one in your PayCard dashboard → API section), then click **Test API Connection**.

== Configuration ==

**Connection**
* *Enable/Disable* — master switch.
* *API URL* — defaults to `https://partners.pay-card.shop/api/v1`.
* *API Token* — your Bearer token (never displayed in full, never logged).
* *Webhook Secret (optional)* — same secret you set when registering the webhook in PayCard.

**General**
* *Automatic Order Processing* — submit orders automatically when they reach the trigger status.
* *Automatic Status Sync* — poll PayCard for status updates every 2 minutes.
* *Debug Mode* — verbose API logging (secrets/codes are always redacted).
* *Maximum Retry Attempts* — 1–10 (default 5, exponential backoff).

**Order Settings**
* *Trigger status* — when to submit (default: Processing).
* *Status on success* — default: Completed.
* *Status on failure* — optional.
* *Status on refund* — default: Refunded.

== Product Mapping ==

Edit any product → **PayCard** tab:

1. Enable **PayCard**.
2. Choose the **PayCard Product** (the list loads live from your account with your tier price and availability).
3. Save. For top-up products, the required customer fields (Player ID, Server…) appear automatically on the product page — driven by the PayCard brand specification.

== Order Flow ==

1. Customer buys a mapped product (and fills identifier fields for top-ups).
2. When the order reaches the trigger status, it is submitted to PayCard once (idempotent).
3. The PayCard order ID is stored on the order (PayCard box in the order editor).
4. Status is synced by polling (and webhook if configured).
5. On success the order moves to your chosen status and delivered codes appear in the order, My Account, and the email.

== Webhooks ==

Polling is the primary sync (PayCard sends webhooks only for redeemable orders). To also receive instant updates:

1. In your PayCard dashboard, register a webhook for the event `order.status_updated` pointing to:
   `https://your-store.com/wp-json/paycard/v1/webhook`
2. Copy the webhook secret into **WooCommerce → Settings → PayCard → Webhook Secret**.

Signatures are verified with HMAC-SHA256; unsigned or mis-signed requests are rejected.

== Troubleshooting ==

* **Connection test fails** — check the token and that your site can reach the API URL (HTTPS).
* **Order not submitted** — verify the product is mapped, the order reached the trigger status, and the PayCard box/notes for the error.
* **Retries exhausted** — the order note shows the last error; use **Send to PayCard** after fixing the cause.
* **Logs** — WooCommerce → Status → Logs → source `paycard-woocommerce`.

== Security ==

* The API token and webhook secret are never displayed in full and never written to logs.
* Delivered codes are never logged.
* Webhook requests must carry a valid HMAC-SHA256 signature.
* All admin actions use nonces and capability checks.

== Frequently Asked Questions ==

= Does the plugin change my WooCommerce prices? =
No. Prices come from your PayCard tier for reference only. WooCommerce prices are never modified automatically.

= Can customers order the same product with different player IDs? =
Yes — each identifier combination is a separate cart item.

= What happens if PayCard is temporarily down? =
Transient failures retry automatically with backoff (up to your configured maximum). No duplicate orders are ever created.

= Is HPOS supported? =
Yes — the plugin uses WooCommerce CRUD APIs only and declares HPOS compatibility.

== Product Import (Bulk) ==

Go to **WooCommerce → استيراد PayCard**:

1. The live PayCard catalog loads with your tier cost, type, and required customer fields per product.
2. Set your **markup %** (default 10%) and the product status (draft recommended).
3. Select products and click **استيراد المحدد** — each product is created with:
   the Arabic name, price = cost + markup, the PayCard mapping, and the brand's customer-field config.
4. Review drafts and publish.

**Per-product field control:** in the product's PayCard tab you can enable/disable each customer field and set it required or optional, and override the service type.

== Arabic & Live Log ==

* Full **Arabic translation** (`languages/paycard-woocommerce-ar.mo`) — loads automatically with your site language.
* **السجل المباشر** in the PayCard dashboard shows the last 50 events in Arabic and auto-refreshes every 30 seconds.
* API calls are pinned to **IPv4** for reliable connectivity.

== Changelog ==

= 1.3.0 =
* حماية من الشراء المكرر على خادم PayCard: كل طلب يُرسل برقم مرجعي ثابت (Idempotency-Key)؛ إعادة المحاولة بعد انقطاع الاتصال ترجع الطلب الأول بلا خصم ثان.
* مرجع لكل مجموعة في الطلب المقسم (معرف لاعب لكل مجموعة)، ومفتاح للموقع يمنع التعارض بين متجرين لنفس التاجر.
* مزامنة المخزون عبر PayCard API V2 كل 15 دقيقة: المنتج النافد يصبح «نفد من المخزون» ويعود عند توفره (لا يمس حالة المخزون التي يضبطها التاجر يدويا). خيار في الإعدادات، مفعل افتراضيا.
* متوافق مع WooCommerce 9.4.

= 1.1.0 =
* Bulk product import (markup, draft review, brand field config included).
* Per-product customer-field control (enable/disable/required/optional) + service-type override.
* Full Arabic translation + Arabic live log with auto-refresh.
* IPv4 pinning for all API calls.

= 1.0.0 =
* Initial release: product mapping, direct top-up, code delivery, polling sync, signed webhook, retry manager, idempotency, HPOS support.

= 1.1.2 =
* Review pass: fixed settings persistence (array option now saved correctly), webhook code attachment (final states now fully re-read before applying), undefined variable guard, IPv4 hook dedupe, numeric meta comparison.

= 1.2.0 =
* Retry executor: scheduled retries now actually run (due attempts processed every 2 minutes; clear exhausted-retry notice).
* Async submission: orders are sent to PayCard in the background (Action Scheduler / WP-Cron) — checkout is never blocked by the API call.
* Multi-identifier orders: one PayCard order per player/account group; collective status sync with codes attached per group.
* auto_status_sync setting is now honored.
* Dashboard uses lightweight aggregate counts (scales to thousands of orders).
* Token field shows a "saved" state without revealing the token; WooCommerce's junk array-style options are cleaned automatically.
* Missing PayCard orders (404) stop polling with a clear note.
* One-time note when an order has no mapped items.
