-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHTTPRequestDeferredPaymentCapture.php
115 lines (100 loc) · 4.37 KB
/
HTTPRequestDeferredPaymentCapture.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
<?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\Helper\StringHelper as AfterpayStringHelper;
use Afterpay\SDK\Model\Payment as AfterpayPayment;
use Afterpay\SDK\HTTP\Request\DeferredPaymentCapture as AfterpayDeferredPaymentCaptureRequest;
/**
* This sample demonstrates capturing a full or partial payment for an open auth.
* Before you can capture a payment, you need to have created a checkout, amd for the consumer to have
* completed the checkout screenflow and confirmed the payment schedule.
* See HTTPRequestDeferredPaymentAuth.php for a sample that satisfies these prerequisites.
*
* A typical use case for partial payment capture is where a shipment is despatched for a portion of an
* order. For a more detailed explanation and additional use cases, see:
* - https://developers.afterpay.com/afterpay-online/reference#deferred-payment-flow
*/
$merchant = null;
$error = null;
$paymentEvent = 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)) {
$capturePaymentRequest = new AfterpayDeferredPaymentCaptureRequest([
'requestId' => $_POST[ 'requestId' ],
'amount' => [ $_POST[ 'amount' ][ 'amount' ], $_POST[ 'amount' ][ 'currency' ] ]
]);
$capturePaymentRequest->setOrderId($_POST[ 'orderId' ]);
if (!is_null($merchant)) {
$capturePaymentRequest
->setMerchantAccount($merchant)
;
}
if ($capturePaymentRequest->send()) {
$order = new AfterpayPayment($capturePaymentRequest->getResponse()->getParsedBody());
$paymentEvent = $capturePaymentRequest->getResponse()->getPaymentEvent();
} else {
$error = $capturePaymentRequest->getResponse()->getParsedBody();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Deferred Payment Capture Request Sample</title>
</head>
<body>
<?php if ($error) : ?>
<h3>Error</h3>
<pre><?php print_r($error); ?></pre>
<p><a href="HTTPRequestDeferredPaymentAuth.php">Try a new auth</a></p>
<?php elseif ($paymentEvent) : ?>
<h3>Payment Capture Successful</h3>
<ul>
<li>Capture Event ID: <?php echo $paymentEvent->getId(); ?></li>
<li>Timestamp: <?php echo $paymentEvent->getCreated(); ?></li>
<li>Open to Capture: <?php echo $order->getOpenToCaptureAmount()->getAmount(); ?></li>
<li>Auth Expiry: <?php echo $order->getEvents()[0]->getExpires(); ?></li>
</ul>
<p><a href="HTTPRequestDeferredPaymentVoid.php?orderId=<?php echo urlencode($_GET['orderId']) ?>">Void Payment for this order</a></p>
<p><a href="HTTPRequestDeferredPaymentAuth.php">Start again</a></p>
<?php endif; ?>
<h3>Deferred Payment Capture</h3>
<form method="POST">
<p>Path params:</p>
<div>Order ID: <input type="text" name="orderId" value="<?php echo urlencode($_GET['orderId']) ?>"></div>
<p>Body params:</p>
<div>Request ID: <input type="text" name="requestId" value="<?php echo AfterpayStringHelper::generateUuid(); ?>"></div>
<div>Amount: <input type="text" name="amount[amount]" value="200.00"></div>
<div>Currency: <input type="text" name="amount[currency]" value="AUD"></div>
<div><button type="submit">Submit</button></div>
</form>
</body>
</html>