Skip to content

Commit

Permalink
Merge pull request #18 from yii2mod/revert-15-master
Browse files Browse the repository at this point in the history
Revert "Stripe lib updated to 7, Stripe Checkout & webhook & some new attributes "
  • Loading branch information
Igor Chepurnoy authored Aug 6, 2020
2 parents 208a1f3 + 7f5f316 commit 3b1ef4f
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 4,401 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Thumbs.db
# composer vendor dir
/vendor

#/composer.lock
/composer.lock

# composer itself is not needed
composer.phar
Expand All @@ -32,4 +32,4 @@ phpunit.phar
/tests/data/config.local.php

# runtime cache
/tests/runtime
/tests/runtime
1 change: 0 additions & 1 deletion .phpunit.result.cache

This file was deleted.

170 changes: 4 additions & 166 deletions Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii2mod\cashier\models\SubscriptionModel;
use yii\helpers\Url;

/**
* Class Billable
Expand All @@ -33,93 +32,6 @@ trait Billable
*/
protected static $stripeKey;

/**
* Mapping between attributes inside Stripe's metadata and a subscriptionModel attributes
* They will be updated by webhook controller , for example: 'metadata_id' => 'student_id'
*
* @return Array
*/
public static function billableMapMetadataAttributes()
{
return [];
}

/**
* Get the Stripe customer instance for the current user and token.
* (copied from SubscriptionBuilder)
*
* @return \Stripe\Customer
*/
public function getStripeCustomer($token=null)
{
if (!$this->stripe_id) {
$customer = $this->createAsStripeCustomer($token);
} else {
$customer = $this->asStripeCustomer();
}

return $customer;
}

/**
* Make a "one off" charge on the customer for the given amount.
*
* @param int $amount
* @param array $options
*
* @return \Stripe\Checkout\Session
*
* @throws Card
*/
public function createCheckoutSessionForSubscription($planID, array $optionsParam = []): \Stripe\Checkout\Session
{
$customer = $this->getStripeCustomer();
if (!$this->stripe_id) {
throw new InvalidArgumentException('No stripe customer provided.');
}

$controller_url_name = '';
if(array_key_exists('controller_url_name', $optionsParam)){
$controller_url_name = $optionsParam['controller_url_name'] . '/';
}
$url_base = Url::base(true);
if(array_key_exists('url_base', $optionsParam)){
$url_base = $optionsParam['url_base'] . '/';
}

$options = [
'customer' => $this->stripe_id,
'payment_method_types' => ['card'],
'subscription_data' => [
'items'=> [
['plan'=> $planID, 'quantity'=> 1]
],
],
'success_url' => $url_base . $controller_url_name . 'success?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => $url_base . $controller_url_name . 'cancel',
];

if(array_key_exists('success_url', $optionsParam)){
$options['success_url'] = $optionsParam['success_url'];
}
if(array_key_exists('cancel_url', $optionsParam)){
$options['cancel_url'] = $optionsParam['cancel_url'];
}
if(array_key_exists('client_reference_id', $optionsParam)){
$options['client_reference_id'] = strval($optionsParam['client_reference_id']);
}
if(array_key_exists('metadata', $optionsParam)){
$options['subscription_data']['metadata'] = $optionsParam['metadata'];
}

// if($this->email){
// $options['customer_email'] = '[email protected]';
// }

$session = \Stripe\Checkout\Session::create($options, ['api_key' => $this->getStripeKey()] );
return $session;
}

/**
* Make a "one off" charge on the customer for the given amount.
*
Expand Down Expand Up @@ -218,44 +130,6 @@ public function newSubscription(string $subscription, string $plan): Subscriptio
return new SubscriptionBuilder($this, $subscription, $plan);
}

/**
* Create a new subscription from checkout params.
*
* @param array $payloadDataObject
*
* @return SubscriptionModel
*/
public function loadSubscriptionModel($subscriptionID, $clientReferenceId)
{
$subscriptionBuilder = new SubscriptionBuilder($this, '', '');
return $subscriptionBuilder->loadSubscriptionModel($subscriptionID, $clientReferenceId);
}

/**
* Create a new subscription from checkout params.
*
* @param array $conditions https://stripe.com/docs/api/subscriptions/list
*
* @return Boolean
*/
public function updateSubscriptionModels($conditions = array())
{
if (!$this->stripe_id) {
return false;
}

$conditions['customer'] = $this->stripe_id;
\Stripe\Stripe::setApiKey(Yii::$app->params['stripe']['apiKey']);
$stripeSubscriptions = \Stripe\Subscription::all($conditions) ;

foreach ($stripeSubscriptions as $stripeSubscription) {
$subscriptionBuilder = new SubscriptionBuilder($this, '', '');
$subscriptionBuilder->updateSubscriptionModel($stripeSubscription, null);
}

return true;
}

/**
* Determine if the user is on trial.
*
Expand Down Expand Up @@ -324,41 +198,6 @@ public function subscription(string $subscription = 'default'): ?SubscriptionMod
return $this->getSubscriptions()->where(['name' => $subscription])->one();
}

/**
* Get a subscription instance by conditions.
*
* @param array $conditions
*
* @return SubscriptionModel|null
*/
public function subscriptionByConditions($conditions): ?SubscriptionModel
{
return $this->getSubscriptions()->where($conditions)->one();
}

/**
* Get a subscription instance by name.
*
* @param string $subscription
*
* @return SubscriptionModel|null
*/
public function subscriptionByStripeID(string $stripe_id = 'default'): ?SubscriptionModel
{
return $this->getSubscriptions()->where(['stripe_id' => $stripe_id])->one();
}

/**
* Get a subscription instance by name.
*
* @param string $subscription
*
* @return SubscriptionModel|null
*/
public static function subscriptionOnlyByStripeID($stripe_id){
return SubscriptionModel::find()->where(['stripe_id'=> $stripe_id])->one();
}

/**
* @return mixed
*/
Expand Down Expand Up @@ -462,10 +301,9 @@ public function invoices(bool $includePending = false, array $parameters = []):
{
$invoices = [];

$parameters = array_merge(['limit' => 24, 'customer' => $this->stripe_id], $parameters);
$parameters = array_merge(['limit' => 24], $parameters);

\Stripe\Stripe::setApiKey(Yii::$app->params['stripe']['apiKey']);
$stripeInvoices = \Stripe\Invoice::all($parameters);
$stripeInvoices = $this->asStripeCustomer()->invoices($parameters);

// Here we will loop through the Stripe invoices and create our own custom Invoice
// instances that have more helper methods and are generally more convenient to
Expand Down Expand Up @@ -632,12 +470,12 @@ public function hasStripeId(): bool
/**
* Create a Stripe customer for the given user.
*
* @param string|null $token
* @param string $token
* @param array $options
*
* @return Customer
*/
public function createAsStripeCustomer($token = null, array $options = []): Customer
public function createAsStripeCustomer(string $token, array $options = []): Customer
{
$options = array_key_exists('email', $options)
? $options : array_merge($options, ['email' => $this->email]);
Expand Down
2 changes: 1 addition & 1 deletion Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct($user, StripeInvoice $invoice)
*/
public function date($timezone = null)
{
$carbon = Carbon::createFromTimestampUTC($this->invoice->created);
$carbon = Carbon::createFromTimestampUTC($this->invoice->date);

return $timezone ? $carbon->setTimezone($timezone) : $carbon;
}
Expand Down
61 changes: 0 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ Next, you should configure your params section in your configuration file:
'params' => [
.....
'stripe' => [
'pubKey' => 'Your Publishable Api Key for Stripe Checkout',
'apiKey' => 'Your Secret Api Key'
],
],
Expand Down Expand Up @@ -492,63 +491,3 @@ $refund = $user->refund($invoice->charge);

var_dump($refund->amount); // 1000
```

Stripe's checkout UI
------------

Easy & quick checkout UI from Stripe to pay subscriptions. Needs a subscription plan ID from a valid subscription. The "controller_url_name" will be used to build url for user redirection, that controller should implement a 'success' & 'cancel' methods. You could add metadata additional info to store with the stripe's subscription.

```php
StripeCheckoutSubsciption::widget([
'session' => $user->createCheckoutSessionForSubscription(
'STRIPE_PLAN_ID',
[
'controller_url_name' => 'stripe',
// 'client_reference_id' => $user->id,
'metadata' => [
"student_id" => $user->id,
"student_name" => $student->name . ' ' . $student->surname,
"user_id" => $user->id,
"some_entity_id_from_other_table" => 777
]
])
]
);
```

If you set a an attribute mapping in your billable model, the webhook controller will save that data in $subscriptionModel->metadata_id . The mapping tie $subscriptionModel->metadata_id and the name of attribute sended in metadata checkout widget
```php
use Billable;
public static function billableMapMetadataAttributes()
{
return [ 'metadata_id' => 'some_entity_id_from_other_table' ];
}
```

The Webhook controller will save in your database a new subscription, only if you create a new webhook in the stripe dashboard. Create a new webhook at developers/webhooks with the folowing information:
- URL: Your webhoolk url, something like https://example-domain.com/webhook/handle-webhook
- Event types: checkout.session.completed

Testing this extension
------------

Install dependencies:
```php
sudo apt install php-sqlite3 php-curl
composer install --prefer-dist --no-scripts --no-autoloader && rm -rf /root/.composer && composer dump-autoload --no-scripts --optimize
```

Add to your Stripe account, in test mode, the following:
- Add one subscripctions named (and with the same ID): main
- Add one subscripction plans named (and with the same ID): monthly-10-1 and monthly-10-2 , price: 10$ , trialdays: 7
- Add a coupon named and with the same ID: coupon-1 of 5$

Then, execute all tests with:
```bash
STRIPE_SECRET="YOUR_STRIPE_TEST_SECRET_KEY" STRIPE_PUB_KEY="YOUR_STRIPE_TEST_PUB_KEY" vendor/phpunit/phpunit/phpunit -v --bootstrap tests/bootstrap.php tests
```

Then, execute single test with:
```bash
STRIPE_SECRET="YOUR_STRIPE_TEST_SECRET_KEY" STRIPE_PUB_KEY="YOUR_STRIPE_TEST_PUB_KEY" vendor/phpunit/phpunit/phpunit -v --bootstrap tests/bootstrap.php tests --filter testSubscriptionsCanBeCreated
```
Loading

0 comments on commit 3b1ef4f

Please sign in to comment.