-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
payment.php
148 lines (119 loc) · 4.45 KB
/
payment.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
141
142
143
144
145
146
147
148
<?php
namespace Wagnerwagner\Merx;
use PayPalHttp\HttpResponse;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Core\ProductionEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
use Stripe\Stripe;
use Stripe\Charge;
use Stripe\Source;
use Stripe\ApiResource;
class Payment
{
private static function getPayPalClient(): PayPalHttpClient
{
if (option('ww.merx.production') === true) {
$environment = new ProductionEnvironment(option('ww.merx.paypal.live.clientID'), option('ww.merx.paypal.live.secret'));
} else {
$environment = new SandboxEnvironment(option('ww.merx.paypal.sandbox.clientID'), option('ww.merx.paypal.sandbox.secret'));
}
return new PayPalHttpClient($environment);
}
private static function setStripeApiKey(): void
{
if (option('ww.merx.production') === true) {
Stripe::setApiKey(option('ww.merx.stripe.live.secret_key'));
} else {
Stripe::setApiKey(option('ww.merx.stripe.test.secret_key'));
}
}
public static function createPayPalPayment(float $total): HttpResponse
{
$client = self::getPayPalClient();
$siteTitle = (string)site()->title();
$request = new OrdersCreateRequest();
$applicationContext = array_merge([
'cancel_url' => url(option('ww.merx.successPage')),
'return_url' => url(option('ww.merx.successPage')),
'user_action' => 'PAY_NOW',
'shipping_preference' => 'NO_SHIPPING',
'brand_name' => $siteTitle,
], option('ww.merx.paypal.applicationContext', []));
if (option('ww.merx.paypal.purchaseUnits')) {
$purchaseUnits = option('ww.merx.paypal.purchaseUnits')();
} else {
$purchaseUnits = [
[
"description" => $siteTitle,
"amount" => [
"value" => number_format($total, 2, '.', ''),
"currency_code" => option('ww.merx.currency'),
],
],
];
}
$request->body = [
"intent" => "CAPTURE",
"purchase_units" => $purchaseUnits,
"application_context" => $applicationContext,
];
$response = $client->execute($request);
return $response;
}
public static function executePayPalPayment(string $orderId): HttpResponse
{
$client = self::getPayPalClient();
$request = new OrdersCaptureRequest($orderId);
$response = $client->execute($request);
return $response;
}
public static function createStripePaymentIntent(float $amount, array $params = [], $options = []): ApiResource
{
self::setStripeApiKey();
$intent = \Stripe\PaymentIntent::create(array_merge([
'amount' => round($amount * 100),
'currency' => option('ww.merx.currency'),
'capture_method' => 'manual',
'payment_method_types' => ['card'],
], $params), $options);
return $intent;
}
public static function getStripePaymentIntent(string $paymentIntentId): ApiResource
{
self::setStripeApiKey();
$intent = \Stripe\PaymentIntent::retrieve($paymentIntentId);
return $intent;
}
public static function createStripeSource(float $amount, string $type = 'sofort', array $data = []): ApiResource
{
self::setStripeApiKey();
$params = array_merge([
"type" => $type,
"amount" => round($amount * 100),
"currency" => option('ww.merx.currency'),
"redirect" => [
"return_url" => url(option('ww.merx.successPage')),
]
], $data);
$source = Source::create($params);
return $source;
}
public static function getStatusOfSource(string $sourceString): string
{
self::setStripeApiKey();
$source = \Stripe\Source::retrieve($sourceString);
return $source->status;
}
public static function createStripeCharge(Float $amount, string $source): ApiResource
{
self::setStripeApiKey();
$charge = Charge::create(array(
'amount' => round($amount * 100),
'currency' => option('ww.merx.currency'),
'source' => $source,
));
return $charge;
}
}