AffiliateWPIntegration.php
3 years ago
AffiliateWPRecurringIntegration.php
3 years ago
AffiliateWPService.php
3 years ago
AffiliateWPServiceProvider.php
3 years ago
AffiliateWPRecurringIntegration.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\AffiliateWP; |
| 4 | |
| 5 | use SureCart\Models\Purchase; |
| 6 | use SureCart\Support\Currency; |
| 7 | |
| 8 | /** |
| 9 | * Custom Integration Class |
| 10 | */ |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class AffiliateWPRecurringIntegration |
| 18 | */ |
| 19 | class AffiliateWPRecurringIntegration extends \Affiliate_WP_Recurring_Base { |
| 20 | /** |
| 21 | * The context for referrals. This refers to the integration that is being used. |
| 22 | * |
| 23 | * @access public |
| 24 | * @var string |
| 25 | */ |
| 26 | public $context = 'surecart'; |
| 27 | |
| 28 | /** |
| 29 | * Get things started |
| 30 | * |
| 31 | * @access public |
| 32 | * @since 2.0 |
| 33 | */ |
| 34 | public function init() { |
| 35 | // Add referral when subscription renewed. |
| 36 | add_action( 'surecart/subscription_renewed', [ $this, 'renewedSubscription' ], 10, 1 ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Records a recurring referral when a subscription renews |
| 41 | * |
| 42 | * @param $subscription Subscription object. |
| 43 | */ |
| 44 | public function renewedSubscription( $subscription ) { |
| 45 | // Check if recurring referral is active |
| 46 | if ( ! class_exists( 'AffiliateWP_Recurring_Referrals' ) ) { |
| 47 | affiliate_wp()->utils->log( 'Recurring referral not applied.' ); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // Get details purchase information |
| 52 | $purchase = Purchase::with( [ 'initial_order', 'subscription', 'subscription.current_period', 'period.checkout', 'product' ] )->find( $subscription->purchase ); |
| 53 | |
| 54 | // Get the order reference. |
| 55 | $reference = $purchase->initial_order ?? null; |
| 56 | |
| 57 | // We must have an order id. |
| 58 | if ( ! $reference->id ) { |
| 59 | affiliate_wp()->utils->log( 'Draft referral creation failed. No order attached.' ); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // Get the parent referral |
| 64 | $parent_referral = affiliate_wp()->referrals->get_by( 'reference', $reference->id, $this->context ); |
| 65 | |
| 66 | // This signup wasn't referred or is the very first payment of a referred subscription |
| 67 | if ( ! $parent_referral || ! is_object( $parent_referral ) || 'rejected' == $parent_referral->status ) { |
| 68 | affiliate_wp()->utils->log( 'Recurring Referrals: No referral found or referral is rejected.' ); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | $amount_due = $purchase->subscription->current_period->checkout->amount_due; |
| 73 | $currency = $reference->currency; |
| 74 | $description = $purchase->product->name; |
| 75 | |
| 76 | if ( Currency::isZeroDecimal( $currency ) ) { |
| 77 | $amount = $amount_due; |
| 78 | } else { |
| 79 | $amount = round( $amount_due / 100, 2 ); |
| 80 | } |
| 81 | |
| 82 | // Calculate referral amount |
| 83 | $referral_amount = $this->calc_referral_amount( $amount, $reference, $parent_referral->referral_id ); |
| 84 | |
| 85 | // Create referral for subscription. |
| 86 | $referral_id = $this->insert_referral( |
| 87 | [ |
| 88 | 'amount' => $referral_amount, |
| 89 | 'reference' => $reference, |
| 90 | 'description' => $description, |
| 91 | 'affiliate_id' => $parent_referral->affiliate_id, |
| 92 | 'context' => $this->context, |
| 93 | ] |
| 94 | ); |
| 95 | |
| 96 | if ( ! $referral_id ) { |
| 97 | affiliate_wp()->utils->log( 'Draft referral creation failed.' ); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // Complete referral |
| 102 | if ( $this->complete_referral( $referral_id ) ) { |
| 103 | affiliate_wp()->utils->log( 'Referral completed successfully during insert_referral()' ); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |
| 109 | } |
| 110 |