-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathrefund-payment.php
71 lines (63 loc) · 2.71 KB
/
refund-payment.php
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
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
* How to refund a payment programmatically
*/
try {
/*
* Initialize the Mollie API library with your API key.
*
* See: https://www.mollie.com/dashboard/developers/api-keys
*/
require "../initialize.php";
/*
* Determine the url parts to these example files.
*/
$protocol = isset($_SERVER['HTTPS']) && strcasecmp('off', $_SERVER['HTTPS']) !== 0 ? "https" : "http";
$hostname = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER['REQUEST_URI'] ?? $_SERVER['PHP_SELF']);
if (isset($_GET['payment_id'])) {
/*
* Retrieve the payment you want to refund from the API.
*/
$paymentId = $_GET['payment_id'];
$payment = $mollie->payments->get($paymentId);
if ($payment->canBeRefunded() && $payment->amountRemaining->currency === 'EUR' && $payment->getAmountRemaining() >= 2.00) {
/*
* Refund € 2,00 of the payment.
*
* https://docs.mollie.com/reference/v2/refunds-api/create-refund
*/
$refund = $payment->refund([
"amount" => [
"currency" => "EUR",
"value" => "2.00", // You must send the correct number of decimals, thus we enforce the use of strings
],
]);
echo "{$refund->amount->currency} {$refund->amount->value} of payment {$paymentId} refunded.", PHP_EOL;
} else {
echo "Payment {$paymentId} can not be refunded.", PHP_EOL;
}
/*
* Retrieve all refunds on a payment.
*/
echo "<ul>";
foreach ($payment->refunds() as $refund) {
echo "<li>";
echo "<strong style='font-family: monospace'>" . htmlspecialchars($refund->id) . "</strong><br />";
echo htmlspecialchars($refund->description) . "<br />";
echo htmlspecialchars($refund->amount->currency) . " " . htmlspecialchars($refund->amount->value) . "<br />";
echo "Status: " . htmlspecialchars($refund->status);
echo "</li>";
}
echo "</ul>";
}
echo "Refund payment: ";
echo "<form method='get'><input name='payment_id' value='tr_xxx'/><input type='submit' /></form>";
echo "<p>";
echo '<a href="' . $protocol . '://' . $hostname . $path . '/create-payment.php">Create a payment</a><br>';
echo '<a href="' . $protocol . '://' . $hostname . $path . '/create-ideal-payment.php">Create an iDEAL payment</a><br>';
echo '<a href="' . $protocol . '://' . $hostname . $path . '/list-payments.php">List payments</a><br>';
echo "</p>";
} catch (\Mollie\Api\Exceptions\ApiException $e) {
echo "API call failed: " . htmlspecialchars($e->getMessage());
}