GetPaid_Manual_Gateway::maybe_renew_subscription()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Manual payment gateway
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Manual Payment Gateway class.
11
 *
12
 */
13
class GetPaid_Manual_Gateway extends GetPaid_Payment_Gateway {
14
15
    /**
16
	 * Payment method id.
17
	 *
18
	 * @var string
19
	 */
20
    public $id = 'manual';
21
22
    /**
23
	 * An array of features that this gateway supports.
24
	 *
25
	 * @var array
26
	 */
27
    protected $supports = array(
28
        'subscription',
29
        'addons',
30
        'single_subscription_group',
31
        'multiple_subscription_groups',
32
        'subscription_date_change',
33
        'subscription_bill_times_change',
34
    );
35
36
    /**
37
	 * Payment method order.
38
	 *
39
	 * @var int
40
	 */
41
	public $order = 11;
42
43
    /**
44
	 * Class constructor.
45
	 */
46
	public function __construct() {
47
        parent::__construct();
48
49
        $this->title        = __( 'Test Gateway', 'invoicing' );
50
        $this->method_title = __( 'Test Gateway', 'invoicing' );
51
52
        add_action( 'getpaid_should_renew_subscription', array( $this, 'maybe_renew_subscription' ), 10, 2 );
53
    }
54
55
    /**
56
	 * Process Payment.
57
	 *
58
	 *
59
	 * @param WPInv_Invoice $invoice Invoice.
60
	 * @param array $submission_data Posted checkout fields.
61
	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
62
	 * @return array
63
	 */
64
	public function process_payment( $invoice, $submission_data, $submission ) {
65
66
        // Mark it as paid.
67
        $invoice->mark_paid();
68
69
        // (Maybe) activate subscriptions.
70
        $subscriptions = getpaid_get_invoice_subscriptions( $invoice );
71
72
        if ( ! empty( $subscriptions ) ) {
73
            $subscriptions = is_array( $subscriptions ) ? $subscriptions : array( $subscriptions );
0 ignored issues
show
introduced by
The condition is_array($subscriptions) is always false.
Loading history...
74
75
            foreach ( $subscriptions as $subscription ) {
76
                if ( $subscription->exists() ) {
77
                    $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() );
78
                    $expiry   = gmdate( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) );
79
80
                    $subscription->set_next_renewal_date( $expiry );
81
                    $subscription->set_date_created( current_time( 'mysql' ) );
82
                    $subscription->set_profile_id( $invoice->generate_key( 'manual_sub_' . $invoice->get_id() . '_' . $subscription->get_id() ) );
83
                    $subscription->activate();
84
                }
85
            }
86
        }
87
88
        // Send to the success page.
89
        wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
90
91
    }
92
93
	/**
94
	 * (Maybe) renews a manual subscription profile.
95
	 *
96
	 *
97
	 * @param WPInv_Subscription $subscription
98
	 */
99
	public function maybe_renew_subscription( $subscription, $parent_invoice ) {
100
		// Ensure its our subscription && it's active.
101
		if ( ! empty( $parent_invoice ) && $this->id === $parent_invoice->get_gateway() && $subscription->has_status( 'active trialling' ) ) {
102
			// Renew the subscription.
103
			$subscription->add_payment(
104
				array(
105
					'transaction_id' => $subscription->get_parent_payment()->generate_key(),
106
					'gateway'        => $this->id,
107
				)
108
			);
109
110
			$subscription->renew();
111
		}
112
	}
113
114
    /**
115
	 * Processes invoice addons.
116
	 *
117
	 * @param WPInv_Invoice $invoice
118
	 * @param GetPaid_Form_Item[] $items
119
	 * @return WPInv_Invoice
120
	 */
121
	public function process_addons( $invoice, $items ) {
122
123
        foreach ( $items as $item ) {
124
            $invoice->add_item( $item );
125
        }
126
127
        $invoice->recalculate_total();
128
        $invoice->save();
129
    }
130
131
}
132