/** * Get Member's Stripe Customer Object. * * @since 1.0.0 * @internal * * @param MS_Model_Member $member The member. */ public function find_customer($member) { $customer_id = $member->get_gateway_profile(self::ID, 'customer_id'); $customer = null; if (!empty($customer_id)) { $customer = M2_Stripe_Customer::retrieve($customer_id); // Seems like the customer was manually deleted on Stripe website. if (isset($customer->deleted) && $customer->deleted) { $customer = null; $member->set_gateway_profile(self::ID, 'customer_id', ''); } } return apply_filters('ms_gateway_stripe_find_customer', $customer, $member, $this); }
/** * Tests the Stripe Subscription gateway * @test */ function stripeplan_subscription() { $gateway = MS_Model_Gateway::factory(MS_Gateway_Stripeplan::ID); $user_id = TData::id('user', 'editor'); $membership_id = TData::id('membership', 'recurring'); $subscription = TData::subscribe($user_id, $membership_id); $controller = MS_Factory::load('MS_Controller_Gateway'); $gateway->update_stripe_data(); $data = array('card' => array('number' => '4242424242424242', 'exp_month' => 12, 'exp_year' => date('Y') + 1, 'cvc' => '314')); $res = M2_Stripe_Token::create($data); $token = $res->id; $form_data = array('_wpnonce' => wp_create_nonce($gateway->id . '_' . $subscription->id), 'gateway' => $gateway->id, 'ms_relationship_id' => $subscription->id, 'step' => 'process_purchase', 'stripeToken' => $token, 'stripeTokenType' => 'card', 'stripeEmail' => '*****@*****.**'); $_POST = $form_data; $_REQUEST = $_POST; // Right now the subscription must have status PENDING $this->assertEquals(MS_Model_Relationship::STATUS_PENDING, $subscription->status); /* * This function processes the purchase and will set the subscription * to active. */ $controller->process_purchase(); // Check the subscription status. $this->assertEquals(MS_Model_Relationship::STATUS_ACTIVE, $subscription->status); $this->assertEquals(1, count($subscription->payments)); // Modify the expiration date to trigger another payment. $today = date('Y-m-d'); $subscription->expire_date = $today; $this->assertEquals($today, $subscription->expire_date); $this->assertEquals(0, $subscription->get_remaining_period()); // Trigger next payment and validate it. $subscription->check_membership_status(); $this->assertEquals(2, count($subscription->payments)); // Modify the expiration date to trigger another payment. $subscription->expire_date = $today; $this->assertEquals($today, $subscription->expire_date); $this->assertEquals(0, $subscription->get_remaining_period()); // Trigger next payment and validate it. // THIS TIME NO PAYMENT SHOULD BE MADE because paycycle_repetitions = 2! $subscription->check_membership_status(); $this->assertEquals(2, count($subscription->payments)); // Also the subscription should be cancelled at stripe now. $customer_id = $subscription->get_member()->get_gateway_profile(MS_Gateway_Stripe_Api::ID, 'customer_id'); $customer = M2_Stripe_Customer::retrieve($customer_id); $invoice = $subscription->get_previous_invoice(); $stripe_sub_id = $invoice->external_id; $stripe_sub = $customer->subscriptions->retrieve($stripe_sub_id); $this->assertEquals('active', $stripe_sub->status); $this->assertTrue($stripe_sub->cancel_at_period_end); // Clean up. $customer->delete(); }