Search code examples
phplaravelpaypal

Paypal Refund amount using laravel


I have successfully implement PayPal with laravel. I am able to make payment using PayPal APIs. Now I want to implement refund from PayPal using laravel. I don't want to make refund from PayPal dashboard but i want to make it done from our admin panel where admin user can see list of payments and link to refund that amount to customer. After successful payment I got Transaction id in response from PayPal. If i use that transaction id to refund then i am not able to do that successfully, and if i copy transaction id from paypal dashboard where all transactions are listed and get detail information of that transaction, then i am able to make refund success. Can anyone tell me how can i get that transaction id using which i can make refund using laravel.


Solution

  • I have to use below code to get actual sale id to refund amount using code not from paypal dashboard

    $apiContext = new ApiContext(new OAuthTokenCredential(
                    "<CLIENT_ID>", "<CLIENT_SCRET_KEY>")
            );
    $payments = Payment::get($payment_id, $apiContext);
    $payments->getTransactions();
    $obj = $payments->toJSON();//I wanted to look into the object
    $paypal_obj = json_decode($obj);//I wanted to look into the object
    $transaction_id = $paypal_obj->transactions[0]->related_resources[0]->sale->id;
    $this->getRefundPayment($transaction_id);
    
    public function getRefundPayment($transaction_id){
        $this->API_UserName  = urlencode($this->API_Username);
        $this->API_Password  = urlencode($this->API_Password);
        $this->API_Signature = urlencode($this->Signature);
        $this->version = urlencode($this->version);
        $transactionid = $transaction_id;
        $DataInArray['currencyCode'] = 'INR';
        $DataInArray['refundType'] = 'Full';
        $DataInArray['transactionID'] = $transactionid;
        $DataInArray['invoiceID'] = '';
        $DataInArray['memo'] = 'This is refund of transaction id ='.$transactionid;
        $DataInArray['amount'] = '';
        if(trim(@$DataInArray['currencyCode'])=="")
            return array("ERROR_MESSAGE"=>"Currency Code is Missing");
        if(trim(@$DataInArray['refundType'])=="")
            return array("ERROR_MESSAGE"=>"Refund Type is Missing");
        if(trim(@$DataInArray['transactionID'])=="")
            return array("ERROR_MESSAGE"=>"Transaction ID is Missing");
        $Api_request = "&TRANSACTIONID={$DataInArray['transactionID']}&REFUNDTYPE={$DataInArray['refundType']}&CURRENCYCODE={$DataInArray['currencyCode']}";
        if(trim(@$DataInArray['invoiceID'])!="")
            $Api_request = "&INVOICEID={$DataInArray['invoiceID']}";
        if(isset($DataInArray['memo']))
            $Api_request .= "&NOTE={$DataInArray['memo']}";
        if(strcasecmp($DataInArray['refundType'], 'Partial') == 0)    {
            if(!isset($DataInArray['amount']))   {
                return array("ERROR_MESSAGE"=>"For Partial Refund - It is essential to mention Amount");
            }    else     {
                $Api_request = $Api_request."&AMT={$DataInArray['amount']}";
            }
            if(!isset($DataInArray['memo']))   {
                return array("ERROR_MESSAGE"=>"For Partial Refund - It is essential to enter text for Memo");
            }
        }                      
        $curl_var = curl_init();
        curl_setopt($curl_var, CURLOPT_URL, $this->API_Endpoint);
        curl_setopt($curl_var, CURLOPT_VERBOSE, 1);
        curl_setopt($curl_var, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl_var, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl_var, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_var, CURLOPT_POST, 1);
        $Api_request_final = "METHOD=RefundTransaction&VERSION={$this->version}&PWD={$this->API_Password}&USER={$this->API_UserName}&SIGNATURE={$this->API_Signature}$Api_request";
        curl_setopt($curl_var, CURLOPT_POSTFIELDS, $Api_request_final);
        // Get response from the server.
        $curlResponse = curl_exec($curl_var);
        if(!$curlResponse)
            return array("ERROR_MESSAGE"=>"RefundTransaction failed".curl_error($curl_var)."(".curl_errno($curl_var).")");
        // Extract the response details.
        $httpResponseAr = explode("&", $curlResponse);
        $aryResponse = array();
        foreach ($httpResponseAr as $i => $value)     {
            $tmpAr = explode("=", $value);
            if(sizeof($tmpAr) > 1)   {
                $aryResponse[$tmpAr[0]] = urldecode($tmpAr[1]);
            }
        }
        if((0 == sizeof($aryResponse)) || !array_key_exists('ACK', $aryResponse))
            return array("ERROR_MESSAGE"=>"Invalid HTTP Response for POST request ($reqStr) to {$this->API_Endpoint}");
       // var_dump($aryResponse);
        return $aryResponse;
    }