This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
AuthorizeRequest.php
81 lines (69 loc) · 2.09 KB
/
AuthorizeRequest.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
<?php
declare(strict_types=1);
namespace MyOnlineStore\Omnipay\KlarnaCheckout\Message;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Common\Http\Exception\NetworkException;
use Omnipay\Common\Http\Exception\RequestException;
/**
* Creates a Klarna Checkout order if it does not exist
*/
final class AuthorizeRequest extends AbstractOrderRequest
{
use MerchantUrlsDataTrait;
/**
* @inheritDoc
*
* @throws InvalidRequestException
*/
public function getData()
{
$this->validate(
'amount',
'currency',
'items',
'locale',
'purchase_country',
'tax_amount'
);
$data = $this->getOrderData();
$data['merchant_urls'] = $this->getMerchantUrls();
return $data;
}
/**
* @return string|null
*/
public function getRenderUrl()
{
return $this->getParameter('render_url');
}
/**
* @inheritDoc
*
* @throws InvalidResponseException
* @throws RequestException when the HTTP client is passed a request that is invalid and cannot be sent.
* @throws NetworkException if there is an error with the network or the remote server cannot be reached.
*/
public function sendData($data)
{
$response = $this->getTransactionReference() ?
$this->sendRequest('GET', '/checkout/v3/orders/' . $this->getTransactionReference(), $data) :
$this->sendRequest('POST', '/checkout/v3/orders', $data);
if ($response->getStatusCode() >= 400) {
throw new InvalidResponseException(
\sprintf('Reason: %s (%s)', $response->getReasonPhrase(), $response->getBody())
);
}
return new AuthorizeResponse($this, $this->getResponseBody($response), $this->getRenderUrl());
}
/**
* @param string $url
*
* @return $this
*/
public function setRenderUrl(string $url): self
{
$this->setParameter('render_url', $url);
return $this;
}
}