/**
  * Add card info to Stripe customer profile and to WordPress user meta.
  *
  * @since  1.0.0
  * @api
  *
  * @param MS_Model_Member $member The member model.
  * @param M2_Stripe_Customer $customer The stripe customer object.
  * @param string $token The stripe card token generated by the gateway.
  */
 public function add_card($member, $customer, $token)
 {
     $card = false;
     // 1. Save card to Stripe profile.
     // Stripe API until version 2015-02-16
     if (!empty($customer->cards)) {
         $card = $customer->cards->create(array('card' => $token));
         $customer->default_card = $card->id;
     }
     // Stripe API since 2015-02-18
     if (!empty($customer->sources)) {
         $card = $customer->sources->create(array('card' => $token));
         $customer->default_source = $card->id;
     }
     if ($card) {
         $customer->save();
     }
     /**
      * This action is used by the Taxamo Add-on to check additional country
      * evidence (CC country).
      *
      * @since  1.0.0
      */
     do_action('ms_gateway_stripe_credit_card_saved', $card, $member, $this);
     // 2. Save card to WordPress user meta.
     if ($card) {
         $member->set_gateway_profile(self::ID, 'card_exp', gmdate('Y-m-d', strtotime("{$card->exp_year}-{$card->exp_month}-01")));
         $member->set_gateway_profile(self::ID, 'card_num', $card->last4);
         $member->save();
     }
     do_action('ms_gateway_stripe_add_card_info_after', $customer, $token, $this);
 }
Пример #2
0
 /**
  * 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();
 }