/
BitpayPayments.php
236 lines (203 loc) · 7.44 KB
/
BitpayPayments.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace BitpayPayments;
use BitpayPayments\Model\BitpayPaymentsConfig;
use Propel\Generator\Model\Database;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Thelia;
use Thelia\Log\Tlog;
use Thelia\Model\Base\ModuleQuery;
use Thelia\Model\ModuleImageQuery;
use Thelia\Model\Order;
use Thelia\Module\BaseModule;
use Thelia\Module\PaymentModuleInterface;
class BitpayPayments extends BaseModule implements PaymentModuleInterface
{
/* @var \Bitpay\PrivateKey */
private $privateKey;
/* @var \Bitpay\PublicKey */
private $publicKey;
/*
* You may now override BaseModuleInterface methods, such as:
* install, destroy, preActivation, postActivation, preDeactivation, postDeactivation
*
* Have fun !
*/
/**
*
* Method used by payment gateway.
*
* If this method return a \Thelia\Core\HttpFoundation\Response instance, this response is send to the
* browser.
*
* In many cases, it's necessary to send a form to the payment gateway. On your response you can return this form already
* completed, ready to be sent
*
* @param \Thelia\Model\Order $order processed order
* @return null|\Thelia\Core\HttpFoundation\Response
*/
public function pay(Order $order)
{
$this->loadBitpayKeys();
$client = new \Bitpay\Client\Client();
$adapter = new \Bitpay\Client\Adapter\CurlAdapter();
$config = new BitpayPaymentsConfig();
$config->pushValues();
if ($config->getSandbox()) {
$pairingKey = $config->getPairingKeySandbox();
$apiKey = $config->getApiKeySandbox();
$network = new \Bitpay\Network\Testnet();
$environment = "Sandbox";
} else {
$pairingKey = $config->getPairingKey();
$apiKey = $config->getApiKey();
$network = new \Bitpay\Network\Livenet();
$environment = "Live";
}
$client->setPrivateKey($this->privateKey);
$client->setPublicKey($this->publicKey);
$client->setNetwork($network);
$client->setAdapter($adapter);
if (!isset($apiKey) || $apiKey == '') {
// must create API key
if (!isset($pairingKey) || $pairingKey == '') {
// error: no pairing key
$error = "Thelia BitpayPayments error: No API key or pairing key for environment $environment provided.";
Tlog::getInstance()->error($error);
throw new \Exception($error);
} else {
// pairing key available, now trying to get an API key
$sin = \Bitpay\SinKey::create()->setPublicKey($this->publicKey)->generate();
try {
$token = $client->createToken(array(
'pairingCode' => $pairingKey,
'label' => 'Thelia BitpayPayments',
'id' => (string)$sin,
));
} catch (\Exception $e) {
$request = $client->getRequest();
$response = $client->getResponse();
$error = 'Thelia BitpayPayments error:' . PHP_EOL . PHP_EOL . $request . PHP_EOL . PHP_EOL . $response . PHP_EOL . PHP_EOL;
Tlog::getInstance()->error($error);
throw new \Exception($error);
}
$config->setApiKeyCurrentEnvironment($token->getToken());
$config->setPairingKeyCurrentEnvironment('');
}
}
// token should be available now
$token = new \Bitpay\Token();
$token->setToken($config->getApiKeyCurrentEnvironment());
$client->setToken($token);
$invoice = new \Bitpay\Invoice();
$item = new \Bitpay\Item();
$item->setCode('testCode');
$item->setDescription('Purchase');
$item->setPrice($order->getTotalAmount());
$invoice->setItem($item);
$invoice->setCurrency( new \Bitpay\Currency($order->getCurrency()->getCode()) );
try {
$client->createInvoice($invoice);
} catch (\Exception $e) {
$request = $client->getRequest();
$response = $client->getResponse();
$error = 'Thelia BitpayPayments error:' . PHP_EOL . PHP_EOL . $request . PHP_EOL . PHP_EOL . $response . PHP_EOL . PHP_EOL;
Tlog::getInstance()->error($error);
throw new \Exception($error);
}
}
/**
* Tries to load the bitpay encryption keys. If that doesn't work, it creates new ones.
*/
private function loadBitpayKeys()
{
try {
$storageEngine = new \Bitpay\Storage\FilesystemStorage();
$this->privateKey = $storageEngine->load(THELIA_CACHE_DIR . 'bitpay.pri');
$this->publicKey = $storageEngine->load(THELIA_CACHE_DIR . 'bitpay.pub');
} catch (\Exception $e) {
$this->generateBitpayKeys();
}
}
/**
* Generates new bitpay encryption keys and saves them.
*/
private function generateBitpayKeys()
{
$this->privateKey = new \Bitpay\PrivateKey(THELIA_CACHE_DIR . 'bitpay.pri');
$this->privateKey->generate();
$this->publicKey = new \Bitpay\PublicKey(THELIA_CACHE_DIR . 'bitpay.pub');
$this->publicKey->generate($this->privateKey);
$storageEngine = new \Bitpay\Storage\FilesystemStorage();
$storageEngine->persist($this->privateKey);
$storageEngine->persist($this->publicKey);
}
/**
*
* This method is call on Payment loop.
*
* If you return true, the payment method will de display
* If you return false, the payment method will not be display
*
* @return boolean
*/
public function isValidPayment()
{
return true;
}
/**
* if you want, you can manage stock in your module instead of order process.
* Return false to decrease the stock when order status switch to pay
*
* @return bool
*/
public function manageStockOnCreation()
{
return false;
}
public function preActivation(ConnectionInterface $con = null)
{
if (version_compare(Thelia::THELIA_VERSION, '2.1.0', '<')) {
return false;
}
return true;
}
public function postActivation(ConnectionInterface $con = null)
{
return;
$database = new Database($con->getWrappedConnection());
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
/* insert the images from image folder if first module activation */
$module = $this->getModuleModel();
if (ModuleImageQuery::create()->filterByModule($module)->count() == 0) {
$this->deployImageFolder($module, sprintf('%s/images', __DIR__), $con);
}
/* set module title */
$this->setTitle(
$module,
array(
"de_DE" => "BitpayPayments",
"en_US" => "BitpayPayments",
"fr_FR" => "BitpayPayments",
)
);
}
/**
* @return string
*/
public function getCode()
{
return "BitpayPayments";
}
/**
* @return int
*/
public static function getModCode($flag=false)
{
$obj = new BitpayPayments();
$mod_code = $obj->getCode();
if($flag) return $mod_code;
$search = ModuleQuery::create()
->findOneByCode($mod_code);
return $search->getId();
}
}