1
|
|
|
<?php |
2
|
|
|
namespace Tahoe\Bundle\MultiTenancyBundle\Gateway; |
3
|
|
|
|
4
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Entity\Tenant; |
5
|
|
|
|
6
|
|
|
class RecurlyManager implements GatewayManagerInterface |
7
|
|
|
{ |
8
|
|
|
private $recurly; |
9
|
|
|
private $accountPrefix; |
10
|
|
|
private $planName; |
11
|
|
|
|
12
|
|
|
public function __construct($subdomain, $privateKey, $prefix, $plan_name) |
13
|
|
|
{ |
14
|
|
|
\Recurly_Client::$subdomain = $subdomain; |
15
|
|
|
\Recurly_Client::$apiKey = $privateKey; |
16
|
|
|
$this->accountPrefix = $prefix; |
17
|
|
|
$this->planName = $plan_name; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
|
|
public function getAccountPrefix() |
24
|
|
|
{ |
25
|
|
|
return $this->accountPrefix; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public function createAccount(Tenant $tenant) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$account_code = sprintf("%s-%s", $this->accountPrefix, $tenant->getId()); |
31
|
|
|
$account = new \Recurly_Account($account_code); |
32
|
|
|
$account->company_name = $tenant->getName(); |
33
|
|
|
$account->create(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function subscriptionExists(Tenant $tenant) |
37
|
|
|
{ |
38
|
|
|
$subscription = $this->getSubscription($tenant); |
39
|
|
|
|
40
|
|
|
return $subscription !== null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getSubscription(Tenant $tenant) |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
|
|
$account_code = sprintf("%s-%s", $this->accountPrefix, $tenant->getId()); |
47
|
|
|
$subscriptions = \Recurly_SubscriptionList::getForAccount($account_code); |
48
|
|
|
} catch(\Exception $e) |
49
|
|
|
{ |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $subscriptions->current(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getBillingInfo(Tenant $tenant) |
57
|
|
|
{ |
58
|
|
|
try { |
59
|
|
|
$account_code = sprintf("%s-%s", $this->accountPrefix, $tenant->getId()); |
60
|
|
|
$billing = \Recurly_BillingInfo::get($account_code); |
61
|
|
|
} catch(\Exception $e) |
62
|
|
|
{ |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $billing; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function updateBillingInfo(Tenant $tenant, $data) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$account_code = sprintf("%s-%s", $this->accountPrefix, $tenant->getId()); |
72
|
|
|
$billing_info = $this->createBillingInfo($data); |
73
|
|
|
$billing_info->account_code = $account_code; |
74
|
|
|
$billing_info->update(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function createSubscription(Tenant $tenant, $data) |
78
|
|
|
{ |
79
|
|
|
$account_code = sprintf("%s-%s", $this->accountPrefix, $tenant->getId()); |
80
|
|
|
|
81
|
|
|
$account = \Recurly_Account::get($account_code); |
82
|
|
|
$account->first_name = $data->first_name; |
83
|
|
|
$account->last_name = $data->last_name; |
84
|
|
|
|
85
|
|
|
$subscription = new \Recurly_Subscription(); |
86
|
|
|
$subscription->plan_code = $this->getPlanName(); |
87
|
|
|
$subscription->currency = 'GBP'; // TODO: make this more flexible |
88
|
|
|
|
89
|
|
|
$billing_info = $this->createBillingInfo($data); |
90
|
|
|
|
91
|
|
|
$account->billing_info = $billing_info; |
92
|
|
|
$subscription->account = $account; |
93
|
|
|
|
94
|
|
|
$subscription->create(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $data |
99
|
|
|
* @return \Recurly_BillingInfo |
100
|
|
|
*/ |
101
|
|
|
private function createBillingInfo($data) |
102
|
|
|
{ |
103
|
|
|
$billing_info = new \Recurly_BillingInfo(); |
104
|
|
|
$billing_info->number = str_replace(' ', '', $data->credit_card_number); |
105
|
|
|
$expiration = explode(" / ", $data->expiration); |
106
|
|
|
$billing_info->month = $expiration[0]; |
107
|
|
|
$billing_info->year = $expiration[1]; |
108
|
|
|
$billing_info->verification_value = $data->verification_value; |
109
|
|
|
|
110
|
|
|
return $billing_info; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function getPlanName() |
117
|
|
|
{ |
118
|
|
|
return $this->planName; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.