|
2 | 2 |
|
3 | 3 | namespace GetCandy\Api\Core\Payments\Providers; |
4 | 4 |
|
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; |
7 | 8 | use PayPal\Api\Payment; |
| 9 | +use PayPal\Rest\ApiContext; |
| 10 | +use PayPal\Api\RefundRequest; |
8 | 11 | use PayPal\Auth\OAuthTokenCredential; |
| 12 | +use GetCandy\Api\Core\Payments\PaymentResponse; |
9 | 13 | use PayPal\Exception\PayPalConnectionException; |
10 | | -use PayPal\Rest\ApiContext; |
| 14 | +use GetCandy\Api\Core\Payments\Models\Transaction; |
11 | 15 |
|
12 | 16 | class PayPal extends AbstractProvider |
13 | 17 | { |
@@ -135,6 +139,58 @@ protected function getTransactions() |
135 | 139 |
|
136 | 140 | public function refund($token, $amount, $description) |
137 | 141 | { |
| 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 | + } |
138 | 194 | } |
139 | 195 |
|
140 | 196 | public function getClientToken() |
|
0 commit comments