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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Paypal\Model;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Model\InfoInterface;
use Magento\Paypal\Model\Config;
use Magento\Paypal\Model\Hostedpro\Request;
use Magento\Sales\Model\Order;
/**
* Website Payments Pro Hosted Solution payment gateway model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Hostedpro extends Direct
{
/**
* Button code
*/
const BM_BUTTON_CODE = 'TOKEN';
/**
* Button type
*/
const BM_BUTTON_TYPE = 'PAYMENT';
/**
* Paypal API method name for button creation
*/
const BM_BUTTON_METHOD = 'BMCreateButton';
/**
* Payment method code
*
* @var string
*/
protected $_code = Config::METHOD_HOSTEDPRO;
/**
* @var string
*/
protected $_formBlockType = \Magento\Paypal\Block\Hosted\Pro\Form::class;
/**
* @var string
*/
protected $_infoBlockType = \Magento\Paypal\Block\Hosted\Pro\Info::class;
/**
* Availability option
*
* @var bool
*/
protected $_canUseInternal = false;
/**
* Availability option
*
* @var bool
*/
protected $_canSaveCc = false;
/**
* Availability option
*
* @var bool
*/
protected $_isInitializeNeeded = true;
/**
* @var \Magento\Paypal\Model\Hostedpro\RequestFactory
*/
protected $hostedproRequestFactory;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
* @param \Magento\Payment\Helper\Data $paymentData
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Payment\Model\Method\Logger $logger
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param ProFactory $proFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Framework\App\RequestInterface $requestHttp
* @param CartFactory $cartFactory
* @param Hostedpro\RequestFactory $hostedproRequestFactory
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Payment\Model\Method\Logger $logger,
\Magento\Framework\Module\ModuleListInterface $moduleList,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\Paypal\Model\ProFactory $proFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Framework\App\RequestInterface $requestHttp,
\Magento\Paypal\Model\CartFactory $cartFactory,
\Magento\Paypal\Model\Hostedpro\RequestFactory $hostedproRequestFactory,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->hostedproRequestFactory = $hostedproRequestFactory;
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$paymentData,
$scopeConfig,
$logger,
$moduleList,
$localeDate,
$proFactory,
$storeManager,
$urlBuilder,
$requestHttp,
$cartFactory,
$resource,
$resourceCollection,
$data
);
}
/**
* Return available CC types for gateway based on merchant country.
* We do not have to check the availability of card types.
*
* @return true
*/
public function getAllowedCcTypes()
{
return true;
}
/**
* Return merchant country code from config,
* use default country if it not specified in General settings
*
* @return string
*/
public function getMerchantCountry()
{
return $this->_pro->getConfig()->getMerchantCountry();
}
/**
* Do not validate payment form using server methods
*
* @return true
*/
public function validate()
{
return true;
}
/**
* Instantiate state and set it to state object
*
* @param string $paymentAction
* @param \Magento\Framework\DataObject $stateObject
* @return void
*/
public function initialize($paymentAction, $stateObject)
{
switch ($paymentAction) {
case Config::PAYMENT_ACTION_AUTH:
case Config::PAYMENT_ACTION_SALE:
$payment = $this->getInfoInstance();
/** @var \Magento\Sales\Model\Order $order */
$order = $payment->getOrder();
$order->setCanSendNewEmailFlag(false);
$payment->setAmountAuthorized($order->getTotalDue());
$payment->setBaseAmountAuthorized($order->getBaseTotalDue());
$this->setPaymentFormUrl($payment);
$stateObject->setState(Order::STATE_PENDING_PAYMENT);
$stateObject->setStatus(Order::STATE_PENDING_PAYMENT);
$stateObject->setIsNotified(false);
break;
default:
break;
}
}
/**
* Sends API request to PayPal to get form URL, then sets this URL to $payment object.
*
* @param \Magento\Payment\Model\InfoInterface $payment
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function setPaymentFormUrl(InfoInterface $payment)
{
$request = $this->buildFormUrlRequest($payment);
$response = $this->sendFormUrlRequest($request);
if ($response) {
$payment->setAdditionalInformation('secure_form_url', $response);
} else {
throw new LocalizedException(__('Cannot get secure form URL from PayPal'));
}
}
/**
* Returns request object with needed data for API request to PayPal to get form URL.
*
* @param \Magento\Payment\Model\InfoInterface $payment
* @return \Magento\Paypal\Model\Hostedpro\Request
*/
protected function buildFormUrlRequest(InfoInterface $payment)
{
$order = $payment->getOrder();
$request = $this->buildBasicRequest()->setOrder($order)->setPaymentMethod($this)->setAmount($order);
return $request;
}
/**
* Returns form URL from request to PayPal.
*
* @param \Magento\Paypal\Model\Hostedpro\Request $request
* @return string|false
*/
protected function sendFormUrlRequest(Request $request)
{
$api = $this->_pro->getApi();
$response = $api->call(self::BM_BUTTON_METHOD, $request->getRequestData());
if (!isset($response['EMAILLINK'])) {
return false;
}
return $response['EMAILLINK'];
}
/**
* Return request object with basic information
*
* @return \Magento\Paypal\Model\Hostedpro\Request
*/
protected function buildBasicRequest()
{
$request = $this->hostedproRequestFactory->create()->setData(
[
'METHOD' => self::BM_BUTTON_METHOD,
'BUTTONCODE' => self::BM_BUTTON_CODE,
'BUTTONTYPE' => self::BM_BUTTON_TYPE,
]
);
return $request;
}
/**
* Get return URL
*
* @param int|null $storeId
* @return string
*/
public function getReturnUrl($storeId = null)
{
return $this->getUrl('paypal/hostedpro/return', $storeId);
}
/**
* Get notify (IPN) URL
*
* @param int|null $storeId
* @return string
*/
public function getNotifyUrl($storeId = null)
{
return $this->getUrl('paypal/ipn', $storeId, false);
}
/**
* Get cancel URL
*
* @param int|null $storeId
* @return string
*/
public function getCancelUrl($storeId = null)
{
return $this->getUrl('paypal/hostedpro/cancel', $storeId);
}
/**
* Build URL for store
*
* @param string $path
* @param int $storeId
* @param bool|null $secure
* @return string
*/
protected function getUrl($path, $storeId, $secure = null)
{
$store = $this->_storeManager->getStore($storeId);
return $this->_urlBuilder->getUrl(
$path,
["_secure" => $secure === null ? $store->isCurrentlySecure() : $secure]
);
}
}