Passed
Push — master ( 8cc3cf...2975b0 )
by Brian
04:06
created

GetPaid_Manual_Gateway::maybe_renew_subscription()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 24
rs 9.9
cc 4
nc 3
nop 2
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( 'subscription', 'addons' );
28
29
    /**
30
	 * Payment method order.
31
	 *
32
	 * @var int
33
	 */
34
	public $order = 11;
35
    
36
    /**
37
	 * Class constructor.
38
	 */
39
	public function __construct() {
40
        parent::__construct();
41
42
        $this->title        = __( 'Manual Payment', 'invoicing' );
43
        $this->method_title = __( 'Manual Payment', 'invoicing' );
44
45
        add_filter( 'getpaid_daily_maintenance_should_expire_subscription', array( $this, 'maybe_renew_subscription' ), 10, 2 );
46
    }
47
48
    /**
49
	 * Process Payment.
50
	 *
51
	 *
52
	 * @param WPInv_Invoice $invoice Invoice.
53
	 * @param array $submission_data Posted checkout fields.
54
	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
55
	 * @return array
56
	 */
57
	public function process_payment( $invoice, $submission_data, $submission ) {
58
59
        // Mark it as paid.
60
        $invoice->mark_paid();
61
62
        // (Maybe) activate subscription.
63
        getpaid_activate_invoice_subscription( $invoice );
64
65
        // Send to the success page.
66
        wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
67
68
    }
69
70
    /**
71
	 * (Maybe) renews a manual subscription profile.
72
	 *
73
	 *
74
	 * @param bool $should_expire
75
     * @param WPInv_Subscription $subscription
76
	 */
77
	public function maybe_renew_subscription( $should_expire, $subscription ) {
78
79
        // Ensure its our subscription && it's active.
80
        if ( 'manual' != $subscription->get_gateway() || ! $subscription->has_status( 'active trialling' ) ) {
81
            return $should_expire;
82
        }
83
84
        // If this is the last renewal, complete the subscription.
85
        if ( $subscription->is_last_renewal() ) {
86
            $subscription->complete();
87
            return false;
88
        }
89
90
        // Renew the subscription.
91
        $subscription->add_payment(
92
            array(
93
                'transaction_id' => $subscription->get_parent_payment()->generate_key(),
94
                'gateway'        => $this->id
95
            )
96
        );
97
98
        $subscription->renew();
99
100
        return false;
101
102
    }
103
104
    /**
105
	 * Processes invoice addons.
106
	 *
107
	 * @param WPInv_Invoice $invoice
108
	 * @param GetPaid_Form_Item[] $items
109
	 * @return WPInv_Invoice
110
	 */
111
	public function process_addons( $invoice, $items ) {
112
113
        foreach ( $items as $item ) {
114
            $invoice->add_item( $item );
115
        }
116
117
        $invoice->recalculate_total();
118
        $invoice->save();
119
    }
120
121
}
122