=== EasySecure Subscriptions ===
Contributors: imminentsoftware
Tags: woocommerce, subscriptions, recurring payments, stripe, membership
Requires at least: 6.0
Tested up to: 7.1
Requires PHP: 8.0
Stable tag: 1.2.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

WooCommerce subscriptions with a built-in Stripe gateway: automatic renewals, failed-payment retries, upgrades and easy cancellation.

== Description ==

EasySecure Subscriptions turns any WooCommerce product into a subscription. Set the billing interval on the product, connect your Stripe account with your test and live keys, and the plugin charges your customers automatically every cycle.

It is built to be safe with money. A renewal is only marked paid when Stripe confirms that the full amount was actually captured. Failed cards are retried and get a grace period before access is removed. Customers keep what they paid for when they cancel, and they can upgrade to a higher plan with a credit for the unused time.

No extra database tables, no external libraries, and no other subscription or Stripe plugin is required.

= 💳 Built-in Stripe gateway (new in 1.2.0) =

* Connect Stripe from the WordPress admin: WooCommerce → Settings → Payments → **EasySecure Stripe**.
* Separate fields for **test keys** and **live keys** (publishable key, secret key and optional webhook signing secret), and a single "Test mode" checkbox to switch between them.
* Keys are checked with Stripe when you save. The settings page shows the current mode, the connected Stripe account and the webhook URL. Wrong keys (for example a test key in a live field) are rejected with a clear message.
* Card form powered by Stripe Elements, so card numbers never touch your server.
* Works on the **block checkout**, the **classic checkout** and the **"Pay now"** page.
* **3D Secure** (SCA) support at checkout.
* **Saved cards:** for subscriptions the card is always saved for automatic renewals. For other products, customers can choose to save it.
* **Refunds** directly from the WooCommerce order screen, full or partial.
* Optional **signed webhook** that completes payments if a customer closes the browser during 3D Secure.
* Test and live data are kept apart. Saved cards and Stripe customers belong to one mode and one Stripe account, so a test card is never charged in live mode.
* At checkout the option shows as "Credit / debit card **EasySecure Subscriptions**", with a test-mode hint while test mode is on.

Already using the WooCommerce Stripe Gateway plugin? Subscriptions bought with it keep renewing through it, so you can switch to the built-in gateway without breaking existing customers.

= 🔁 Renewals you can trust =

* Automatic renewals on a daily, weekly, monthly or yearly interval (for example every 3 months).
* Optional subscription length: the subscription ends after a set number of periods.
* Renewals are charged off-session with the customer's saved card.
* A renewal order is only marked paid when Stripe reports "succeeded" **and** the captured amount and currency match the order.
* Each renewal attempt has its own idempotency key, and overlapping cron runs are locked, so a customer is never charged twice for one cycle.
* Correct month handling: 31 January + 1 month = 28/29 February.

= ⚠️ Failed payments, retries and grace period =

* A declined renewal never moves the billing date forward.
* The subscription becomes **Past due**. The customer keeps access for a **7-day grace period**.
* The card is retried automatically on day **1, 3 and 5**.
* The customer gets an email with a **Pay now** link to pay with another card. That card then becomes the one used for later renewals.
* If the renewal is still unpaid when the grace period ends, the subscription is **Suspended** and access is removed. Paying the renewal reactivates it.
* Cards that need 3D Secure during a renewal are not marked paid; the customer is asked to pay the renewal themselves.

= ⬆️ Plans, upgrades and one plan per customer =

* Give products a **Plan Group** and a **Plan Level** (for example Silver = 1, Gold = 2 in the group "membership").
* **One plan per group per customer.** Buying the same plan twice, or a lower plan of the same group, is blocked. Subscription checkout requires an account, so this cannot be bypassed with guest checkout.
* **Upgrades with proration:** customers get an "Upgrade to Gold" button in My Account. At checkout the unused part of the current period is credited (shown as "Credit for unused time on Silver"). Once the upgrade is paid, the old plan stops billing and is marked **Switched**.
* Downgrades: the customer cancels the higher plan, keeps it until the end of the paid period, and then buys the lower plan.

= ✋ Cancellation that respects the paid period =

* Customers cancel from **My Account**. Nothing is charged again, but access continues until the end of the period already paid (**Pending cancel**).
* Until then the customer can **Resume** the subscription with one click.
* Shop managers can choose **Cancel at period end** or **Cancel now** on the Subscriptions screen.

= 🔒 Restricting content to subscribers =

* Shortcode: `[easysecure_restricted products="12,34"]Members only content[/easysecure_restricted]`
* PHP: `easysecure_user_has_access( $user_id, $product_ids )`
* Access counts as active while the subscription is Active, Past due (grace period) or Pending cancel.

= 📧 Email notifications =

* Subscription activated
* Renewal reminder, about one day before the next charge
* Renewal payment failed, with a Pay now link
* Subscription suspended
* Cancellation confirmed, showing the date access ends
* Subscription ended or expired

= 🛠 Admin =

* Subscriptions screen with status, next payment date, next retry and grace period end for every subscription.
* Paginated list with cancel, resume and cancel-now actions.
* Subscription statuses: Active, Past due, Pending cancel, Suspended, Cancelled, Expired, Switched.
* Compatible with WooCommerce High-Performance Order Storage (HPOS).
* Subscription products are locked to quantity 1.

== External services ==

This plugin connects to Stripe to take card payments, process renewals and issue refunds. Stripe is operated by Stripe, Inc. Nothing is sent to Stripe until you enter your Stripe API keys and enable the EasySecure Stripe payment method.

= Stripe API (api.stripe.com) =
Used to verify your API keys, create Stripe customers, create and check payment intents, and create refunds.

Data sent: the order amount and currency, the order ID and order key, your site address, the customer's WordPress user ID, name and email address, the Stripe customer ID and the saved payment method ID. Refund requests also send the refund amount and reason.

When: when you save the API keys, when a customer pays at checkout or on the "Pay now" page, when a customer returns from 3D Secure, when a renewal is charged, and when you refund an order.

= Stripe.js (js.stripe.com) =
Loaded on the checkout and "Pay now" pages to show the secure card field (Stripe Elements).

Data sent: the card details and the customer's billing name, email, phone number and address are sent directly from the customer's browser to Stripe. Card details never reach your server.

When: whenever a customer opens the checkout or "Pay now" page while EasySecure Stripe is enabled.

= Stripe webhooks (optional) =
If you add a webhook in your Stripe dashboard, Stripe sends payment status events to your site at `/wc-api/easysecure_stripe_webhook/`. The plugin does not send any data in this step.

Stripe Terms of Service: https://stripe.com/legal
Stripe Privacy Policy: https://stripe.com/privacy

== Installation ==

1. Upload the `easysecure-subscriptions` folder to `/wp-content/plugins/`, or install the ZIP from Plugins → Add New → Upload Plugin.
2. Activate the plugin. WooCommerce must be active.
3. **Connect Stripe:** go to WooCommerce → Settings → Payments → EasySecure Stripe (or click "Stripe settings" on the Plugins screen).
   * Tick **Enable EasySecure Stripe**.
   * Paste your **test** publishable and secret keys from Stripe Dashboard → Developers → API keys (with Test mode on in Stripe).
   * Paste your **live** keys (with Test mode off in Stripe).
   * Keep **Test mode** ticked while you test, and untick it to take real payments. Live payments need HTTPS.
   * Save. The status box should show "Connected to <your account>".
4. **Create a subscription product:** edit a product, tick the subscription option, and set the billing interval. Optionally set the subscription length, Plan Group and Plan Level.
5. Place a test order with card `4242 4242 4242 4242`, any future expiry date and any CVC.

= Optional: Stripe webhook =

In Stripe → Developers → Webhooks, add an endpoint with the URL shown on the EasySecure Stripe settings page (it ends in `/wc-api/easysecure_stripe_webhook/`). Select the events `payment_intent.succeeded` and `payment_intent.payment_failed`, and paste the signing secret (`whsec_…`) into the test or live webhook field.

= Stripe test cards =

* `4242 4242 4242 4242`: payment succeeds
* `4000 0025 0000 3155`: asks for 3D Secure at checkout; renewals succeed afterwards
* `4000 0000 0000 0002`: card declined

== Frequently Asked Questions ==

= Do I need the WooCommerce Stripe Gateway plugin? =
No. EasySecure Subscriptions has its own Stripe gateway. If you already use the WooCommerce Stripe Gateway plugin, subscriptions bought with it keep renewing through it. You can turn off its card option at checkout, but keep the plugin active and its keys saved until those subscriptions have ended or moved to the built-in gateway.

= Where do I enter my Stripe test and live keys? =
WooCommerce → Settings → Payments → EasySecure Stripe. There are separate fields for the test keys and the live keys, and a "Test mode" checkbox decides which pair is used.

= How do I go live? =
Enter your live keys, untick "Test mode" and save. Check that the status box says "Connected" for the live connection. Your site must use HTTPS. Cards saved in test mode are not used in live mode.

= Is my customers' card data safe? =
Yes. Card details are entered in Stripe's secure field and sent directly to Stripe. Your site only stores Stripe's reference to the card (a payment method ID), never the card number.

= Do I need a webhook? =
No. Card payments and renewals work without it. The webhook only helps when a customer closes the browser in the middle of 3D Secure.

= What happens when a renewal payment fails? =
The subscription becomes "Past due" and the customer keeps access for 7 days. The card is retried on day 1, 3 and 5, and the customer receives an email with a Pay now link. If nothing is paid by the end of the grace period, the subscription is suspended and access is removed. Paying the renewal reactivates it. Developers can change the timing with the `easysecure_grace_period_days` and `easysecure_renewal_retry_days` filters.

= What happens when a customer cancels? =
Nothing is charged again, and access continues until the end of the paid period. Until then the customer can resume. Shop managers can also cancel immediately.

= Can customers upgrade their plan? =
Yes. Give the plans the same Plan Group and a Plan Level. The customer sees an "Upgrade to …" button in My Account and gets a credit for the unused time of the current plan. The old plan stops billing once the upgrade is paid.

= Can a customer buy the same plan twice? =
No. A customer can have one plan per Plan Group. Subscription checkout requires an account.

= Can I refund a payment? =
Yes. Use the normal WooCommerce "Refund" button on the order. Payments made with EasySecure Stripe are refunded in Stripe automatically, in full or in part.

= Which other payment gateways work? =
Automatic renewals are built in for EasySecure Stripe and the WooCommerce Stripe Gateway plugin. With other gateways (for example bank transfer), the customer receives the renewal order and pays it manually. Developers can add automatic charging for another gateway with the `easysecure_process_renewal_payment` filter.

= Can I offer free trials? =
Not yet.

= Does it work with variable products? =
Subscriptions are configured on simple products.

= Does the plugin create custom database tables? =
No. All data is stored in WooCommerce orders, order item meta and WordPress options.

= Will it conflict with WooCommerce Subscriptions? =
Running both is not recommended. Use one or the other.

== Developers ==

Actions:

* `easysecure_subscription_activated`, `easysecure_subscription_reactivated`
* `easysecure_subscription_renewed`, `easysecure_renewal_payment_failed`
* `easysecure_subscription_past_due`, `easysecure_subscription_suspended`
* `easysecure_subscription_cancellation_scheduled`, `easysecure_subscription_cancelled`, `easysecure_subscription_expired`
* `easysecure_subscription_switched`
* `easysecure_subscription_access_granted`, `easysecure_subscription_access_revoked`
* `easysecure_subscription_status_updated`

Filters:

* `easysecure_grace_period_days` (default 7)
* `easysecure_renewal_retry_days` (default `array( 1, 2, 2 )`, days after the previous attempt)
* `easysecure_process_renewal_payment`: charge renewals for another gateway. Return `status` success only after the gateway confirmed the full amount.

Functions:

* `easysecure_user_has_access( $user_id, $product_ids = array() )`
* `easysecure_get_user_subscriptions( $user_id, $statuses = null )`

== Screenshots ==

1. EasySecure Stripe settings with test and live keys and the connection status
2. Card form at checkout
3. Subscription settings in the product data box (interval, length, Plan Group, Plan Level)
4. Subscription status, next payment date and Cancel button for the customer in My Account
5. Admin Subscriptions screen
6. Payment failed email with a Pay now link

== Changelog ==

= 1.2.0 =
* New: built-in "EasySecure Stripe" card gateway with test and live API keys in WooCommerce → Settings → Payments. No other Stripe plugin is required.
* New: the keys are checked with Stripe on save, and the settings page shows the connected account, the current mode and the webhook URL. Wrong key formats are rejected.
* New: works with the block checkout, the classic checkout and the "Pay now" page for failed renewals. Includes 3D Secure, saved cards and refunds from the order screen.
* New: optional signed webhook for payments finished after 3D Secure.
* New: "EasySecure Subscriptions" label next to the card option at checkout, and the renewal notice is shown above the card field.
* Saved cards and Stripe customers are kept separately for test and live mode, so switching modes never charges a card from the other mode.
* A renewal paid with another card or gateway makes that card the one charged for later renewals.

= 1.1.0 =
* Fix: Stripe renewals are charged off-session through a PaymentIntent. A renewal is only marked paid when Stripe reports "succeeded" and the captured amount equals the order total. Before this fix, renewals could be marked paid while 0 was collected.
* Fix: failed renewals no longer move the next payment date forward. New "past due" status with automatic retries (day 1, 3, 5) and a 7-day grace period, then "suspended", which removes access. Paying the renewal reactivates the subscription.
* Fix: customer cancellation now ends access at the end of the paid period ("pending cancel"), and the customer can resume until then. Admins can cancel at period end or immediately.
* Fix: one plan per account. Buying the same plan again, or a lower tier of the same plan group, is blocked. Subscription checkout requires an account.
* Fix: subscriptions with a length no longer charge one extra renewal on the expiry date.
* New: upgrades between plans of the same group, with a prorated credit for unused time. The old plan stops billing once the upgrade is paid.
* New: "Plan Group" and "Plan Level" product fields; Pay now / Upgrade / Resume buttons in My Account.
* New: payment-failed, suspended and subscription-ended emails.
* New: `easysecure_user_has_access()`, the `[easysecure_restricted]` shortcode, and access granted/revoked actions.
* New: `easysecure_process_renewal_payment` filter for gateways other than Stripe. Those gateways now fall back to manual payment instead of being marked paid without a verified charge.
* Overlapping cron runs are locked, and each renewal attempt sends its own Stripe idempotency key, so a renewal cannot be charged twice.
* Fix: replaced the deprecated woocommerce_payment_token_created hook with woocommerce_new_payment_token.

= 1.0.1 =
* Fix: subscriptions bought after the first cron run are now registered and renewed
* Fix: renewal reminder and expiry emails are now actually sent; reminder resets each billing cycle
* Fix: admin cancellation no longer sends two emails
* Fix: renewal orders no longer create new subscriptions of their own
* Fix: subscriptions now start for gateways that do not call payment_complete (e.g. bank transfer)
* Fix: month/year renewals no longer overflow (31 Jan + 1 month = 28/29 Feb)
* New: subscription length (expiry) fields on the product screen
* New: declares WooCommerce HPOS compatibility; admin order column works with HPOS
* Security: frontend cancellation requires login and order ownership (or shop manager)
* Admin subscriptions list is paginated; removed orphaned daily cron; added uninstall cleanup
* Tested on WordPress 7.1, WooCommerce 11.3, PHP 8.4 (static compatibility check for PHP 8.0-8.5)

= 1.0.0 =
* Initial public release
* Product-level subscription metadata
* Automatic renewal cron system
* Secure token-based renewals
* Email notifications
* Admin subscription controls

== Upgrade Notice ==

= 1.2.0 =
Adds a built-in Stripe gateway with test and live keys. Existing subscriptions made with the WooCommerce Stripe Gateway plugin keep renewing through that plugin.

= 1.1.0 =
Important payment fixes: renewals are only marked paid after Stripe confirms the charge, failed payments get retries and a grace period, and cancellation keeps access until the end of the paid period.

= 1.0.1 =
Important fix: new subscriptions are now renewed by the cron, and reminder emails are sent.

= 1.0.0 =
Initial release of EasySecure Subscriptions.
