summaryrefslogtreecommitdiffstats
path: root/vendor/paypal/paypal-checkout-sdk/samples/RefundOrder.php
blob: fc9428fd5e32655f0b2b9caf23b8782dfb6b949e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace Sample;

require __DIR__ . '/../vendor/autoload.php';
use Sample\PayPalClient;
use PayPalCheckoutSdk\Payments\CapturesRefundRequest;

class RefundOrder
{

    /**
     * Function to create an refund capture request. Payload can be updated to issue partial refund.
     */
    public static function buildRequestBody()
    {
        return array(
            'amount' =>
                array(
                    'value' => '20.00',
                    'currency_code' => 'USD'
                )
        );
    }

    /**
     * This function can be used to preform refund on the capture. 
     */
    public static function refundOrder($captureId, $debug=false)
    {
        $request = new CapturesRefundRequest($captureId);
        $request->body = self::buildRequestBody();
        $client = PayPalClient::client();
        $response = $client->execute($request);

        if ($debug)
        {
            print "Status Code: {$response->statusCode}\n";
            print "Status: {$response->result->status}\n";
            print "Order ID: {$response->result->id}\n";
            print "Links:\n";
            foreach($response->result->links as $link)
            {
                print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
            }
            // To toggle printing the whole response body comment/uncomment below line
            echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
        }
        return $response;
    }
}

/**
 * This is the driver function which invokes the refund capture function with
 * Capture Id to perform refund on capture.
 */
if (!count(debug_backtrace()))
{
    RefundOrder::refundOrder('8XL09935J2224701N', true);
}