-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-legal-seller-user-and-intiate-onboarding-with-mangopay.php
140 lines (124 loc) · 6.23 KB
/
create-legal-seller-user-and-intiate-onboarding-with-mangopay.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
140
<?php
include_once("../vendor/autoload.php");
include_once("helpers.php");
use Google\Protobuf\Timestamp;
try {
// Get the API URL.
$apiUrl = getenv("EFTY_PAY_API_URL");
// Random string to be used in the username & email.
$randomString = generateRandomString(5);
// Set the options to pass to the connection clients (SSL & admin / integrator token in the authorization header).
$opts = [
"credentials" => \Grpc\ChannelCredentials::createSsl(),
"update_metadata" => function ($metaData) {
$token = generateToken();
$metaData['authorization'] = [$token];
return $metaData;
},
];
// Define the clients for the users & payments API.
$usersClient = new \Eftypay\Users\UsersClient($apiUrl, $opts);
$paymentsClient = new \Eftypay\Payments\PaymentsClient($apiUrl, $opts);
// Define the seller with nested objects.
$seller = (new \Eftypay\Users\User())
->setFirstName("Patrick")
->setLastName("Kosterman")
->setUsername("patrick-kosterman-seller-" . $randomString)
->setEmail("patrick-kosterman-seller-" . $randomString. "@efty.com")
->setPassword("wasdfp-0wsdfe-mafdfrw")
->setCompanyRegistrationNumber("87654321")
->setCompanyName("Efty Pay B.V.")
->setCompanyAddress(
(new \Eftypay\Common\Address())
->setAddressLine1("Zuiderpark 17")
->setPostalCode("9724 AG")
->setCity("Groningen")
->setCountry(\Eftypay\Common\CountryCode::NL)
)
->setVatSettings(
(new \Eftypay\Common\VatSettings())->setHasVat(true)
)
->setUserType(\Eftypay\Users\UserType::BUYER_OR_SELLER)
->setStatus(\Eftypay\Users\UserStatus::ACTIVE)
->setPhoneNumber("+34615504467")
->setPaymentDetails(
(new \Eftypay\Payments\PaymentDetails())->setMangopayDetails(
(new \Eftypay\Payments\Mangopay\MangopayDetails())
->setOnboardAsSellerWithMangopay(true)
->setUserCategory(\Eftypay\Payments\Mangopay\UserCategory::OWNER)
)
);
// Create the user request.
$userRequest = (new \Eftypay\Users\UserRequest())->setUser($seller);
// Create the user.
list($userResponse, $status) = $usersClient->CreateUser($userRequest)->wait();
if ($status->code !== 0) {
// If we get an error code back, then log the error.
throw new Exception(sprintf(
'Error creating the user. Status Code: %s, Details: %s.',
$status->code,
$status->details
));
}
// No error, print the new user ID.
echo "New seller user created: " . $userResponse->getId() . "\r\n";
$dateOfBirth = new \DateTime('1982-04-12');
$timestamp = new Timestamp();
$protobufTimestamp = $timestamp->fromDateTime($dateOfBirth);
// Create the onboarding object. In this case we want to onboard the seller as a legal user, as it's a company.
$mangopayOnboardingRequest = (new \Eftypay\Payments\Mangopay\MangopayOnboarding())
->setSellerUserId(
(new \Eftypay\Common\Id())
->setId($userResponse->getId()) // Assuming $seller has an ID set.
)
->setLegalUser(
(new \Eftypay\Payments\Mangopay\LegalUser())
->setRegisteredName($userResponse->getCompanyName())
->setCompanyNumber($userResponse->getCompanyRegistrationNumber())
->setEmail($userResponse->getEmail())
->setRegisteredAddress(
(new \Eftypay\Payments\Mangopay\Address())
->setAddressLine1($userResponse->getCompanyAddress()->getAddressLine1())
->setAddressLine2($userResponse->getCompanyAddress()->getAddressLine2())
->setCity($userResponse->getCompanyAddress()->getCity())
->setRegionOrCounty($userResponse->getCompanyAddress()->getStateOrCounty())
->setPostalCode($userResponse->getCompanyAddress()->getPostalCode())
->setCountry($userResponse->getCompanyAddress()->getCountry())
)
->setLegalRepresentative(
(new \Eftypay\Payments\Mangopay\Person())
->setFirstName($userResponse->getFirstName())
->setLastName($userResponse->getLastName())
->setEmail($userResponse->getEmail())
->setDateOfBirth($protobufTimestamp)
->setNationality(\Eftypay\Common\CountryCode::NL)
->setCountryOfResidence(\Eftypay\Common\CountryCode::NL)
)
->setLegalUserType(\Eftypay\Payments\Mangopay\LegalUserType::BUSINESS)
->setVatNumber("abc123")
)
->setOnboardingType(\Eftypay\Payments\Mangopay\OnboardingType::LEGAL_USER);
// Onboard the user; the user will receive an email with a link to complete their onboarding in the Efty Pay environment.
list($onboardingResponse, $status) = $paymentsClient->CreateSellerOnboardingForMangopay($mangopayOnboardingRequest)->wait();
if ($status->code !== 0) {
// If we get an error code back, then log the error.
throw new Exception(sprintf(
'Error onboarding the user. Status Code: %s, Details: %s.',
$status->code,
$status->details
));
}
// No error, print the new user ID.
echo "New seller onboarding successfully initiated for user: " . $onboardingResponse->getSellerUserId()->getId() . "\r\n";
// If you want to redirect the user from your application to the Efty Pay onboarding environment. You can generate a magic link for the user.
$sellerId = (new \Eftypay\Common\Id())
->setId($userResponse->getId());
list($response, $status) = $paymentsClient->GetGenericMagicLink($sellerId)->wait();
if ($status->code !== 0) {
// If we get an error code back, then log the error.
throw new Exception(sprintf('Error getting the magic link. Status Code: %s, Details: %s.', $status->code, $status->details));
}
echo "Magic link: " . $response->getValue() . "\r\n\r\n";
} catch (Exception $e) {
echo $e;
}