-
Notifications
You must be signed in to change notification settings - Fork 13
/
GatewayPayPal.php
280 lines (221 loc) · 7.52 KB
/
GatewayPayPal.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace Amsgames\LaravelShopGatewayPaypal;
/**
* Gateway that adds PayPal payments to Laravel Shop.
*
* @author Alejandro Mostajo
* @copyright Amsgames, LLC
* @license MIT
* @package Amsgames\LaravelShop
*/
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\CreditCard;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\Payer;
use PayPal\Exception\PayPalConnectionException;
use Amsgames\LaravelShop\Exceptions\CheckoutException;
use Amsgames\LaravelShop\Exceptions\GatewayException;
use Amsgames\LaravelShop\Exceptions\ShopException;
use Amsgames\LaravelShop\Core\PaymentGateway;
use Illuminate\Support\Facades\Config;
class GatewayPayPal extends PaymentGateway
{
/**
* PayPal's api context.
* @var object
*/
protected $apiContext;
/**
* PayPal's credit card.
* @var object
*/
protected $creditCard = null;
/**
* PayPal's credit card.
* @var object
*/
protected $validTypes = ['visa', 'mastercard', 'amex', 'discover'];
/**
* Called on cart checkout.
*
* @param Cart $cart Cart.
*/
public function onCheckout($cart)
{
if (!isset($this->creditCard))
throw new CheckoutException('Credit Card is not set.', 0);
if (!in_array($this->creditCard->getType(), $this->validTypes))
throw new CheckoutException('Credit Card is not supported.', 1);
if ($this->getPatternType($this->creditCard->getNumber()) != $this->creditCard->getType())
throw new CheckoutException('Credit Card is invalid.', 2);
}
/**
* Called by shop to charge order's amount.
*
* @param Cart $cart Cart.
*
* @return bool
*/
public function onCharge($order)
{
if (!isset($this->creditCard))
throw new GatewayException('Credit Card is not set.', 0);
try {
if ($order->total <= 0) {
$this->detail = 'Order total is 0; no PayPal transaction required.';
$this->transactionId = uniqid();
return true;
}
$this->setContext();
$instrument = new FundingInstrument();
$instrument->setCreditCard($this->creditCard);
$payer = new Payer();
$payer->setPaymentMethod('credit_card')
->setFundingInstruments([$instrument]);
$list = new ItemList();
$list->setItems($this->toPayPalItems($order));
$details = new Details();
$details->setShipping($order->totalShipping)
->setTax($order->totalTax)
->setSubtotal($order->totalPrice);
$amount = new Amount();
$amount->setCurrency(Config::get('shop.currency'))
->setTotal($order->total)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($list)
->setDescription(sprintf(
'%s payment, Order #%d',
Config::get('shop.name'),
$order->id
))
->setInvoiceNumber($order->id);
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions([$transaction]);
//$request = clone $payment;
$payment->create($this->apiContext);
$this->transactionId = $payment->id;
$this->detail = 'Success';
return true;
} catch (PayPalConnectionException $e) {
$response = json_decode($e->getData());
throw new GatewayException(
sprintf(
'%s: %s',
$response->name,
isset($response->message) ? $response->message : 'Paypal payment Failed.'
),
1001,
$e
);
} catch (\Exception $e) {
throw new ShopException(
$e->getMessage(),
1000,
$e
);
}
return false;
}
/**
* Sets credit card for usage.
*
* @param string $type Card type. i.e. visa, mastercard
* @param int $number Card number.
* @param mixed $expireMonth Month in which the card expires.
* @param mixed $expireYear Year in which the card expires.
* @param int $cvv CVV.
* @param string $firstname First name printed in card.
* @param string $lastname Last name printed in card.
*/
public function setCreditCard(
$type,
$number,
$expireMonth,
$expireYear,
$cvv,
$firstname,
$lastname
) {
$this->creditCard = new CreditCard();
$this->creditCard->setType($type)
->setNumber($number)
->setExpireMonth($expireMonth)
->setExpireYear($expireYear)
->setCvv2($cvv)
->setFirstName($firstname)
->setLastName($lastname);
return $this;
}
/**
* Setups contexts for api calls.
*/
private function setContext()
{
$this->apiContext = new ApiContext(new OAuthTokenCredential(
Config::get('services.paypal.client_id'),
Config::get('services.paypal.secret')
));
if (!Config::get('services.paypal.sandbox'))
$this->apiContext->setConfig(['mode' => 'live']);
}
/**
* Converts the items in the order into paypal items for purchase.
*
* @param object $order Order.
*
* @return array
*/
private function toPayPalItems($order)
{
$items = [];
foreach ($order->items as $shopItem) {
if ($shopItem->price > 0) {
$item = new Item();
$item->setName(substr($shopItem->displayName, 0, 127))
->setDescription($shopItem->sku)
->setCurrency($shopItem->currency)
->setQuantity($shopItem->quantity)
->setTax($shopItem->tax)
->setPrice($shopItem->price);
$items[] = $item;
}
}
return $items;
}
/**
* Returns the credit card type based on a credit card number pattern.
*
* @param string $cardNumber Credit card number.
*
* @return string
*/
private function getPatternType($cardNumber)
{
if (empty($cardNumber)) return;
$types = [
'visa' => '(4\d{12}(?:\d{3})?)',
'amex' => '(3[47]\d{13})',
'jcb' => '(35[2-8][89]\d\d\d{10})',
'maestro' => '((?:5020|5038|6304|6579|6761)\d{12}(?:\d\d)?)',
'solo' => '((?:6334|6767)\d{12}(?:\d\d)?\d?)',
'mastercard' => '(5[1-5]\d{14})',
'switch' => '(?:(?:(?:4903|4905|4911|4936|6333|6759)\d{12})|(?:(?:564182|633110)\d{10})(\d\d)?\d?)',
];
$names = ['visa','amex', 'jcb', 'maestro', 'solo', 'mastercard', 'switch'];
$matches = [];
$pattern = "#^(?:" . implode('|', $types) . ")$#";
$result = preg_match($pattern, str_replace(' ', '', $cardNumber), $matches);
return $result > 0 ? $names[sizeof($matches)-2] : false;
}
}