-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paypal.php
188 lines (167 loc) · 6.15 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
<?php
namespace frontend\components\payment\clients;
use Yii;
use yii\web\BadRequestHttpException;
use yii\base\Exception;
use frontend\components\payment\PaymentGateway;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction as PaypalTransaction;
use PayPal\Api\PaymentExecution;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;
class Paypal extends PaymentGateway
{
const PAYMENT_STATE_CREATED = 'created';
const PAYMENT_STATE_APPROVED = 'approved';
public $identifier = 'paypal';
public $type = 'online';
public function loadConfig()
{
$settings = Yii::$app->settings;
$paypalMode = $settings->get('PaypalSettingForm', 'mode', 'sandbox');
$config = [];
if ($paypalMode == 'live') {
$clientId = $settings->get('PaypalSettingForm', 'client_id');
$clientSecret = $settings->get('PaypalSettingForm', 'client_secret');
$config = ['mode' => 'LIVE'];
} else {
$clientId = $settings->get('PaypalSettingForm', 'sandbox_client_id');
$clientSecret = $settings->get('PaypalSettingForm', 'sandbox_client_secret');
$config = ['mode' => 'SANDBOX'];
}
$context = new ApiContext(new OAuthTokenCredential($clientId, $clientSecret));
$context->setConfig($config);
return $context;
}
protected function loadData()
{
$cart = $this->cart;
$totalPrice = $cart->getTotalPrice();
$currency = "USD";
$fee = $this->getServiceFee($totalPrice);
$totalPrice += $fee;
$itemList = [];
foreach ($cart->getItems() as $cartItem) {
$ppItem = new Item();
if ($cartItem->getQuantity() < 1) {
$ppItem->setName($cartItem->getTitle() . " (1/2)");
$ppItem->setQuantity(1);
$ppItem->setPrice($cartItem->getTotalPrice());
} else {
$ppItem->setName($cartItem->getTitle());
$ppItem->setQuantity($cartItem->getQuantity());
$ppItem->setPrice($cartItem->getPrice());
}
$ppItem->setCurrency($currency);
$ppItem->setSku($cartItem->getId());
$itemList[] = $ppItem;
}
// For discount
if ($cart->hasDiscount()) {
$discount = $cart->getDiscount();
$discountItem = new Item();
$discountItem->setName($discount->getTitle())
->setCurrency($currency)
->setQuantity(1)
->setSku($discount->getId())
->setPrice(($cart->getTotalDiscount()) * (-1));
$itemList[] = $discountItem;
}
// Paypal service fee
$feeItem = new Item();
$feeItem->setName('Paypal service fee')
->setCurrency($currency)
->setQuantity(1)
->setSku('PPFEE')
->setPrice($fee);
$itemList[] = $feeItem;
$ppitemList = new ItemList();
$ppitemList->setItems($itemList);
$details = new Details();
$details->setShipping(0)
->setTax(0)
->setSubtotal($totalPrice);
// ### Amount
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal($totalPrice)
->setDetails($details);
$transaction = new PaypalTransaction();
$transaction->setAmount($amount)
->setItemList($ppitemList)
->setDescription($cart->getTitle())
->setInvoiceNumber($this->getReferenceId());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($this->getConfirmUrl())
->setCancelUrl($this->getCancelUrl());
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
return $payment;
}
protected function getServiceFee($totalPrice)
{
$percent = Yii::$app->settings->get('PaypalSettingForm', 'fee', 0);
return $totalPrice * $percent / 100;
}
protected function sendRequest()
{
$apiContext = $this->loadConfig();
$payment = $this->loadData();
try {
$payment->create($apiContext);
if (self::PAYMENT_STATE_CREATED == strtolower($payment->state)) {// order was created
$link = $payment->getApprovalLink();
$query = parse_url($link, PHP_URL_QUERY);
parse_str($query, $params);
return $this->redirect($link);
}
} catch (PayPalConnectionException $ex) {
throw $ex;
}
}
protected function verify($response)
{
extract($response); // now can use $paymentId, $PayerID, $token
if (!$paymentId || !$PayerID || !$token) throw new BadRequestHttpException("The request is invalid", 1);
$this->setPaymentId($paymentId);
$apiContext = $this->loadConfig();
$payment = Payment::get($paymentId, $apiContext);
if (self::PAYMENT_STATE_CREATED != strtolower($payment->state)) throw new BadRequestHttpException("Transaction #$paymentId : status is invalid", 1);
$execution = new PaymentExecution();
$execution->setPayerId($PayerID);
$transactions = $payment->getTransactions();
$transaction = reset($transactions);
$execution->addTransaction($transaction);
try {
$payment->execute($execution, $apiContext);
return self::PAYMENT_STATE_APPROVED == strtolower($payment->state);
} catch (Exception $e) {
throw $e;
}
}
public function cancelPayment()
{
return true;
}
public function doSuccess()
{
$refId = $this->getReferenceId();
return $this->redirect($this->getSuccessUrl(['ref' => $refId]));
}
public function doError()
{
return $this->redirect($this->getErrorUrl());
}
}