Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1. // ----------------------------------------
  2. // Primeiro passo
  3.  
  4. try {
  5.  
  6.     $refund = Payment::refund($invoice->payment_id);
  7.  
  8.     if ($refund) {
  9.         $invoice->refund_id = $refund->id;
  10.         $invoice->refund_at = now();
  11.         $invoice->save();
  12.        
  13.     } else {
  14.         return response("Não foi possível cancelar o pedido.", Response::HTTP_INTERNAL_SERVER_ERROR);
  15.     }
  16.  
  17. } catch (MercadoPagoException $e) {
  18.  
  19.     if ($e->getCause()) {
  20.         Log::info("[invoice refund]: #".$invoice->id." --> ".json_encode($e->getCause()));
  21.     }
  22.  
  23.     return response("Não foi possível cancelar o pedido.", Response::HTTP_INTERNAL_SERVER_ERROR);
  24. }
  25.  
  26. // ----------------------------------------
  27. // Segundo passo
  28.  
  29. public function refund($id) {
  30.        
  31.     $payment = MpPayment::find($id);
  32.  
  33.     if ($payment) {
  34.        
  35.         // Realizar reembolso
  36.         if ($payment->status == "approved") {
  37.         return $payment->refund();
  38.        
  39.         // Realizar cancelamento
  40.         } else {
  41.         return $payment->cancel();
  42.         }
  43.     }
  44. }
  45.  
  46. // ----------------------------------------
  47. // Terceiro passo
  48.  
  49. public function refund() {
  50.        
  51.     $url = "https://api.mercadopago.com/v1/payments/".$this->id."/refunds";
  52.     $data = self::request("POST", $url);
  53.  
  54.     return $data;
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement