-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHTTPRequestImmediatePaymentCapture.php
More file actions
94 lines (83 loc) · 4.02 KB
/
HTTPRequestImmediatePaymentCapture.php
File metadata and controls
94 lines (83 loc) · 4.02 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* @copyright Copyright (c) 2020-2021 Afterpay Corporate Services Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
$composer_autoload = __DIR__ . '/../vendor/autoload.php';
if (file_exists($composer_autoload)) {
require_once $composer_autoload;
} else {
require_once __DIR__ . '/../test/autoload.php';
}
use Afterpay\SDK\HTTP\Request\CreateCheckout as AfterpayCreateCheckoutRequest;
use Afterpay\SDK\HTTP\Request\ImmediatePaymentCapture as AfterpayImmediatePaymentCaptureRequest;
/**
* This sample builds an HTML form to simulate the checkout page of an e-commerce platform.
* A click on the "Proceed to Afterpay" button posts to the current page, which calls the "Create Checkout" API
* (satisfying only the bare minimum technical requirements) and then (in the event of success) redirects to
* the Afterpay Checkout URL. If you navigate through the consumer payment flow and click "confirm" to commit
* to the payment schedule, you should return to this page with new query parameters appended to the URL.
* The "orderToken" parameter will then be used to submit an "Immediate Payment Capture" Request. Some of the
* important components of the Response will then be rendered above the HTML form.
*/
$error = null;
$order = null;
if (! empty($_POST)) {
$createCheckoutRequest = new AfterpayCreateCheckoutRequest([
'amount' => [ '200', 'AUD' ],
'consumer' => [ 'email' => 'nobody@example.com' ],
'merchant' => [
'redirectConfirmUrl' => $_POST['redirectReturnUrl'],
'redirectCancelUrl' => $_POST['redirectReturnUrl']
]
]);
if ($createCheckoutRequest->send()) {
header('Location: ' . $createCheckoutRequest->getResponse()->getParsedBody()->redirectCheckoutUrl);
} else {
$error = $createCheckoutRequest->getResponse()->getParsedBody();
}
} elseif (! empty($_GET)) {
$immediatePaymentCaptureRequest = new AfterpayImmediatePaymentCaptureRequest([
'token' => urlencode($_GET['orderToken'])
]);
if ($immediatePaymentCaptureRequest->send()) {
$order = $immediatePaymentCaptureRequest->getResponse()->getParsedBody();
} else {
$error = $immediatePaymentCaptureRequest->getResponse()->getParsedBody();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Immediate Payment Capture Request Sample</title>
</head>
<body>
<?php if ($error) : ?>
<pre><?php print_r($error); ?></pre>
<?php elseif ($order) : ?>
<h3>Order Record Created</h3>
<ul>
<li>ID: <?php echo $order->id; ?></li>
<li>Status: <?php echo $order->status; ?></li>
<li>Is Approved? <?php echo $immediatePaymentCaptureRequest->getResponse()->isApproved() ? 'YES - Proceed to thank you page.' : 'NO - Return to checkout with payment declined error.'; ?></li>
</ul>
<p><em>Note: Payment is only captured if the status of the order record is approved. You can check this with <code>Afterpay\SDK\HTTP\Response\ImmediatePaymentCapture::isApproved</code>.</em></p>
<?php endif; ?>
<form method="POST">
<div>Return here after checkout: <input type="text" name="redirectReturnUrl" value="<?php echo 'http' . ((! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 's' : '') . '://' . htmlspecialchars($_SERVER['HTTP_HOST']) . (strstr($_SERVER['REQUEST_URI'], '?') ? substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')) : $_SERVER['REQUEST_URI']); ?>"></div>
<div><button type="submit">Proceed to Afterpay</button></div>
</form>
</body>
</html>