Passed
Push — master ( 16627f...b71328 )
by Brian
07:50 queued 03:26
created

renew_manual_subscription_profile()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 45
rs 8.6506
cc 7
nc 6
nop 1
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' );
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_action( 'wpinv_renew_manual_subscription_profile', array( $this, 'renew_manual_subscription_profile' ) );
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) set recurring hooks.
63
        $this->start_manual_subscription_profile( $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
	 * Starts a manual subscription profile.
72
	 *
73
	 *
74
	 * @param WPInv_Invoice $invoice Invoice.
75
	 */
76
	public function start_manual_subscription_profile( $invoice ) {
77
78
        // Retrieve the subscription.
79
        $subscription = wpinv_get_subscription( $invoice );
80
        if ( empty( $subscription ) ) {
81
            return;
82
        }
83
84
        // Schedule an action to run when the subscription expires.
85
        $action_id = as_schedule_single_action(
86
            strtotime( $subscription->expiration ),
87
            'wpinv_renew_manual_subscription_profile',
88
            array( $invoice->get_id() ),
89
            'invoicing'
90
        );
91
92
        // Use the action id as the subscription id.
93
        $subscription->update( 
94
            array(
95
                'profile_id' => $action_id, 
96
                'status'     => 'trialling' == $subscription->status ? 'trialling' : 'active'
97
            )
98
        );
99
100
    }
101
102
    /**
103
	 * Renews a manual subscription profile.
104
	 *
105
	 *
106
	 * @param int $invoice_id Invoice.
107
	 */
108
	public function renew_manual_subscription_profile( $invoice_id ) {
109
110
        // Retrieve the subscription.
111
        $subscription = wpinv_get_subscription( $invoice_id );
112
        if ( empty( $subscription ) ) {
113
            return;
114
        }
115
116
        // Abort if it is canceled or complete.
117
        if ( $subscription->status == 'completed' || $subscription->status == 'cancelled' ) {
118
            return;
119
        }
120
121
        // If we have not maxed out on bill times...
122
        $times_billed = $subscription->get_times_billed();
123
        $max_bills    = $subscription->bill_times;
124
125
        if ( empty( $max_bills ) || $max_bills > $times_billed ) {
126
127
            // Retrieve the invoice.
128
            $invoice = new WPInv_Invoice( $invoice_id );
129
130
            // Renew the subscription.
131
            $subscription->add_payment( array(
132
                'amount'         => $subscription->recurring_amount,
133
                'transaction_id' => $invoice->generate_key(),
134
                'gateway'        => $this->id
135
            ) );
136
137
        }
138
139
        // Renew/Complete the subscription.
140
        $subscription->renew();
141
142
        if ( 'completed' != $subscription->status ) {
143
144
            // Schedule an action to run when the subscription expires.
145
            $action_id = as_schedule_single_action(
146
                strtotime( $subscription->expiration ),
147
                'wpinv_renew_manual_subscription_profile',
148
                array( $invoice_id ),
149
                'invoicing'
150
            );
151
152
            $subscription->update( array( 'profile_id' => $action_id, ) );
153
154
        }
155
156
    }
157
158
}
159