PaypalStandard.php
5.37 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Modules\PaypalStandard\Http\Controllers;
use App\Events\InvoicePaid;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Http\Requests\Customer\InvoicePayment as PaymentRequest;
use App\Models\Income\Invoice;
class PaypalStandard extends Controller
{
/**
* Show the form for editing the specified resource.
* @param Invoice
* @param PaymentRequest
* @return Response
*/
public function show(Invoice $invoice, PaymentRequest $request)
{
$gateway = setting('paypalstandard');
$gateway['action'] = 'https://www.paypal.com/cgi-bin/webscr';
if ($gateway['mode'] == 'sandbox') {
$gateway['action'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
}
$customer = explode(" ", $invoice->customer_name);
$last_name = array_pop($customer);
$first_name = implode(" ", $customer);
$invoice->first_name = $first_name;
$invoice->last_name = $last_name;
$gateway['language'] = \App::getLocale();
$html = view('paypalstandard::show', compact('gateway', 'invoice'))->render();
return response()->json([
'code' => 'paypalstandard',
'name' => $gateway['name'],
'description' => trans('paypalstandard::paypalstandard.description'),
'redirect' => false,
'html' => $html,
]);
}
public function result(Invoice $invoice, Request $request)
{
$success = true;
switch ($request['payment_status']) {
case 'Completed':
$message = trans('messages.success.added', ['type' => trans_choice('general.customers', 1)]);
break;
case 'Canceled_Reversal':
case 'Denied':
case 'Expired':
case 'Failed':
case 'Pending':
case 'Processed':
case 'Refunded':
case 'Reversed':
case 'Voided':
$message = trans('messages.error.added', ['type' => trans_choice('general.customers', 1)]);
$success = false;
break;
}
if ($success) {
flash($message)->success();
} else {
flash($message)->warning();
}
$redirect = url('customers/invoices/' . $invoice->id);
return redirect($redirect);
}
public function callback(Invoice $invoice, Request $request)
{
$gateway = setting('paypalstandard');
$paypal_log = new Logger('Paypal');
$paypal_log->pushHandler(new StreamHandler(storage_path('logs/paypal.log')), Logger::INFO);
if ($invoice) {
$url = 'https://ipnpb.paypal.com/cgi-bin/webscr';
if ($gateway['mode'] == 'sandbox') {
$url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
}
$client = new Client(['verify' => false]);
$paypal_request['cmd'] = '_notify-validate';
foreach ($request->toArray() as $key => $value) {
$paypal_request[$key] = urlencode(html_entity_decode($value, ENT_QUOTES, 'UTF-8'));
}
$result = $client->post($url, $paypal_request);
if ($result->getStatusCode() != 200) {
$paypal_log->info('PAYPAL_STANDARD :: CURL failed ', $result->getBody()->getContents());
} else {
$result = $result->getBody()->getContents();
}
if ($gateway['debug']) {
$paypal_log->info('PAYPAL_STANDARD :: IPN REQUEST: ', $request->toArray());
//$paypal_log->info('PAYPAL_STANDARD :: IPN RESULT: ', $result);
}
if ((strcmp($result, 'VERIFIED') == 0 || strcmp($result, 'UNVERIFIED') == 0) || true) {
switch ($request['payment_status']) {
case 'Completed':
$receiver_match = (strtolower($request['receiver_email']) == strtolower($gateway['email']));
$total_paid_match = ((float)$request['mc_gross'] == $invoice->amount);
if ($receiver_match && $total_paid_match) {
event(new InvoicePaid($invoice, $request->toArray()));
}
if (!$receiver_match) {
$paypal_log->info('PAYPAL_STANDARD :: RECEIVER EMAIL MISMATCH! ' . strtolower($request['receiver_email']));
}
if (!$total_paid_match) {
$paypal_log->info('PAYPAL_STANDARD :: TOTAL PAID MISMATCH! ' . $request['mc_gross']);
}
break;
case 'Canceled_Reversal':
case 'Denied':
case 'Expired':
case 'Failed':
case 'Pending':
case 'Processed':
case 'Refunded':
case 'Reversed':
case 'Voided':
$paypal_log->info('PAYPAL_STANDARD :: NOT COMPLETED ' . $request->toArray());
break;
}
} else {
$paypal_log->info('PAYPAL_STANDARD :: VERIFIED != 0 || UNVERIFIED != 0 ' . $request->toArray());
}
}
}
}