=== Netmasha Attribution ===
Contributors: mahmoudnaksha
Tags: woocommerce, attribution, refunds, orders, tracking
Requires at least: 6.3
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.4.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Connect your store to Netmasha with one sign-in and one approval. Keeps the click id through checkout, reports paid orders, cancellations and refunds in real time, and lets shoppers spend their Netmasha points as a single-use coupon.

== Description ==

Netmasha Attribution links sales on your WooCommerce store to the Netmasha shoppers who sent them.

* **Connects in one click.** After you install and activate the plugin, press **Connect to Netmasha**, sign in at Netmasha and approve WooCommerce's own read-only access screen. You land back in WooCommerce, connected. Nothing is copied or pasted: no store id, secret or API key.

* Keeps Netmasha's click id (`vm_click`) from the landing page through the cart to the order and each order line.
* Works with both the classic checkout and the WooCommerce block checkout.
* Compatible with High-Performance Order Storage (HPOS).
* Keeps the click id even when a page cache serves the landing page (a tiny script stores it in a cookie in the browser).
* Optionally sends paid orders, cancellations and refunds to Netmasha as they happen, signed with your webhook secret, so shoppers' points and store commission are up to date immediately.

* Lets shoppers spend their Netmasha points at your store. Installing this plugin means Netmasha may create single-use, fixed-amount coupons (codes that start with `NM`) in your shop through a signed route, as agreed in your Netmasha merchant terms. There is no setting to switch this on or off. A Netmasha coupon can be used once, expires at the exact time Netmasha set, never combines with other coupons, and is removed by Netmasha if it is not used.

Requires WooCommerce and a Netmasha store account.

Step-by-step help: https://business.netmasha.com/docs/connect-woocommerce

== Installation ==

1. Upload the plugin ZIP in Plugins > Add New > Upload Plugin, or extract it into `wp-content/plugins/`.
2. Activate it.
3. Press **Connect to Netmasha** in the notice at the top of your dashboard (or in WooCommerce > Settings > Netmasha), sign in at Netmasha, and approve the read-only access that WooCommerce shows. Press **Disconnect** in the same place to stop.

== Frequently Asked Questions ==

= Does it change my checkout? =
No. It only adds hidden order data (`_netmasha_click`).

= What does Connect do exactly? =
The plugin opens Netmasha with your site address and a one-time check value. After you sign in, WooCommerce asks you to approve **read-only** access (orders and products) for an app called Netmasha. Netmasha then gives the plugin a store id and a secret in return for a one-time code. The secret is stored with autoload off and never shown. Disconnect removes the secret and deletes the read-only key from your WooCommerce settings (Advanced > REST API).

= Can I still enter the details by hand? =
Only for support: add `define( 'NETMASHA_MANUAL_SETUP', true );` to wp-config.php and the manual fields appear. Merchants never need it.

= Which coupons can Netmasha create? =
Only single-use fixed-amount coupons whose code starts with `NM`, signed with your webhook secret. Netmasha cannot read, edit or delete any other coupon, product, order or customer. The request is rejected if the signature is wrong, if it is more than five minutes old, or if it was already used. If WooCommerce coupons are switched off in your shop, the plugin tells Netmasha so, and shoppers are not offered a coupon.

= What if Netmasha is unreachable when I refund? =
The refund still completes. The failure is written to the WooCommerce logs (source `netmasha-attribution`), and Netmasha also reconciles orders and refunds periodically.

= Is data removed on uninstall? =
The plugin's settings are deleted. Order data is kept.

== External services ==

When you press Connect to Netmasha, this plugin sends your site address and a one-time check value (the hash of a random value that stays on your site) to the Netmasha portal (https://store.netmasha.com) by redirecting your browser there, and later sends the one-time code it receives back, with that random value, to the Netmasha API (https://api.netmasha.com/api/connect/exchange). Netmasha answers with your store id, your store name and a secret. When you press Disconnect, it sends a signed disconnect request to the same API.

When this store is connected, this plugin sends data about Netmasha-attributed orders to the Netmasha API (https://api.netmasha.com), signed with your webhook secret: (1) when an order becomes processing or completed: order id and number, status, total, tax, shipping, currency, payment time, the Netmasha click id and the quantity and subtotal of each line; (2) when an order is cancelled: order id, currency and cancellation time; (3) when an order is refunded: refund id, order number, refund amount, currency and refund time. If a Netmasha points coupon was used on the order, the paid, cancelled and refund reports also list that coupon's code and discount amount (only coupons starting with NM; none of your other coupons). No customer name, address, email or phone is sent. Nothing is sent for orders that have neither a Netmasha click id nor a Netmasha coupon, and nothing is sent when the store is disconnected.

This plugin also receives requests from Netmasha (https://api.netmasha.com) at the REST route `/wp-json/netmasha/v1/vouchers`. Each request is signed with your webhook secret, must be less than five minutes old and can be used only once. The requests ask the plugin to (1) report whether this shop can accept coupons and the plugin version, (2) create a single-use coupon with a code starting with NM, an amount, a minimum order and an expiry time, or (3) remove such a coupon, and the plugin answers whether it was removed or had already been used. The coupons are ordinary WooCommerce data, no code is downloaded or run, and nothing about your customers is received.

Service provided by Netmasha: [Terms of service](https://netmasha.com/terms) | [Privacy policy](https://netmasha.com/privacy)

== Privacy ==

The plugin stores an opaque click id in a 30-day cookie and in order and order-line meta. It contains no personal data. It sends order totals, cancellations, refund amounts and the code and discount of any Netmasha points coupon to api.netmasha.com only when configured, as described under External services. The coupons Netmasha creates contain no personal data.

== Changelog ==

= 1.4.0 =
* One-click **Connect to Netmasha**: sign in at Netmasha, approve WooCommerce's own read-only access screen, done. No store id, secret or API key to copy.
* **Disconnect** button: tells Netmasha, forgets the secret and deletes the read-only API key on this site. Netmasha can also disconnect from its side.
* The Netmasha settings tab is now a status page ("Connected to ..."). Manual entry is only available to support through `NETMASHA_MANUAL_SETUP`.
* A notice on the dashboard, Plugins and WooCommerce screens until the store is connected.
* Uninstall also removes the read-only API key.

= 1.3.0 =
* Netmasha points can be spent at the store: a signed REST route (`/wp-json/netmasha/v1/vouchers`) lets Netmasha create and remove single-use `NM` coupons. Requests are verified with the webhook secret, a five-minute window and a one-time nonce.
* Netmasha coupons are refused from their exact expiry moment, and a coupon whose paid order is cancelled cannot be used again.
* Paid, cancelled and refunded reports include the Netmasha coupon code and discount, and are also sent for orders that used a Netmasha coupon without a click id.

= 1.1.0 =
* Block checkout (Store API) orders are now stamped.
* New WooCommerce > Settings > Netmasha page.
* Real-time signed refund reports.
* Declares HPOS and cart/checkout blocks compatibility.

= 1.0.0 =
* Initial release: click id on cart, order and order lines (classic checkout).
