| 1 |
<?php |
| 2 |
/** |
| 3 |
* GiveWP gateway and status translation tables. |
| 4 |
* |
| 5 |
* Centralises the mapping from GiveWP gateway slugs / donation statuses to |
| 6 |
* SureDonation equivalents, plus runtime detection of whether a gateway |
| 7 |
* (and its Pro recurring handler) is currently loaded on this site. |
| 8 |
* |
| 9 |
* @package SureDonation |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SureDonation\Inc\Import\Givewp; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* Status_Map class. |
| 19 |
* |
| 20 |
* All methods are pure and stateless; safe to call statically anywhere. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Status_Map { |
| 25 |
|
| 26 |
/** |
| 27 |
* Map a GiveWP gateway slug to the SureDonation gateway slug we store on the donation row. |
| 28 |
* |
| 29 |
* Slugs that match exactly (stripe / paypal / mollie / razorpay) pass through |
| 30 |
* unchanged. `manual` is renamed to `offline`. PayPal Commerce, PayPal Pro, |
| 31 |
* PayPal Adaptive etc. collapse to `paypal`. Stripe sub-method gateways |
| 32 |
* (stripe_checkout / stripe_ach / stripe_ideal / ...) collapse to `stripe`. |
| 33 |
* Unknown slugs are preserved as-is so historical records keep their |
| 34 |
* provenance even when SureDonation cannot transact through that gateway. |
| 35 |
* |
| 36 |
* @param string $givewp_gateway GiveWP gateway slug. |
| 37 |
* @return string SureDonation gateway slug (may equal the input). |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
public static function map_gateway( $givewp_gateway ) { |
| 41 |
$givewp_gateway = is_string( $givewp_gateway ) ? $givewp_gateway : ''; |
| 42 |
|
| 43 |
$passthrough = [ 'stripe', 'paypal', 'mollie', 'razorpay', 'offline' ]; |
| 44 |
if ( in_array( $givewp_gateway, $passthrough, true ) ) { |
| 45 |
return $givewp_gateway; |
| 46 |
} |
| 47 |
|
| 48 |
$map = [ |
| 49 |
'manual' => 'offline', |
| 50 |
'paypalcommerce' => 'paypal', |
| 51 |
'paypaladaptive' => 'paypal', |
| 52 |
'paypal_pro' => 'paypal', |
| 53 |
'paypal_express' => 'paypal', |
| 54 |
'paypal_standard' => 'paypal', |
| 55 |
'stripe_checkout' => 'stripe', |
| 56 |
'stripe_ach' => 'stripe', |
| 57 |
'stripe_ideal' => 'stripe', |
| 58 |
'stripe_sepa' => 'stripe', |
| 59 |
'stripe_apple_pay' => 'stripe', |
| 60 |
'stripe_google_pay' => 'stripe', |
| 61 |
'stripe_becs' => 'stripe', |
| 62 |
'stripe_bancontact' => 'stripe', |
| 63 |
]; |
| 64 |
|
| 65 |
return isset( $map[ $givewp_gateway ] ) ? $map[ $givewp_gateway ] : $givewp_gateway; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Map a GiveWP donation status (post_status on the give_payment CPT) to |
| 70 |
* the SureDonation `payment_status` value stored on the donations row. |
| 71 |
* |
| 72 |
* @param string $givewp_status GiveWP donation post_status. |
| 73 |
* @return string SureDonation payment_status value. |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
public static function map_donation_status( $givewp_status ) { |
| 77 |
$givewp_status = is_string( $givewp_status ) ? $givewp_status : ''; |
| 78 |
|
| 79 |
$map = [ |
| 80 |
'publish' => 'completed', |
| 81 |
'give_complete' => 'completed', |
| 82 |
'give_pending' => 'pending', |
| 83 |
'pending' => 'pending', |
| 84 |
'give_processing' => 'processing', |
| 85 |
'give_cancelled' => 'cancelled', |
| 86 |
'give_abandoned' => 'cancelled', |
| 87 |
'give_failed' => 'failed', |
| 88 |
'give_revoked' => 'cancelled', |
| 89 |
'give_preapproval' => 'pending', |
| 90 |
'refunded' => 'refunded', |
| 91 |
'give_refunded' => 'refunded', |
| 92 |
]; |
| 93 |
|
| 94 |
return isset( $map[ $givewp_status ] ) ? $map[ $givewp_status ] : 'pending'; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Map a GiveWP subscription status to SureDonation's subscription_status value. |
| 99 |
* |
| 100 |
* @param string $givewp_status GiveWP subscription status. |
| 101 |
* @return string SureDonation subscription_status value. |
| 102 |
* @since 1.0.0 |
| 103 |
*/ |
| 104 |
public static function map_subscription_status( $givewp_status ) { |
| 105 |
$givewp_status = is_string( $givewp_status ) ? $givewp_status : ''; |
| 106 |
|
| 107 |
$map = [ |
| 108 |
'active' => 'active', |
| 109 |
'cancelled' => 'cancelled', |
| 110 |
'expired' => 'expired', |
| 111 |
'completed' => 'completed', |
| 112 |
'pending' => 'pending', |
| 113 |
'failing' => 'past_due', |
| 114 |
'suspended' => 'paused', |
| 115 |
]; |
| 116 |
|
| 117 |
return isset( $map[ $givewp_status ] ) ? $map[ $givewp_status ] : 'pending'; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Check whether a SureDonation gateway is currently loaded on this site. |
| 122 |
* |
| 123 |
* Used by the migration tool to display per-gateway "live vs historical" |
| 124 |
* badges in the data-found panel: records for gateways whose helper class |
| 125 |
* is loaded will be fully functional after import; others import as |
| 126 |
* historical-only ledger rows. |
| 127 |
* |
| 128 |
* @param string $sd_gateway SureDonation gateway slug. |
| 129 |
* @return bool True if the gateway's helper class is loaded (or offline, which is always available). |
| 130 |
* @since 1.0.0 |
| 131 |
*/ |
| 132 |
public static function is_gateway_live( $sd_gateway ) { |
| 133 |
if ( 'offline' === $sd_gateway ) { |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
$detectors = [ |
| 138 |
'stripe' => 'SureDonation\\Inc\\Payments\\Stripe\\Stripe_Helper', |
| 139 |
'paypal' => 'SureDonation\\Inc\\Payments\\PayPal\\PayPal_Helper', |
| 140 |
'mollie' => 'SureDonation\\Inc\\Payments\\Mollie\\Mollie_Helper', |
| 141 |
'razorpay' => 'SureDonation\\Inc\\Payments\\Razorpay\\Razorpay_Helper', |
| 142 |
]; |
| 143 |
|
| 144 |
if ( ! isset( $detectors[ $sd_gateway ] ) ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
return class_exists( $detectors[ $sd_gateway ] ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Check whether a SureDonation gateway's Pro recurring handler is loaded. |
| 153 |
* |
| 154 |
* Imported subscriptions for gateways whose handler is NOT loaded are |
| 155 |
* stored with `subscription_status = 'imported-historical'` so the donor |
| 156 |
* dashboard can hide cancel/suspend/activate actions. This mitigates the |
| 157 |
* default-to-Stripe fallback in |
| 158 |
* suredonation-pro/inc/recurring/subscription-api.php:362. |
| 159 |
* |
| 160 |
* @param string $sd_gateway SureDonation gateway slug. |
| 161 |
* @return bool True if the gateway's subscription handler class is loaded. |
| 162 |
* @since 1.0.0 |
| 163 |
*/ |
| 164 |
public static function is_subscription_handler_live( $sd_gateway ) { |
| 165 |
$detectors = [ |
| 166 |
'stripe' => 'SureDonationPro\\Inc\\Recurring\\Subscription_Handler', |
| 167 |
'paypal' => 'SureDonationPro\\Inc\\Payments\\PayPal\\PayPal_Subscription_Handler', |
| 168 |
'mollie' => 'SureDonationPro\\Inc\\Payments\\Mollie\\Mollie_Subscription_Handler', |
| 169 |
'razorpay' => 'SureDonationPro\\Inc\\Payments\\Razorpay\\Razorpay_Subscription_Handler', |
| 170 |
]; |
| 171 |
|
| 172 |
if ( ! isset( $detectors[ $sd_gateway ] ) ) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
return class_exists( $detectors[ $sd_gateway ] ); |
| 177 |
} |
| 178 |
} |
| 179 |
|