Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to do refund due to bad requestRazorpay\Api\Errors\ServerError: The server did not send back a well-formed response. #383

Open
rishab1788 opened this issue Dec 29, 2024 · 13 comments

Comments

@rishab1788
Copy link

Steps to reproduce the behavior

Use Refund API -

$payment = $this->razorpay_api->payment->fetch($razorpay_payment_id);
$payment->refund(array("amount" => $amountInSmallestUnit, "speed" => "normal",  "notes"=>array("notes_key_1"=>$refundReason),"receipt" => "Receipt No." . $bookingId));
$refundId = $this->razorpay_api->payment->fetch($razorpay_payment_id)->fetchMultipleRefund()['items'][0]['id'];

here while refund the API fails and give below response it used to work before but now it does not work can some please help me here

Unable to do refund due to bad requestRazorpay\Api\Errors\ServerError: The server did not send back a well-formed response. Server Response: Array in /var/www/html/gearz-vehicle/razorpay-php/src/Request.php:150 Stack trace: #0 /var/www/html/gearz-vehicle/razorpay-php/src/Request.php(219): Razorpay\Api\Request->throwServerError(Array, 404) #1 /var/www/html/gearz-vehicle/razorpay-php/src/Request.php(115): Razorpay\Api\Request->verifyErrorFormat(Array, 404) #2 /var/www/html/gearz-vehicle/razorpay-php/src/Request.php(109): Razorpay\Api\Request->processError(Array, 404, Object(WpOrg\Requests\Response)) #3 /var/www/html/gearz-vehicle/razorpay-php/src/Request.php(58): Razorpay\Api\Request->checkErrors(Object(WpOrg\Requests\Response)) #4 /var/www/html/gearz-vehicle/razorpay-php/src/Entity.php(92): Razorpay\Api\Request->request('POST', 'https://api.raz...', Array) #5 /var/www/html/gearz-vehicle/razorpay-php/src/Entity.php(20): Razorpay\Api\Entity->request('POST', 'refunds/', Array) #6 /var/www/html/gearz-vehicle/razorpay-php/src/Refund.php(17): Razorpay\Api\Entity->create(Array) #7 /var/www/html/gearz-vehicle/razorpay-php/src/Payment.php(52): Razorpay\Api\Refund->create(Array) #8 /var/www/html/gearz-vehicle/includes/classes/payment/OrderRefundProcessor.php(20): Razorpay\Api\Payment->refund(Array) #9 /var/www/html/gearz-vehicle/admin/edit-booking.php(195): OrderRefundProcessor->processRefund('19537', 'pay_PcuYuxicJ8W...', 'boooking succes...', '144') #10
here while refund the API fails and give below response it used to work before but now it does not work can some please help me here

Expected behavior

This api should do the refund instead of throwing error

Actual behavior

thworing error

Code snippets

`Unable to do refund due to bad requestRazorpay\Api\Errors\ServerError: The server did not send back a well-formed response. Server Response: Array in /var/www/html/gearz-vehicle/razorpay-php/src/Request.php:150 Stack trace: #0 /var/www/html/gearz-vehicle/razorpay-php/src/Request.php(219): Razorpay\Api\Request->throwServerError(Array, 404) #1 /var/www/html/gearz-vehicle/razorpay-php/src/Request.php(115): Razorpay\Api\Request->verifyErrorFormat(Array, 404) #2 /var/www/html/gearz-vehicle/razorpay-php/src/Request.php(109): Razorpay\Api\Request->processError(Array, 404, Object(WpOrg\Requests\Response)) #3 /var/www/html/gearz-vehicle/razorpay-php/src/Request.php(58): Razorpay\Api\Request->checkErrors(Object(WpOrg\Requests\Response)) #4 /var/www/html/gearz-vehicle/razorpay-php/src/Entity.php(92): Razorpay\Api\Request->request('POST', 'https://api.raz...', Array) #5 /var/www/html/gearz-vehicle/razorpay-php/src/Entity.php(20): Razorpay\Api\Entity->request('POST', 'refunds/', Array) #6 /var/www/html/gearz-vehicle/razorpay-php/src/Refund.php(17): Razorpay\Api\Entity->create(Array) #7 /var/www/html/gearz-vehicle/razorpay-php/src/Payment.php(52): Razorpay\Api\Refund->create(Array) #8 /var/www/html/gearz-vehicle/includes/classes/payment/OrderRefundProcessor.php(20): Razorpay\Api\Payment->refund(Array) #9 /var/www/html/gearz-vehicle/admin/edit-booking.php(195): OrderRefundProcessor->processRefund('19537', 'pay_PcuYuxicJ8W...', 'boooking succes...', '144') #10
`

Php version

Php v7.2

Library version

2.8.5

Additional Information

No response

@pankajsahukumar
Copy link

I am encountering the same issue with refunds, receiving an 'Array to string conversion' error. This functionality was working fine earlier but has recently started causing this error.

@rishab1788
Copy link
Author

@pankajsahukumar seems like libraby is broken i tried with latest version then also there is same problem for refund I am using now API. you can also use API.
custom code which works in PHP -

<?php
class RazorpayPaymentService
{
    private $apiKey;
    private $apiSecret;

    public function __construct($apiKey, $apiSecret)
    {
        $this->apiKey = $apiKey;
        $this->apiSecret = $apiSecret;
    }

    public function processRefund($paymentId, $refundAmount, $notes = [], $receipt = null)
    {
        $url = "https://api.razorpay.com/v1/payments/$paymentId/refund";

        $data = [
            "amount" => $refundAmount
        ];

        if (!empty($notes)) {
            $data['notes'] = $notes;
        }

        if (!empty($receipt)) {
            $data['receipt'] = $receipt;
        }

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_USERPWD, "{$this->apiKey}:{$this->apiSecret}");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $error = curl_error($ch);
            curl_close($ch);
            return ["success" => false, "message" => "cURL Error: $error"];
        }

        $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        $responseData = json_decode($response, true);

        if ($httpStatus >= 200 && $httpStatus < 300) {
            return ["success" => true, "data" => $responseData];
        } else { 
            return ["success" => false, "message" => $responseData['error']['description'] ?? "Unknown error"];
        }
    }
}

@rishab1788
Copy link
Author

Still some time i am getting this error

@pankajsahukumar
Copy link

actually package is not broken there api server is throwing an error which is not compatible with the package error handler

@raguventhan
Copy link

Am also getting same error while doing refund

@nptugoz
Copy link

nptugoz commented Jan 6, 2025

Looks like PHP SDK is using a request URL (v1/refund/create) that is no longer supported. It should be /v1/payments/:id/refund.

You can replace the refund function in src/Payment.php with the following:

    public function refund($attributes = array())
    {
        $relativeUrl = $this->getEntityUrl() . $this->id . '/refund';
        return $this->request('POST', $relativeUrl, $attributes);
    }

@raguventhan
Copy link

@nptugoz tried your suggestion but its not working.

@rishab1788
Copy link
Author

@raguventhan even the API solution didn't work for you?

@nptugoz
Copy link

nptugoz commented Jan 7, 2025

@nptugoz tried your suggestion but its not working.

@raguventhan It is working for me. Are you using $api->payment->fetch($payment_id)->refund();? If so, this fix should work. Let me know if you are using a different method.

@raguventhan
Copy link

@nptugoz am using this $rzpApi->payment ->fetch( $paymentId )->refund( $data );

@raguventhan
Copy link

raguventhan commented Jan 8, 2025

@rishab1788 the curl approach is working

@ankitdas13
Copy link
Contributor

@rishab1788 This issue has been fixed, you don't have to do any changes.

Could you please check once from your end and let us know?

@jaganax
Copy link

jaganax commented Jan 13, 2025

@ankitdas13, can we know what went wrong and what is fixed? just for information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants