-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathPayPal.php
256 lines (220 loc) · 9.45 KB
/
PayPal.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace PayPal;
use Monolog\Logger;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Model\PaypalCartQuery;
use PayPal\Model\PaypalPlanifiedPaymentQuery;
use PayPal\Service\Base\PayPalBaseService;
use PayPal\Service\PayPalAgreementService;
use PayPal\Service\PayPalLoggerService;
use PayPal\Service\PayPalPaymentService;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Propel;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Translation\Translator;
use Thelia\Install\Database;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Model\ModuleImageQuery;
use Thelia\Model\Order;
use Thelia\Model\OrderStatusQuery;
use Thelia\Module\AbstractPaymentModule;
use Thelia\Tools\URL;
class PayPal extends AbstractPaymentModule
{
/** @var string */
const DOMAIN_NAME = 'paypal';
const ROUTER = 'router.PayPal';
/**
* The confirmation message identifier
*/
const CONFIRMATION_MESSAGE_NAME = 'paypal_payment_confirmation';
const RECURSIVE_MESSAGE_NAME = 'paypal_recursive_payment_confirmation';
const CREDIT_CARD_TYPE_VISA = 'visa';
const CREDIT_CARD_TYPE_MASTERCARD = 'mastercard';
const CREDIT_CARD_TYPE_DISCOVER = 'discover';
const CREDIT_CARD_TYPE_AMEX = 'amex';
const PAYPAL_METHOD_PAYPAL = 'paypal';
const PAYPAL_METHOD_EXPRESS_CHECKOUT = 'express_checkout';
const PAYPAL_METHOD_CREDIT_CARD = 'credit_card';
const PAYPAL_METHOD_PLANIFIED_PAYMENT = 'planified_payment';
const PAYPAL_PAYMENT_SERVICE_ID = 'paypal_payment_service';
const PAYPAL_CUSTOMER_SERVICE_ID = 'paypal_customer_service';
const PAYPAL_AGREEMENT_SERVICE_ID = 'paypal_agreement_service';
const PAYMENT_STATE_APPROVED = 'approved';
const PAYMENT_STATE_CREATED = 'created';
const PAYMENT_STATE_REFUSED = 'refused';
const PAYPAL_API_BASE_SANDBOX_URL = "https://api-m.sandbox.paypal.com";
const PAYPAL_API_BASE_URL = "https://api-m.paypal.com";
const PAYPAL_API_AUTH_URL = "/v1/oauth2/token";
const PAYPAL_API_CREATE_ORDER_URL = "/v2/checkout/orders";
const PAYPAL_API_CREATE_PLAN_URL = "/v1/billing/plans";
const PAYPAL_API_CREATE_PRODUCT_URL = "/v1/catalogs/products";
/**
* 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 Order $order
* @return RedirectResponse
* @throws \Exception
*/
public function pay(Order $order)
{
return new RedirectResponse(URL::getInstance()->absoluteUrl('/order/paypal/pay', ["order_id" => $order->getId()]));
}
/**
*
* 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()
{
$isValid = false;
// Check if total order amount is within the module's limits
$order_total = $this->getCurrentOrderTotalAmount();
$min_amount = Paypal::getConfigValue('minimum_amount', 0);
$max_amount = Paypal::getConfigValue('maximum_amount', 0);
if (
($order_total > 0)
&&
($min_amount <= 0 || $order_total >= $min_amount)
&&
($max_amount <= 0 || $order_total <= $max_amount)
) {
// Check cart item count
$cartItemCount = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->countCartItems();
if ($cartItemCount <= Paypal::getConfigValue('cart_item_count', 9)) {
$isValid = true;
if (PayPalBaseService::isSandboxMode()) {
// In sandbox mode, check the current IP
$raw_ips = explode("\n", Paypal::getConfigValue('allowed_ip_list', ''));
$allowed_client_ips = array();
foreach ($raw_ips as $ip) {
$allowed_client_ips[] = trim($ip);
}
$client_ip = $this->getRequest()->getClientIp();
$isValid = in_array($client_ip, $allowed_client_ips);
}
}
}
return $isValid;
}
/**
* 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;
}
/**
* @param \Propel\Runtime\Connection\ConnectionInterface $con
*/
public function postActivation(ConnectionInterface $con = null): void
{
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . "/Config/create.sql"));
// Setup some default values at first install
if (null === self::getConfigValue('minimum_amount', null)) {
self::setConfigValue('minimum_amount', 0);
self::setConfigValue('maximum_amount', 0);
self::setConfigValue('send_payment_confirmation_message', 1);
self::setConfigValue('cart_item_count', 999);
}
if (null === MessageQuery::create()->findOneByName(self::CONFIRMATION_MESSAGE_NAME)) {
$message = new Message();
$message
->setName(self::CONFIRMATION_MESSAGE_NAME)
->setHtmlTemplateFileName('paypal-payment-confirmation.html')
->setTextTemplateFileName('paypal-payment-confirmation.txt')
->setLocale('en_US')
->setTitle('Paypal payment confirmation')
->setSubject('Payment of order {$order_ref}')
->setLocale('fr_FR')
->setTitle('Confirmation de paiement par Paypal')
->setSubject('Confirmation du paiement de votre commande {$order_ref}')
->save()
;
}
if (null === MessageQuery::create()->findOneByName(self::RECURSIVE_MESSAGE_NAME)) {
$message = new Message();
$message
->setName(self::RECURSIVE_MESSAGE_NAME)
->setHtmlTemplateFileName('paypal-recursive-payment-confirmation.html')
->setTextTemplateFileName('paypal-recursive-payment-confirmation.txt')
->setLocale('en_US')
->setTitle('Paypal payment confirmation')
->setSubject('Payment of order {$order_ref}')
->setLocale('fr_FR')
->setTitle('Confirmation de paiement par Paypal')
->setSubject('Confirmation du paiement de votre commande {$order_ref}')
->save()
;
}
/* Deploy the module's image */
$module = $this->getModuleModel();
if (ModuleImageQuery::create()->filterByModule($module)->count() == 0) {
$this->deployImageFolder($module, sprintf('%s/images', __DIR__), $con);
}
}
public function update($currentVersion, $newVersion, ConnectionInterface $con = null): void
{
$finder = (new Finder())
->files()
->name('#.*?\.sql#')
->sortByName()
->in(__DIR__ . DS . 'Config' . DS . 'Update')
;
$database = new Database($con);
/** @var \Symfony\Component\Finder\SplFileInfo $updateSQLFile */
foreach ($finder as $updateSQLFile) {
if (version_compare($currentVersion, str_replace('.sql', '', $updateSQLFile->getFilename()), '<')) {
$database->insertSql(
null,
[
$updateSQLFile->getPathname()
]
);
}
}
}
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"])
->autowire(true)
->autoconfigure(true);
}
public static function getBaseUrl()
{
if ((bool)PayPal::getConfigValue('sandbox') === true){
return self::PAYPAL_API_BASE_SANDBOX_URL;
}
return self::PAYPAL_API_BASE_URL;
}
}