Skip to content

Commit

Permalink
Add PayPal refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed May 27, 2021
1 parent ec51919 commit fd3a8e9
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions src/Core/Payments/Providers/PayPal.php
Expand Up @@ -2,12 +2,16 @@

namespace GetCandy\Api\Core\Payments\Providers;

use GetCandy\Api\Core\Payments\Models\Transaction;
use GetCandy\Api\Core\Payments\PaymentResponse;
use PayPal\Api\Amount;
use PayPal\Api\Refund;
use PayPal\Api\Capture;
use PayPal\Api\Payment;
use PayPal\Rest\ApiContext;
use PayPal\Api\RefundRequest;
use PayPal\Auth\OAuthTokenCredential;
use GetCandy\Api\Core\Payments\PaymentResponse;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Rest\ApiContext;
use GetCandy\Api\Core\Payments\Models\Transaction;

class PayPal extends AbstractProvider
{
Expand Down Expand Up @@ -135,6 +139,58 @@ protected function getTransactions()

public function refund($token, $amount, $description)
{
try {
$paypalAmount = new Amount;
$paypalAmount->setCurrency("GBP")
->setTotal($amount / 100);

$refundRequest = new RefundRequest;
$refundRequest->setAmount($paypalAmount);

// ### Retrieve Capture paypalAmountdetails
$capture = Capture::get($token, $this->context);

// ### Refund the Capture
$captureRefund = $capture->refundCapturedPayment($refundRequest, $this->context);

$transaction = new Transaction;
$transaction->success = true;
$transaction->refund = true;
$transaction->order()->associate($this->order);
$transaction->merchant = 'N/A';
$transaction->provider = 'PayPal';
$transaction->driver = 'paypal';
$transaction->amount = $captureRefund->amount->total;
$transaction->notes = null;
$transaction->status = $captureRefund->state;
$transaction->card_type = '-';
$transaction->last_four = '-';
$transaction->transaction_id = $captureRefund->capture_id;
$transaction->save();

return $transaction;

} catch (PayPalConnectionException $e) {
$errors = json_decode($e->getData());
$response = new PaymentResponse(false, 'Refund Failed', json_decode($e->getData(), true));

$transaction = new Transaction;
$transaction->success = false;
$transaction->refund = true;
$transaction->order()->associate($this->order);
$transaction->merchant = 'N/A';
$transaction->provider = 'PayPal';
$transaction->driver = 'paypal';
$transaction->amount = $amount;
$transaction->notes = $errors->message;
$transaction->status = $errors->name;
$transaction->card_type = '-';
$transaction->last_four = '-';
$transaction->transaction_id = $token;
$transaction->save();

return $transaction;
}
}

public function getClientToken()
Expand Down

0 comments on commit fd3a8e9

Please sign in to comment.