|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GoCardlessPayment\MandateCheckout; |
|
4
|
|
|
|
|
5
|
|
|
use GoCardlessPayment\Api; |
|
6
|
|
|
use GoCardlessPayment\Contracts\GoCardlessCustomer; |
|
7
|
|
|
use GoCardlessPayment\GoCardlessPayment; |
|
8
|
|
|
use GoCardlessPayment\Makeable; |
|
9
|
|
|
use Illuminate\Support\Arr; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Builder encapsulate all requests exchanges to create checkout page url for Mandate creation. |
|
13
|
|
|
* |
|
14
|
|
|
* @see https://developer.gocardless.com/billing-requests/setting-up-a-dd-mandate |
|
15
|
|
|
*/ |
|
16
|
|
|
class MandateCheckoutPage |
|
17
|
|
|
{ |
|
18
|
|
|
use Makeable; |
|
19
|
|
|
|
|
20
|
|
|
protected Api $client; |
|
21
|
|
|
|
|
22
|
|
|
protected ?GoCardlessCustomer $gocardlessCustomer = null; |
|
23
|
|
|
|
|
24
|
|
|
protected BillingRequest $billingRequest; |
|
25
|
|
|
|
|
26
|
|
|
protected BillingRequestFlow $billingRequestFlow; |
|
27
|
|
|
|
|
28
|
1 |
|
public function __construct(BillingRequest $billingRequest, BillingRequestFlow $billingRequestFlow) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->client = GoCardlessPayment::api(); |
|
31
|
|
|
|
|
32
|
1 |
|
$this->billingRequest = $billingRequest; |
|
33
|
1 |
|
$this->billingRequestFlow = $billingRequestFlow; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function useCustomer(GoCardlessCustomer $gocardlessCustomer): static |
|
37
|
|
|
{ |
|
38
|
1 |
|
$this->gocardlessCustomer = $gocardlessCustomer; |
|
39
|
|
|
|
|
40
|
1 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @throws \GoCardlessPro\Core\Exception\InvalidStateException |
|
45
|
|
|
*/ |
|
46
|
1 |
|
protected function sendBillingRequest(): \GoCardlessPro\Resources\BillingRequest |
|
47
|
|
|
{ |
|
48
|
1 |
|
$params = $this->billingRequest->jsonSerialize(); |
|
49
|
1 |
|
if ($this->gocardlessCustomer) { |
|
50
|
1 |
|
$keyName = GoCardlessPayment::$syncMetadataKeyName; |
|
51
|
|
|
|
|
52
|
1 |
|
Arr::set($params, "metadata.{$keyName}", $this->gocardlessCustomer->getSyncKey()); |
|
53
|
1 |
|
if ($gocardlessKey = $this->gocardlessCustomer->gocardlessKey()) { |
|
54
|
1 |
|
Arr::set($params, 'links.customer', $gocardlessKey); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Additionally we provide sync keys in mandate and payment requests for more informational object |
|
58
|
|
|
// inside GoCardless dashboard |
|
59
|
1 |
|
if (Arr::has($params, 'mandate_request')) { |
|
60
|
1 |
|
Arr::set($params, "mandate_request.metadata.{$keyName}", $this->gocardlessCustomer->getSyncKey()); |
|
61
|
|
|
} |
|
62
|
1 |
|
if (Arr::has($params, 'payment_request')) { |
|
63
|
|
|
Arr::set($params, "payment_request.metadata.{$keyName}", $this->gocardlessCustomer->getSyncKey()); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return $this->client->billingRequests()->create([ |
|
|
|
|
|
|
68
|
1 |
|
'params' => $params, |
|
69
|
1 |
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @throws \Exception |
|
74
|
|
|
*/ |
|
75
|
1 |
|
protected function sendBillingFlowRequest(string $billingResponseId): \GoCardlessPro\Resources\BillingRequestFlow |
|
76
|
|
|
{ |
|
77
|
1 |
|
if ($this->gocardlessCustomer) { |
|
78
|
1 |
|
$this->billingRequestFlow->prefilledCustomer( |
|
79
|
1 |
|
PrefilledCustomer::make() |
|
80
|
1 |
|
->givenName($this->gocardlessCustomer->gocardlessGivenName()) |
|
81
|
1 |
|
->familyName($this->gocardlessCustomer->gocardlessFamilyName()) |
|
82
|
1 |
|
->email($this->gocardlessCustomer->gocardlessEmail()) |
|
83
|
1 |
|
->postalCode($this->gocardlessCustomer->gocardlessPostalCode()) |
|
84
|
1 |
|
->addressLine1($this->gocardlessCustomer->gocardlessAddressLine1()) |
|
85
|
1 |
|
->addressLine2($this->gocardlessCustomer->gocardlessAddressLine2()) |
|
86
|
1 |
|
->addressLine3($this->gocardlessCustomer->gocardlessAddressLine3()) |
|
87
|
1 |
|
->city($this->gocardlessCustomer->gocardlessCity()) |
|
88
|
1 |
|
->region($this->gocardlessCustomer->gocardlessRegion()) |
|
89
|
1 |
|
->countryCode($this->gocardlessCustomer->gocardlessCountryCode()) |
|
90
|
1 |
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
return $this->client->billingRequestFlows()->create([ |
|
|
|
|
|
|
94
|
1 |
|
'params' => $this->billingRequestFlow->setBillingRequestId($billingResponseId)->jsonSerialize(), |
|
95
|
1 |
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @throws \GoCardlessPro\Core\Exception\InvalidStateException|\Exception |
|
100
|
|
|
*/ |
|
101
|
1 |
|
protected function sendRequest(): \GoCardlessPro\Resources\BillingRequestFlow |
|
102
|
|
|
{ |
|
103
|
1 |
|
$response = $this->sendBillingRequest(); |
|
104
|
|
|
|
|
105
|
1 |
|
return $this->sendBillingFlowRequest($response->id); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @throws \GoCardlessPro\Core\Exception\InvalidStateException |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function requestCheckoutUrl(): string |
|
112
|
|
|
{ |
|
113
|
1 |
|
return $this->sendRequest()->authorisation_url; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|