forked from yii2mod/yii2-cashier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStripeCheckoutSubsciption.php
157 lines (135 loc) · 4.19 KB
/
StripeCheckoutSubsciption.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @copyright Copyright Victor Demin, 2014
* @license https://github.com/ruskid/yii2-stripe/LICENSE
* @link https://github.com/ruskid/yii2-stripe#readme
*/
namespace yii2mod\cashier;
use Yii;
use yii\helpers\Html;
/**
* Yii stripe simple form checkout class.
* https://stripe.com/docs/checkout#integration-simple
*
* @author Victor Demin <[email protected]>
*/
class StripeCheckoutSubsciption extends \yii\base\Widget {
/**
* The Stripe API key.
*
* @var string
*/
protected static $stripePubKey;
/**
* Label of the button that starts stripe checkout
* @var string plan ID
*/
public $buttonlabel = "Subscribe";
/**
* Label of the button that starts stripe checkout
* @var string plan ID
*/
public $buttonSuffixID = "";
/**
* Url redirect on success
* @var string succes url
*/
public $successUrl = "/";
/**
* Url redirect on cancel
* @var string cancel url
*/
public $cancelUrl = "/";
/**
* Additional options to be added.
* @var array additional options.
*/
public $options = [];
/**
* Stripe session
* @var \Stripe\Checkout\Session
*/
public $session = null;
/**
* Stripe session
* @var \Stripe\Checkout\Session
*/
public $sessionId = null;
/**
* @see Stripe's javascript location
* @var string url to stripe's javascript
*/
public $stripeJs = "https://js.stripe.com/v3";
const BUTTON_CLASS = 'stripe-button';
/**
* @see Init extension default
*/
public function init() {
parent::init();
}
/**
* Will show the Stripe's simple form modal
*/
public function run() {
echo Html::script('', [
'src' => $this->stripeJs,
]);
if($this->session!=null){
$this->sessionId = $this->session->id;
}
if($this->buttonSuffixID==''){
$this->buttonSuffixID = rand(1, 1000000);
}
echo $this->generateButton($this->buttonSuffixID, $this->buttonlabel);
echo Html::script("
(function() {
var stripe = Stripe('{$this->getStripePubKey()}');
var checkoutButton = document.getElementById('checkout-button-{$this->buttonSuffixID}');
checkoutButton.addEventListener('click', function () {
// When the customer clicks on the button, redirect
// them to Checkout.
stripe.redirectToCheckout({
// Instead use one of the strategies described in
// https://stripe.com/docs/payments/checkout/fulfillment
// successUrl: 'https://your-website.com/success',
// cancelUrl: 'https://your-website.com/canceled',
sessionId: '{$this->sessionId}'
})
.then(function (result) {
if (result.error) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer.
var displayError = document.getElementById('stripe-checkout-error-message');
displayError.textContent = result.error.message;
}
});
});
})();
", ['defer' => true]);
echo Html::tag('div', '', [
'id'=> 'stripe-checkout-error-message',
// 'class' => "alert alert-danger",
'role' => "alert"
]);
}
/**
* Will generate the stripe form
* @return string the generated stripe's modal form
*/
private function generateButton($randElemIDsufix, $label) {
return Html::button(Yii::t('app', $label), [
'class' => 'btn btn-primary btn-flat',
"id" => "checkout-button-" . $randElemIDsufix,
"role" => "link"
]);
}
/**
* Get the Stripe API key.
*
* @return string
*/
public static function getStripePubKey(): string
{
return static::$stripePubKey ?: Yii::$app->params['stripe']['pubKey'];
}
}