Refund a completed payment by passing the sale_id in the request URI. In addition, include an empty JSON payload in the request body for a full refund. For a partial refund, include an amount object in the request body.
public refund ( Refund $refund, |
||
$refund | Refund | |
$apiContext | is the APIContext for this call. It can be used to pass dynamic configuration and credentials. | |
$restCall | PayPalRestCall | is the Rest Call Service that is used to make rest calls |
Результат | Refund |
use PayPal\Api\Sale; // ### Refund amount // Includes both the refunded amount (to Payer) // and refunded fee (to Payee). Use the $amt->details // field to mention fees refund details. $amt = new Amount(); $amt->setCurrency('USD')->setTotal(0.01); // ### Refund object $refund = new Refund(); $refund->setAmount($amt); // ###Sale // A sale transaction. // Create a Sale object with the // given sale transaction id. $sale = new Sale(); $sale->setId($saleId); try { // Create a new apiContext object so we send a new // PayPal-Request-Id (idempotency) header for this resource $apiContext = getApiContext($clientId, $clientSecret); // Refund the sale // (See bootstrap.php for more on `ApiContext`) $refundedSale = $sale->refund($refund, $apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("Refund Sale", "Sale", $refundedSale->getId(), $refund, $ex); exit(1); } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printResult("Refund Sale", "Sale", $refundedSale->getId(), $refund, $refundedSale); return $refundedSale;
public function refundSale($saleId, $refundAmount, $currency = CurrencyConst::EURO) { $apiContext = $this->connectionService->getApiContext(); $amount = new Amount(); $amount->setCurrency($currency)->setTotal($refundAmount); $refund = new Refund(); $refund->setAmount($amount); $sale = new Sale(); $sale->setId($saleId); $refundSale = $sale->refund($refund, $apiContext); return $refundSale; }
$amt = new Amount(); $amt->setCurrency('USD'); $amt->setTotal('0.01'); $refund = new Refund(); $refund->setAmount($amt); $saleId = '3RM92092UW5126232'; // ###Sale // A sale transaction. // Create a Sale object with the // given sale transaction id. $sale = new Sale(); $sale->setId($saleId); try { // Refund the sale // (See bootstrap.php for more on `ApiContext`) $sale->refund($refund, $apiContext); } catch (\PPConnectionException $ex) { echo "Exception:" . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); exit(1); } ?> <html> <body> <div>Refunding sale id: <?php echo $saleId; ?> </div> <pre><?php var_dump($sale); ?>
/** * @dataProvider mockProvider * @param Sale $obj */ public function testRefund($obj, $mockApiContext) { $mockPPRestCall = $this->getMockBuilder('\\PayPal\\Transport\\PayPalRestCall')->disableOriginalConstructor()->getMock(); $mockPPRestCall->expects($this->any())->method('execute')->will($this->returnValue(RefundTest::getJson())); $refund = RefundTest::getObject(); $result = $obj->refund($refund, $mockApiContext, $mockPPRestCall); $this->assertNotNull($result); }