Monday, April 7, 2014

Create Refund Using PayPal Api

REST API Reference - PayPal

Create a file such named 'createRefund.php' using following contents:


<?php
session_start();
require './PaypalBase.php';
$paymentId = "";
$amount = 0;
if(isset($_GET["paymentId"]) && PaypalBase::isValidString($_GET["paymentId"])) {
    $paymentId = $_GET["paymentId"]; 
    $dataArray = array();
    if(isset($_GET["amount"]) && PaypalBase::isValidString($_GET["amount"]) 
            && floatval($_GET["amount"]) > 0) {
        $amount = floatval($_GET["amount"]);
        $dataArray = array(
            "amount" => array(
                "total" => floatval($_GET["amount"]),
                "currency" => "USD"
            )
        );
    }
    $refundArray = PaypalBase::createRefund($paymentId, $dataArray);
    PaypalBase::printR($refundArray);
    if($refundArray != NULL) {
        return;
    } else {
        echo "<br/><b>Error in refund</b>";
    }
}
?>
<form>
    Payment: <input type="text" name="paymentId" value="<?php echo $paymentId; ?>"/><br/>
    Amount: <input type="text" name="amount" value="<?php echo $amount; ?>"/><br/>
    <input type="submit" value="Process..."/>
</form>

createRefund method from 'PaypalBase.php'


<?php
public static function createRefund($paymentId, $dataArray = NULL) {
    self::info("createRefund");
    self::info("-----------------------------------------------");
    self::info("-----------------------------------------------");
    $accessToken = self::generateAccessToken(PPConstants::CLIENT_ID, PPConstants::CLIENT_SECRET);
    if(!$accessToken) {
        throw new Exception("Creating accessToken failed");
    }
    self::info("AccessToken: " . $accessToken);
    if(!$paymentId || strlen(trim($paymentId)) == 0) {
        throw new Exception("paymentId required");
    }
    self::info("Creating refund: $paymentId");

    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer '.$accessToken,
        "PayPal-Request-Id" => self::generateRequestId(),
        "User-Agent" => self::getUserAgent()
    );
    if($dataArray != NULL && is_array($dataArray) && count($dataArray) > 0) {
        array_push($headers, "Content-Length: " . strlen(json_encode($dataArray)));
        self::info($dataArray);
    }
    if($dataArray != NULL && !is_array($dataArray) && strlen($dataArray) > 0) {
        array_push($headers, "Content-Length: " . strlen($dataArray));
        self::info($dataArray);
    }
    $paymentUrl = PPConstants::REST_SANDBOX_ENDPOINT.
            PPConstants::PAYMENT_GET_URL.$paymentId.PPConstants::PAYMENT_REFUND_URL;
    if(!self::$testMode) {
        $paymentUrl = PPConstants::REST_LIVE_ENDPOINT.
                PPConstants::PAYMENT_GET_URL.$paymentId.PPConstants::PAYMENT_REFUND_URL;
    }
    self::info("Payment url:$paymentUrl");
    $result = self::makeCurlCall($paymentUrl, "POST", json_encode($dataArray), NULL, $headers);
    $returnResult = NULL;
    if($result != NULL && is_array($result) 
            && isset($result["code"]) && isset($result["response"])) {
        $result = json_decode($result["response"]);
        if($result != NULL) {
            $returnResult = $result;
        }
    }
    self::info("Result: ");
    if($returnResult) {
        self::info($returnResult);
    } else {
        self::info($result);
    }
    return $returnResult;
}
?>

Output would be like this from valid refund:


stdClass Object
(
    [id] => 3E1338310M1546933
    [create_time] => 2014-04-07T13:08:52Z
    [update_time] => 2014-04-07T13:08:52Z
    [state] => completed
    [amount] => stdClass Object
        (
            [total] => 20.00
            [currency] => USD
        )

    [sale_id] => 6RE41122CN245801J
    [parent_payment] => PAY-99S90613BC814724UKNBKCVI
    [links] => Array
        (
            [0] => stdClass Object
                (
                    [href] => https://api.sandbox.paypal.com/v1/payments/refund/3E1338310M1546933
                    [rel] => self
                    [method] => GET
                )

            [1] => stdClass Object
                (
                    [href] => https://api.sandbox.paypal.com/v1/payments/payment/PAY-99S90613BC814724UKNBKCVI
                    [rel] => parent_payment
                    [method] => GET
                )

            [2] => stdClass Object
                (
                    [href] => https://api.sandbox.paypal.com/v1/payments/sale/6RE41122CN245801J
                    [rel] => sale
                    [method] => GET
                )

        )

)

Output would be like this from invalid refund:


stdClass Object
(
    [name] => TRANSACTION_REFUSED
    [message] => The request was refused.{0}
    [information_link] => https://developer.paypal.com/webapps/developer/docs/api/#TRANSACTION_REFUSED
    [debug_id] => 58299c9502f28
)

No comments:

Post a Comment