-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathPayPalCommerce.php
353 lines (314 loc) · 12.3 KB
/
PayPalCommerce.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
namespace Give\PaymentGateways\PayPalCommerce;
use Give\Donations\Models\Donation;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\PaymentGateways\Commands\GatewayCommand;
use Give\Framework\PaymentGateways\Commands\PaymentComplete;
use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException;
use Give\Framework\PaymentGateways\PaymentGateway;
use Give\Framework\Support\ValueObjects\Money;
use Give\Log\Log;
use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail;
/**
* Class PayPalCommerce
*
* Boots the PayPalCommerce gateway and provides its basic registration properties
*
* @since 2.9.0
*/
class PayPalCommerce extends PaymentGateway
{
/**
* @deprecated
*
* Use getId or id function to access payment gateway id.
*
*/
const GATEWAY_ID = 'paypal-commerce';
/**
* @since 2.19.0
*/
public function getLegacyFormFieldMarkup(int $formId, array $args): string
{
return give(AdvancedCardFields::class)->addCreditCardForm($formId);
}
/**
* @since 2.19.0
*
* @return string
*/
public static function id(): string
{
return 'paypal-commerce';
}
/**
* @since 2.19.0
*
* @return string
*/
public function getId(): string
{
return self::id();
}
/**
* @since 2.19.0
*
* @return string
*/
public function getName(): string
{
return esc_html__('PayPal Donations', 'give');
}
/**
* @since 2.19.0
*
* @return string
*/
public function getPaymentMethodLabel(): string
{
return esc_html__('Credit Card', 'give');
}
/**
* @since 4.0.0 updated to update and capture payment
* @since 2.19.0
*
* @param array{payPalOrderId: string|null, payPalAuthorizationId: string|null} $gatewayData
* @throws \Exception
*/
public function createPayment(Donation $donation, $gatewayData): GatewayCommand
{
$payPalOrderId = $gatewayData['payPalOrderId'];
/** @var Repositories\PayPalOrder $payPalOrderRepository */
$payPalOrderRepository = give(Repositories\PayPalOrder::class);
$payPalOrder = $payPalOrderRepository->getApprovedOrder($payPalOrderId);
if ($payPalOrder->status === 'COMPLETED') {
$this->validatePayPalOrder($payPalOrder);
$transactionId = $payPalOrder->purchase_units[0]->payments->captures[0]->id;
} elseif ($payPalOrder->status === 'APPROVED' || $payPalOrder->status === 'CREATED') {
$this->validate3dSecure($payPalOrder);
if ($this->shouldUpdateOrder($donation, $payPalOrder)){
$payPalOrderRepository->updateApprovedOrder($payPalOrderId, $donation->amount);
}
// ready to capture order, response is the updated PayPal order.
$response = $payPalOrderRepository->approveOrder($payPalOrderId);
$this->validatePayPalOrder($response);
$transactionId = $response->purchase_units[0]->payments->captures[0]->id;
} else {
throw new PaymentGatewayException('PayPal Order status is not found.');
}
give()->payment_meta->update_meta(
$donation->id,
'_give_order_id',
$payPalOrderId
);
return PaymentComplete::make($transactionId)
->setPaymentNotes(
sprintf(
__('Transaction Successful. PayPal Transaction ID: %1$s PayPal Order ID: %2$s', 'give'),
$transactionId,
$payPalOrderId
)
);
}
/**
* @since 3.0.0 Conditionally add "Transaction Type" setting.
* @since 2.33.0 Register new payment field type setting.
* @since 2.27.3 Enable Venmo payment method by default.
* @since 2.16.2 Add setting "Transaction type".
*/
public function getOptions()
{
/* @var MerchantDetail $merchantDetails */
$merchantDetails = give(MerchantDetail::class);
$settings = [
[
'type' => 'title',
'id' => 'give_gateway_settings_1',
'table_html' => false,
],
[
'id' => 'paypal_commerce_introduction',
'type' => 'paypal_commerce_introduction',
],
[
'type' => 'sectionend',
'id' => 'give_gateway_settings_1',
'table_html' => false,
],
[
'type' => 'title',
'id' => 'give_gateway_settings_2',
],
[
'name' => esc_html__('Account Country', 'give'),
'id' => 'paypal_commerce_account_country',
'type' => 'paypal_commerce_account_country',
],
[
'name' => esc_html__('Connect With Paypal', 'give'),
'id' => 'paypal_commerce_account_manger',
'type' => 'paypal_commerce_account_manger',
],
[
'name' => esc_html__('Accept Venmo', 'give'),
'id' => 'paypal_commerce_accept_venmo',
'type' => 'radio_inline',
'desc' => esc_html__(
'Displays a button allowing Donors to pay with Venmo (US-only). Donations still come into your PayPal account and are subject to normal PayPal transaction fees.',
'give'
),
'default' => 'enabled',
'options' => [
'enabled' => esc_html__('Enabled', 'give'),
'disabled' => esc_html__('Disabled', 'give'),
],
],
[
'name' => esc_html__('Payment Field Type', 'give'),
'desc' => sprintf(
esc_html__(
'"Auto" provides the most payment options to the donor as possible, based on your account. "Smart Buttons Only" shows only the payment buttons. There is no "Hosted Only" option at this time due to limitations with PayPal\'s hosted fields. Not sure what this means? %1$sRead here%2$s.',
'give'
),
'<a href="https://docs.givewp.com/paypal-settings" target="_blank">',
'</a>'
),
'id' => 'paypal_payment_field_type',
'type' => 'radio_inline',
'options' => [
'auto' => esc_html__('Auto', 'give'),
'smart-buttons' => esc_html__('Smart Buttons Only', 'give'),
],
'default' => 'auto',
],
[
'name' => esc_html__('PayPal Donations Gateway Settings Docs Link', 'give'),
'id' => 'paypal_commerce_gateway_settings_docs_link',
'url' => esc_url('http://docs.givewp.com/paypal-donations'),
'title' => esc_html__('PayPal Donations Gateway Settings', 'give'),
'type' => 'give_docs_link',
],
[
'type' => 'sectionend',
'id' => 'give_gateway_settings_2',
],
];
if (Utils::isDonationTransactionTypeSupported($merchantDetails->accountCountry ?: '')) {
$settings = give_settings_array_insert(
$settings,
'paypal_commerce_accept_venmo',
[
[
'name' => esc_html__('Transaction Type', 'give'),
'desc' => esc_html__(
'Nonprofits must verify their status to withdraw donations they receive via PayPal. PayPal users that are not verified nonprofits must demonstrate how their donations will be used, once they raise more than $10,000. By default, GiveWP transactions are sent to PayPal as donations. You may change the transaction type using this option if you feel you may not meet PayPal\'s donation requirements.',
'give'
),
'id' => 'paypal_commerce_transaction_type',
'type' => 'radio_inline',
'options' => [
'donation' => esc_html__('Donation', 'give'),
'standard' => esc_html__('Standard Transaction', 'give'),
],
'default' => 'donation',
]
]
);
}
if ($merchantDetails->accountIsReady) {
$settings = give_settings_array_insert(
$settings,
'paypal_commerce_gateway_settings_docs_link',
[
[
'name' => esc_html__('Collect Billing Details', 'give'),
'id' => 'paypal_commerce_collect_billing_details',
'type' => 'radio_inline',
'desc' => esc_html__(
'If enabled, required billing address fields are added to PayPal Donations Donation forms. These fields are required to process the transaction when enabled. Billing address details are added to both the donation and donor record in GiveWP.',
'give'
),
'default' => 'disabled',
'options' => [
'enabled' => esc_html__('Enabled', 'give'),
'disabled' => esc_html__('Disabled', 'give'),
],
],
]
);
}
/**
* filter the settings
*
* @since 2.9.6
*/
return apply_filters('give_get_settings_paypal_commerce', $settings);
}
/**
* @since 2.20.0
* @inerhitDoc
* @throws Exception
*/
public function refundDonation(Donation $donation)
{
throw new Exception('Method has not been implemented yet. Please use the legacy method in the meantime.');
}
/**
* @since 4.0.0
*/
private function shouldUpdateOrder(Donation $donation, $payPalOrder): bool
{
$orderAmount = $payPalOrder->purchase_units[0]->amount->value;
$orderCurrency = $payPalOrder->purchase_units[0]->amount->currency_code;
$currentOrderAmount = Money::fromDecimal($orderAmount, $orderCurrency);
if (!$currentOrderAmount->equals($donation->amount)) {
Log::error(
sprintf(
'Initial PayPal Order amount does not match donation amount. PayPal Order ID: %s, Donation ID: %s',
$payPalOrder->id,
$donation->id
)
);
return true;
}
return false;
}
/**
* @throws PaymentGatewayException
*/
private function validatePayPalOrder(object $payPalOrder): void
{
$transaction = $payPalOrder->purchase_units[0]->payments->captures[0];
$errors = property_exists($payPalOrder, 'details') ? $payPalOrder->details[0] : [];
if (!$transaction) {
throw new PaymentGatewayException('PayPal Order does not have a transaction.');
}
if ($transaction->status === "DECLINED") {
$errorMessage = sprintf(
__('PayPal Order has been declined. Transaction status:: %s', 'give'),
$transaction->status
);
throw new PaymentGatewayException($errorMessage);
}
if (!empty($errors)) {
$errorMessage = sprintf(
__('PayPal Order has an error: %s', 'give'),
$errors->issue[0]->description
);
throw new PaymentGatewayException($errorMessage);
}
$this->validate3dSecure($payPalOrder);
}
/**
* @unreleased
*
* @throws PaymentGatewayException
*/
private function validate3dSecure(object $payPalOrder): void
{
// Check if the order is not ready for 3D Secure authentication
if (isset($payPalOrder->payment_source->card->authentication_result->liability_shift) && !in_array($payPalOrder->payment_source->card->authentication_result->liability_shift, ['POSSIBLE', 'YES'])) {
throw new PaymentGatewayException('Card type and issuing bank are not ready to complete a 3D Secure authentication.');
}
}
}