forked from PrestaShop/PrestaShop-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paypal_orders.php
152 lines (137 loc) · 4.9 KB
/
paypal_orders.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
149
150
151
152
<?php
/*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_'))
exit;
/*
* PayPal notification fields
*/
define('ID_INVOICE', 'invoice');
define('ID_PAYER', 'payer_id');
define('ID_TRANSACTION', 'txn_id');
define('CURRENCY', 'mc_currency');
define('PAYER_EMAIL', 'payer_email');
define('PAYMENT_DATE', 'payment_date');
define('TOTAL_PAID', 'mc_gross');
define('SHIPPING', 'shipping');
define('VERIFY_SIGN', 'verify_sign');
class PayPalOrder
{
/*
* Get PayPal order data
* - ID Order
* - ID Transaction
* - ID Invoice
* - Currency (ISO)
* - Total paid
* - Shipping
* - Capture (bool)
* - Payment date
* - Payment method (int)
* - Payment status
*/
public static function getTransactionDetails($ppec = false, $payment_status = false)
{
if ($ppec && $payment_status)
{
$transaction_id = pSQL($ppec->result['PAYMENTINFO_0_TRANSACTIONID']);
return array(
'currency' => pSQL($ppec->result['PAYMENTINFO_0_CURRENCYCODE']),
'id_invoice' => null,
'id_transaction' => $transaction_id,
'transaction_id' => $transaction_id,
'total_paid' => (float)$ppec->result['PAYMENTINFO_0_AMT'],
'shipping' => (float)$ppec->result['PAYMENTREQUEST_0_SHIPPINGAMT'],
'payment_date' => pSQL($ppec->result['PAYMENTINFO_0_ORDERTIME']),
'payment_status' => pSQL($payment_status)
);
}
else
{
$transaction_id = pSQL(Tools::getValue(ID_TRANSACTION));
return array(
'currency' => pSQL(Tools::getValue(CURRENCY)),
'id_invoice' => pSQL(Tools::getValue(ID_INVOICE)),
'id_transaction' => $transaction_id,
'transaction_id' => $transaction_id,
'total_paid' => (float)Tools::getValue(TOTAL_PAID),
'shipping' => (float)Tools::getValue(SHIPPING),
'payment_date' => pSQL(Tools::getValue(PAYMENT_DATE)),
'payment_status' => pSQL($payment_status)
);
}
}
public static function getOrderById($id_order)
{
return Db::getInstance()->getRow(
'SELECT * FROM `'._DB_PREFIX_.'paypal_order`
WHERE `id_order` = '.(int)$id_order
);
}
public static function getIdOrderByTransactionId($id_transaction)
{
$sql = 'SELECT `id_order`
FROM `'._DB_PREFIX_.'paypal_order`
WHERE `id_transaction` = \''.pSQL($id_transaction).'\'';
$result = Db::getInstance()->getRow($sql);
if ($result != false)
return (int)$result['id_order'];
return 0;
}
public static function saveOrder($id_order, $transaction)
{
$order = new Order((int)$id_order);
$total_paid = (float)$transaction['total_paid'];
if (!isset($transaction['payment_status']) || !$transaction['payment_status'])
$transaction['payment_status'] = 'NULL';
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'paypal_order`
(`id_order`, `id_transaction`, `id_invoice`, `currency`, `total_paid`, `shipping`, `capture`, `payment_date`, `payment_method`, `payment_status`)
VALUES ('.(int)$id_order.', \''.pSQL($transaction['id_transaction']).'\', \''.pSQL($transaction['id_invoice']).'\',
\''.pSQL($transaction['currency']).'\',
\''.$total_paid.'\',
\''.(float)$transaction['shipping'].'\',
\''.(int)Configuration::get('PAYPAL_CAPTURE').'\',
\''.pSQL($transaction['payment_date']).'\',
\''.(int)Configuration::get('PAYPAL_PAYMENT_METHOD').'\',
\''.pSQL($transaction['payment_status']).'\')'
);
}
public static function updateOrder($id_order, $transaction)
{
$total_paid = (float)$transaction['total_paid'];
if (!isset($transaction['payment_status']) || !$transaction['payment_status'])
$transaction['payment_status'] = 'NULL';
$sql = 'UPDATE `'._DB_PREFIX_.'paypal_order`
SET `payment_status` = \''.pSQL($transaction['payment_status']).'\'
WHERE `id_order` = \''.(int)$id_order.'\'
AND `id_transaction` = \''.pSQL($transaction['id_transaction']).'\'
AND `currency` = \''.pSQL($transaction['currency']).'\'';
if ((int)Configuration::get('PAYPAL_SANDBOX') != 1)
$sql .= 'AND `total_paid` = \''.$transaction['total_paid'].'\'
AND `shipping` = \''.(float)$transaction['shipping'].'\';';
Db::getInstance()->Execute($sql);
}
}