forked from ruskid/yii2-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripeCheckout.php
147 lines (125 loc) · 4.24 KB
/
StripeCheckout.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
<?php
/**
* @copyright Copyright Victor Demin, 2014
* @license https://github.com/ruskid/yii2-stripe/LICENSE
* @link https://github.com/ruskid/yii2-stripe#readme
*/
namespace ruskid\stripe;
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 StripeCheckout extends \yii\base\Widget {
/**
* Form's action that will perform a charge
* @var string form's action url
*/
public $action = "/";
/**
* @see Stripe. The amount (in cents) that's shown to the user.
* Note that you will still have to explicitly include it when you create a charge using the Stripe API.
* @var integer Stripe's amount
*/
public $amount;
/**
* @see Stripe's javascript location
* @var string url to stripe's javascript
*/
public $stripeJs = "https://checkout.stripe.com/checkout.js";
/**
* @see Stripe. The name of your company or website.
* @var string Stripe's modal name
*/
public $name = "Demo Site";
/**
* @see Stripe. A description of the product or service being purchased.
* @var string Stripe's modal description
*/
public $description = "2 widgets ($20.00)";
/**
* @see Stripe. A relative URL pointing to a square image of your brand or product.
* The recommended minimum size is 128x128px.
* @var string Stripe's modal image
*/
public $image = "/128x128.png";
/**
* @see Stripe. The currency of the amount (3-letter ISO code). The default is USD.
* @var string currency
*/
public $currency;
/**
* @see Stripe. The text to be shown on the default blue button.
* @var string label
*/
public $label = 'Pay';
/**
* @see Stripe. The label of the payment button in the Checkout form (e.g. “Subscribe”, “Pay {{amount}}”, etc.).
* If you include {{amount}}, it will be replaced by the provided amount.
* Otherwise, the amount will be appended to the end of your label.
* @var string
*/
public $panelLabel;
/**
* @see Stripe. Specify whether Checkout should validate the billing ZIP code (true or false).
* The default is false.
* @var boolean
*/
public $validateZipCode;
/**
* @see Stripe. If you already know the email address of your user, you can provide it to Checkout to be pre-filled.
* @var string user's email
*/
public $userEmail;
/**
* @see Stripe. Specify whether to include the option to "Remember Me" for future purchases (true or false).
* The default is true.
* @var boolean
*/
public $allowRemember;
const BUTTON_CLASS = 'stripe-button';
/**
* @see Init extension default
*/
public function init() {
StripeHelper::prepareBoolean($this->allowRemember);
StripeHelper::prepareBoolean($this->validateZipCode);
parent::init();
}
/**
* Will show the Stripe's simple form modal
*/
public function run() {
echo $this->generateStripeForm();
}
/**
* Will generate the stripe form
* @return string the generated stripe's modal form
*/
private function generateStripeForm() {
return Html::beginForm($this->action, 'POST') . $this->generateScriptTag() . Html::endForm();
}
/**
* Will generate Stripe script tag with passed parameters
* @return string the generated script tag
*/
private function generateScriptTag() {
return Html::script('', [
'src' => $this->stripeJs,
'data-key' => Yii::$app->stripe->publicKey,
'data-amount' => $this->amount,
'data-name' => $this->name,
'data-description' => $this->description,
'data-currency' => $this->currency,
'data-panel-label' => $this->panelLabel,
'data-zip-code' => $this->validateZipCode,
'data-email' => $this->userEmail,
'data-label' => $this->label,
'data-allow-remember-me' => $this->allowRemember,
'class' => self::BUTTON_CLASS,
]);
}
}