-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHTTPRequestDeferredPaymentAuth.php
139 lines (119 loc) · 5.24 KB
/
HTTPRequestDeferredPaymentAuth.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?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\DeferredPaymentAuth as AfterpayDeferredPaymentAuthRequest;
/**
* 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 "Deferred Payment Auth" Request. Some of the
* important components of the Response will then be rendered above the HTML form.
*/
$merchant = null;
$error = null;
$order = null;
/**
* Remember, if you have not configured merchant credentials in your .env.php file, you can specify them
* manually for every request. Uncomment the following lines and replace the "MERCHANT_ID" and "SECRET_KEY"
* placeholders with your Sandbox credentials to use this method.
*/
/*$merchant = new Afterpay\SDK\MerchantAccount();
$merchant
->setMerchantId('MERCHANT_ID')
->setSecretKey('SECRET_KEY')
;*/
if (! empty($_POST)) {
$createCheckoutRequest = new AfterpayCreateCheckoutRequest([
'amount' => [ '200', 'AUD' ],
'consumer' => [ 'email' => '[email protected]' ],
'merchant' => [
'redirectConfirmUrl' => $_POST['redirectReturnUrl'],
'redirectCancelUrl' => $_POST['redirectReturnUrl']
]
]);
if (!is_null($merchant)) {
$createCheckoutRequest
->setMerchantAccount($merchant)
;
}
$createCheckoutRequest->send();
$createCheckoutResponse = $createCheckoutRequest->getResponse();
$obj = $createCheckoutResponse->getParsedBody();
if ($createCheckoutResponse->isSuccessful()) {
header('Location: ' . $obj->redirectCheckoutUrl);
} else {
$error = $obj;
}
} elseif (! empty($_GET)) {
$deferredPaymentAuthRequest = new AfterpayDeferredPaymentAuthRequest([
'token' => urlencode($_GET['orderToken'])
]);
if (!is_null($merchant)) {
$deferredPaymentAuthRequest
->setMerchantAccount($merchant)
;
}
$deferredPaymentAuthRequest->send();
$deferredPaymentAuthResponse = $deferredPaymentAuthRequest->getResponse();
$obj = $deferredPaymentAuthResponse->getParsedBody();
if ($deferredPaymentAuthResponse->isSuccessful()) {
$order = $obj;
} else {
$error = $obj;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Deferred Payment Auth Request Sample</title>
</head>
<body>
<?php if ($error) : ?>
<h3>Error</h3>
<pre><?php print_r($error); ?></pre>
<p><a href="HTTPRequestDeferredPaymentAuth.php">Try again</a></p>
<?php elseif ($order) : ?>
<h3>Order Record Created</h3>
<ul>
<li>Order ID: <?php echo $order->id; ?></li>
<li>Status: <?php echo $order->status; ?></li>
<li>Is Approved? <?php echo $deferredPaymentAuthRequest->getResponse()->isApproved() ? 'YES - Proceed to thank you page.' : 'NO - Return to checkout with payment declined error.'; ?></li>
</ul>
<?php if ($deferredPaymentAuthRequest->getResponse()->isApproved()) : ?>
<p><a href="HTTPRequestDeferredPaymentCapture.php?orderId=<?php echo $order->id; ?>">Capture Payment for this order</a></p>
<p><a href="HTTPRequestDeferredPaymentVoid.php?orderId=<?php echo $order->id; ?>">Void Payment for this order</a></p>
<?php else : ?>
<p><a href="HTTPRequestDeferredPaymentAuth.php">Start again</a></p>
<?php endif; ?>
<?php else : ?>
<h3>Deferred Payment Auth</h3>
<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>
<?php endif; ?>
</body>
</html>