This repository has been archived by the owner on May 29, 2022. It is now read-only.
/
Module.php
executable file
·139 lines (126 loc) · 4.91 KB
/
Module.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
<?php
namespace achertovsky\paypal;
use achertovsky\paypal\components\ModuleTrait;
use yii\helpers\Url;
use Yii;
use yii\base\BootstrapInterface;
class Module extends \yii\base\Module implements BootstrapInterface
{
public $cronControllerPath = 'achertovsky\paypal\controllers\console\CronController';
/**
* for console commands
*/
public function bootstrap($app)
{
if ($app instanceof \yii\console\Application) {
$app->controllerMap[$this->id] = [
'class' => $this->cronControllerPath,
'module' => $this,
];
}
}
use ModuleTrait;
//here is arrays like for Url::toRoute()
public $ipnUrl = ['/payment/payment/payment-notification'];
public $expressSuccessUrl = ['/payment/payment/express-payment'];
public $subscriptionExpressSuccessUrl = ['/payment/payment/subscription-express-confirm'];
public $cancelUrl = ['/', '#' => 'cancel'];
//default currency
public $currency = 'USD';
//models for this module
public $modelMap = [
'PaypalExpressPayment' => 'achertovsky\paypal\models\PaypalExpressPayment',
'PaypalSubscriptionExpress' => 'achertovsky\paypal\models\PaypalSubscriptionExpress',
];
//paypal express checkout version
public $ECVersion = '104.0';
//boolean which indicates is express payment is enabled in app
public $enableExpressPayment = true;
//boolean which indicates is subscription flow is enabled in app
public $enableSubscriptionExpress = true;
//getter to receive users name. it must be assigned to your option
public $subscriptionUsernameGetter = 'username';
//defines if user receive notifications via email when subscription status changed
public $subscriptionEmailNotification = false;
//enables caching or vise versa
public $enableSettingsCache = true;
public function __construct($id, $parent = null, $config = array())
{
parent::__construct($id, $parent, $config);
$this->ipnUrl = Yii::$app->urlManager->createAbsoluteUrl($this->ipnUrl);
$this->expressSuccessUrl = Yii::$app->urlManager->createAbsoluteUrl($this->expressSuccessUrl);
$this->subscriptionExpressSuccessUrl = Yii::$app->urlManager->createAbsoluteUrl($this->subscriptionExpressSuccessUrl);
$this->cancelUrl = Yii::$app->urlManager->createAbsoluteUrl($this->cancelUrl);
}
/**
* creates clear model for express payment
* @return achertovsky\paypal\models\PaypalExpressPayment
*/
public function getPaypalExpressPayment()
{
$paypal = new $this->modelMap['PaypalExpressPayment'];
$paypal->expressSuccessUrl = $this->expressSuccessUrl;
$paypal->cancelUrl = $this->cancelUrl;
$paypal->ipnUrl = $this->ipnUrl;
$paypal->currency = $this->currency;
$paypal->ECVersion = $this->ECVersion;
$paypal->newRecord();
return $paypal;
}
/**
* find existing record in DB by token and fills the model
* @param string $token
* @return achertovsky\paypal\models\PaypalExpressPayment
*/
public function getPaypalExpressPaymentByToken($token)
{
$paypal = call_user_func([
$this->modelMap['PaypalExpressPayment'],
'findByToken'
], $token);
if (empty($paypal)) {
return null;
}
$paypal->expressSuccessUrl = $this->expressSuccessUrl;
$paypal->cancelUrl = $this->cancelUrl;
$paypal->ipnUrl = $this->ipnUrl;
$paypal->currency = $this->currency;
$paypal->ECVersion = $this->ECVersion;
$paypal->newRecord();
return $paypal;
}
/**
* creates clear model for subscription via express payment
* @return achertovsky\paypal\models\PaypalSubscriptionExpress
*/
public function getPaypalSubscriptionExpress()
{
$subscription = new $this->modelMap['PaypalSubscriptionExpress'];
$subscription->successUrl = $this->subscriptionExpressSuccessUrl;
$subscription->cancelUrl = $this->cancelUrl;
$subscription->currency = $this->currency;
$subscription->ECVersion = $this->ECVersion;
$subscription->newRecord();
return $subscription;
}
/**
* find existing record in DB by token and fills the model
* @param string $token
* @return achertovsky\paypal\models\PaypalExpressPayment
*/
public function getPaypalSubscriptionExpressByToken($token)
{
$subscription = call_user_func([
$this->modelMap['PaypalSubscriptionExpress'],
'findByToken'
], $token);
if (empty($subscription)) {
return null;
}
$subscription->successUrl = $this->subscriptionExpressSuccessUrl;
$subscription->cancelUrl = $this->cancelUrl;
$subscription->currency = $this->currency;
$subscription->ECVersion = $this->ECVersion;
return $subscription;
}
}