Skip to content

Commit fd3a8e9

Browse files
committed
Add PayPal refunds
1 parent ec51919 commit fd3a8e9

1 file changed

Lines changed: 59 additions & 3 deletions

File tree

src/Core/Payments/Providers/PayPal.php

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace GetCandy\Api\Core\Payments\Providers;
44

5-
use GetCandy\Api\Core\Payments\Models\Transaction;
6-
use GetCandy\Api\Core\Payments\PaymentResponse;
5+
use PayPal\Api\Amount;
6+
use PayPal\Api\Refund;
7+
use PayPal\Api\Capture;
78
use PayPal\Api\Payment;
9+
use PayPal\Rest\ApiContext;
10+
use PayPal\Api\RefundRequest;
811
use PayPal\Auth\OAuthTokenCredential;
12+
use GetCandy\Api\Core\Payments\PaymentResponse;
913
use PayPal\Exception\PayPalConnectionException;
10-
use PayPal\Rest\ApiContext;
14+
use GetCandy\Api\Core\Payments\Models\Transaction;
1115

1216
class PayPal extends AbstractProvider
1317
{
@@ -135,6 +139,58 @@ protected function getTransactions()
135139

136140
public function refund($token, $amount, $description)
137141
{
142+
try {
143+
$paypalAmount = new Amount;
144+
$paypalAmount->setCurrency("GBP")
145+
->setTotal($amount / 100);
146+
147+
$refundRequest = new RefundRequest;
148+
$refundRequest->setAmount($paypalAmount);
149+
150+
// ### Retrieve Capture paypalAmountdetails
151+
$capture = Capture::get($token, $this->context);
152+
153+
// ### Refund the Capture
154+
$captureRefund = $capture->refundCapturedPayment($refundRequest, $this->context);
155+
156+
$transaction = new Transaction;
157+
$transaction->success = true;
158+
$transaction->refund = true;
159+
$transaction->order()->associate($this->order);
160+
$transaction->merchant = 'N/A';
161+
$transaction->provider = 'PayPal';
162+
$transaction->driver = 'paypal';
163+
$transaction->amount = $captureRefund->amount->total;
164+
$transaction->notes = null;
165+
$transaction->status = $captureRefund->state;
166+
$transaction->card_type = '-';
167+
$transaction->last_four = '-';
168+
$transaction->transaction_id = $captureRefund->capture_id;
169+
$transaction->save();
170+
171+
return $transaction;
172+
173+
} catch (PayPalConnectionException $e) {
174+
$errors = json_decode($e->getData());
175+
$response = new PaymentResponse(false, 'Refund Failed', json_decode($e->getData(), true));
176+
177+
$transaction = new Transaction;
178+
$transaction->success = false;
179+
$transaction->refund = true;
180+
$transaction->order()->associate($this->order);
181+
$transaction->merchant = 'N/A';
182+
$transaction->provider = 'PayPal';
183+
$transaction->driver = 'paypal';
184+
$transaction->amount = $amount;
185+
$transaction->notes = $errors->message;
186+
$transaction->status = $errors->name;
187+
$transaction->card_type = '-';
188+
$transaction->last_four = '-';
189+
$transaction->transaction_id = $token;
190+
$transaction->save();
191+
192+
return $transaction;
193+
}
138194
}
139195

140196
public function getClientToken()

0 commit comments

Comments
 (0)