Problem with transaction id

Jordivid
Contributor
Contributor

Greetings to all.

 

I am new to web programming. Right now I am finishing my first demo online shop using javascript and php, which I intend to use in a portfolio. I decided to use Paypal sandbox to include Paypal express checkout as the payment method. 

 

So I implemented the createOrder and onApprove functions. Inside the onApprove function I perform the funds capture, which returns the transaction id. I associate this id to the customer's order and save it into database. If the order is cancelled I use this transaction id to issue a refund request from Paypal, and it is here when the problem arises. I have coded this refund request in 2 ways:

 

1) Using orders API:

   $amt = new Amount();

   //This param contains the amount to refund
   $amt->setTotal($_POST["amount"])->setCurrency('EUR');

   $refund = new Refund();
   $refund->setAmount($amt);

   $sale = new Sale();

   //This param contains the database stored transaction id
   $sale->setId($_POST["paypalID"]);

   try {
      $refundedSale = $sale->refund($refund, $apiContext);
   } catch (PayPal\Exception\PayPalConnectionException $ex) {
        $err = $notify.$ex->getMessage();
   } catch (Exception $ex) {
        $err = $notify.$ex->getMessage();
   }

 

2) Using payments API:

class RefundOrder
{

  public static function refundOrder($captureId, $debug=false)
  {
    $request = new CapturesRefundRequest($captureId);
    $request->body = self::buildRequestBody();

    $client = PayPalClient::client();
    $response = $client->execute($request);

    return $response;

  }

  public static function buildRequestBody()
  {
    return array( 'amount' => array('value' => '0.10', 'currency_code' => 'EUR'));
  }
}

 

RefundOrder::refundOrder('DATABASE-STORED-TRANSACTION-ID', true);

 

The problem is that in both cases Paypal does not accept the transaction id returned by the capture. When I inspect personal and bussiness sandbox accounts both show different id's. I am aware that both id's are related to the same transaction, but as I've seen I need to get the id shown on the bussines account to perform the refund: if I manually replace the Paypal id on the database with that on the bussiness account then when I cancel an order in my app it performs the refund correctly.

 

I've carefully reviewed the params returned by the capture, and searched in forums and faqs, but I can't find anything related to this subject. Summed up, the funds capture returns an id which I can't use in a refund, but there must be a way to get the id I need. I guess I must have done something wrong or either I am missing something, could anyone please help?

 

Thanks for reading.

Login to Me Too
1 REPLY 1

MTS_Jennifer
Moderator
Moderator

Hello,

Thank you for posting to the PayPal Sandbox Community.

You are correct that you need to use the Transaction ID from the Business Account for the completed payment to perform a refund request.

You will want to make sure that your database is not storing any spaces or special characters with the transaction id. Also if the transaction was not a captured payment then you cannot refund it. The only option is to void the transaction.

You may want to perform a var dump to determine if your database is passing the correct transaction id.

If you comment your merchant id for your business sandbox account, I can look at the back end logs and see what is being passed in when you attempt to refund the payment.

Thank you,

Jennifer

PayPal

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.