-
Notifications
You must be signed in to change notification settings - Fork 0
/
Braintree.php
121 lines (107 loc) · 3.7 KB
/
Braintree.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
<?php
namespace App\Services\Payment;
use Braintree\Gateway;
use Braintree\PayPalAccount;
use Braintree\Result\Successful;
/**
* Braintree provides abstraction for "PayPal Braintree" payment gateway interaction.
*
* Example:
*
* ```
* $braintree = new Braintree([
* 'environment' => 'sandbox',
* 'merchantId' => 'your_merchant_id',
* 'publicKey' => 'your_public_key',
* 'privateKey' => 'your_private_key'
* ]);
* ```
*
* @see https://articles.braintreepayments.com/get-started/try-it-out
* @see https://www.braintreepayments.com/sandbox
* @see https://github.com/braintree/braintree_php
* @see https://developer.paypal.com/braintree/docs/start/hello-server/php
*/
class Braintree
{
protected Gateway $gateway;
public function __construct(array $config)
{
$this->gateway = new Gateway($config);
}
/**
* Generates new client token, which should be passed to API client (e.g. JavaScript).
*
* @see https://developer.paypal.com/braintree/docs/start/hello-client/javascript/v3/
*
* @param int|null $customerId
* @return string client token.
*/
public function generateClientToken($customerId = null): string
{
return $this->gateway->clientToken()->generate([
'customerId' => $customerId,
]);
}
/**
* Create a Braintree customer for the given attributes.
*
* @param string $paymentMethodNonce
* @param array $options
* @return array created customer data.
* @throws \RuntimeException
*/
public function createCustomer(string $paymentMethodNonce, array $options = []): array
{
$response = $this->gateway->customer()->create(
array_replace_recursive([
//'firstName' => '',
//'lastName' => '',
//'email' => '',
'paymentMethodNonce' => $paymentMethodNonce,
'creditCard' => [
'options' => [
'verifyCard' => true,
],
],
], $options)
);
if (! $response->success) {
throw new \RuntimeException('Unable to create Braintree customer: ' . $response->message);
}
$paymentMethod = $response->customer->defaultPaymentMethod();
$isPaypalAccount = $paymentMethod instanceof PaypalAccount;
return [
'customer_id' => $response->customer->id,
'paypal_email' => $isPaypalAccount ? $paymentMethod->email : null,
'card_brand' => $isPaypalAccount ? null : $paymentMethod->cardType,
'card_last_four' => $isPaypalAccount ? null : $paymentMethod->last4,
];
}
/**
* Make a "one off" charge on the customer for the given amount.
*
* @see https://developer.paypal.com/braintree/docs/reference/request/transaction/sale/php
*
* @param int $customerId
* @param int $amount
* @param array $options
* @return array transaction data.
* @throws \RuntimeException
*/
public function charge($customerId, $amount, array $options = []): array
{
$paymentMethod = $this->gateway->customer()->find($customerId)->defaultPaymentMethod();
$response = $this->gateway->transaction()->sale(array_merge([
'amount' => number_format($amount, 2, '.', ''),
'paymentMethodToken' => $paymentMethod->token,
'options' => [
'submitForSettlement' => true,
],
], $options));
if (! $response->success) {
throw new \RuntimeException('Braintree was unable to perform a charge: ' . $response->message);
}
return $response->transaction->jsonSerialize();
}
}