-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpayment.php
More file actions
185 lines (159 loc) · 4.71 KB
/
payment.php
File metadata and controls
185 lines (159 loc) · 4.71 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* @package Redform
* @subpackage Payment.paypal
* @copyright Copyright (C) 2008 redCOMPONENT.com. All rights reserved.
* @license GNU/GPL, see LICENSE
*/
defined('_JEXEC') or die('Restricted access');
/**
* Paypal helper
*
* @package Redform
* @subpackage Payment.paypal
* @since 2.5
*/
class PaymentPaypal extends RdfPaymentHelper
{
/**
* name of the gateway for dispatching
* @var string
*/
protected $gateway = 'paypal';
protected $params = null;
/**
* Display or redirect to the payment page for the gateway
*
* @param object $request payment request object
* @param string $return_url return url for redirection
* @param string $cancel_url cancel url for redirection
*
* @return true on success
*/
public function process($request, $return_url = null, $cancel_url = null)
{
$app = JFactory::getApplication();
$reference = $request->key;
if (empty($return_url))
{
$return_url = $this->getUrl('processing', $reference);
}
if (empty($cancel_url))
{
$cancel_url = $this->getUrl('cancel', $reference);
}
// Get price and currency
$res = $this->getDetails($reference);
if ($this->params->get('paypal_sandbox', 1) == 1)
{
$paypalurl = "https://www.sandbox.paypal.com/cgi-bin/webscr";
}
else
{
$paypalurl = "https://www.paypal.com/cgi-bin/webscr";
}
$post_variables = Array(
"cmd" => "_xclick",
"business" => $this->params->get('paypal_account'),
"item_name" => $request->title,
"no_shipping" => '1',
"invoice" => $request->uniqueid,
"amount" => RedshopHelperCurrency::convert($this->getPrice($res), '', $this->params->get('currency')),
"return" => $return_url,
"notify_url" => $this->getUrl('notify', $reference),
"cancel_return" => $cancel_url,
"undefined_quantity" => "0",
"currency_code" => $this->params->get('currency'),
"no_note" => "1"
);
$query_string = "?";
foreach ($post_variables as $name => $value)
{
$query_string .= $name . "=" . urlencode($value) . "&";
}
$app->redirect($paypalurl . $query_string);
}
/**
* handle the recpetion of notification
*
* @return bool paid status
*/
public function notify()
{
$mainframe = JFactory::getApplication();
$db = JFactory::getDBO();
$post = JRequest::get('post');
$reference = JRequest::getvar('reference');
$paid = 0;
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
$data = array();
foreach ($post as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
$data[] = "$key=$value";
}
// Assign posted variables to local variables
$item_name = $post['item_name'];
$item_number = $post['item_number'];
$payment_status = $post['payment_status'];
$payment_amount = $post['mc_gross'];
$payment_currency = $post['mc_currency'];
$txn_id = $post['txn_id'];
$receiver_email = $post['receiver_email'];
$payer_email = $post['payer_email'];
// Post back to PayPal system to validate
if ($this->params->get('paypal_sandbox', 1) == 1)
{
$paypalurl = "https://www.sandbox.paypal.com";
}
else
{
$paypalurl = "https://www.paypal.com";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $paypalurl . '/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.paypal.com'));
$res = curl_exec($ch);
curl_close($ch);
if (strcmp($res, "VERIFIED") == 0)
{
/* Check the payment_status is Completed
check that txn_id has not been previously processed
check that receiver_email is your Primary PayPal email
check that payment_amount/payment_currency are correct */
$res = $this->getDetails($reference);
if ($payment_amount != $this->getPrice($res))
{
RdfHelperLog::simpleLog('PAYPAL NOTIFICATION WRONG AMOUNT(' . $this->getPrice($res) . ') - ' . $reference);
}
elseif ($payment_currency != $res->currency)
{
RdfHelperLog::simpleLog('PAYPAL NOTIFICATION WRONG CURRENCY (' . $res->currency . ') - ' . $reference);
}
elseif (strcasecmp($payment_status, 'completed') == 0)
{
$paid = 1;
}
}
elseif (strcmp($res, "INVALID") == 0)
{
// Log for manual investigation
RdfHelperLog::simpleLog('PAYPAL NOTIFICATION INVALID IPN' . ' - ' . $reference);
}
else
{
RdfHelperLog::simpleLog('PAYPAL NOTIFICATION HTTP ERROR' . ' for ' . $reference);
}
$this->writeTransaction($reference, implode("\n", $data), $payment_status, $paid);
// For routing
JRequest::setVar('reference', $reference);
return $paid;
}
}