forked from leegarner-glfusion/paypal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.inc.php
235 lines (205 loc) · 7.73 KB
/
services.inc.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Web service functions for the PayPal plugin.
* This is used to supply PayPal functions to other plugins.
*
* @author Lee Garner <lee@leegarner.com>
* @copyright Copyright (c) 2011 Lee Garner <lee@leegarner.com>
* @package paypal
* @version 0.5.1
* @since version 0.5.0
* @license http://opensource.org/licenses/gpl-2.0.php
* GNU Public License v2 or later
* @filesource
*/
if (!defined ('GVERSION')) {
die ('This file can not be used on its own!');
}
/**
* Create the payment buttons for an external item.
* Creates the requested buy_now button type and, if requested,
* an add_cart button.
*
* All gateways that have the 'external' service enabled as well as the
* requested button type will provide a button.
*
* $args['btn_type'] can be empty or not set, to create only an Add to Cart
* button. $args['add_cart'] must still be set in this case. If neither
* button type is requested, an empty array is returned.
*
* Provided $args should include at least:
* 'item_number', 'item_name', 'price', 'quantity', and 'item_type'
* $args['btn_type'] should reflect the type of immediate-purchase button
* desired. $args['add_cart'] simply needs to be set to get an add-to-cart
* button.
*
* @uses PaymentGw::ExternalButton()
* @param array $args Array of item information
* @param array &$output Pointer to output array
* @param array &$svc_msg Unused
* @return integer Status code
*/
function service_genButton_paypal($args, &$output, &$svc_msg)
{
global $_CONF, $_PP_CONF;
$btn_type = isset($args['btn_type']) ? $args['btn_type'] : '';
$output = array();
// Create the immediate purchase button, if requested. As soon as a
// gateway supplies the requested button type, break from the loop.
if (!empty($btn_type)) {
PAYPAL_loadGateways(); // load all gateways
if (!empty($_PP_CONF['gateways'])) { // Should be at least one
// Get the first gateway that supports the button type
foreach ($_PP_CONF['gateways'] as $gw_info) {
if (PaymentGw::Supports($btn_type, $gw_info) &&
PaymentGw::Supports('external', $gw_info) &&
class_exists($gw_info['id'])) {
$gw = new $gw_info['id'];
$output[] = $gw->ExternalButton($args, $btn_type);
}
}
}
}
// Now create an add-to-cart button, if requested.
if (isset($args['add_cart']) && $_PP_CONF['ena_cart'] == 1) {
if (!isset($args['item_type'])) $args['item_type'] = PP_PROD_VIRTUAL;
$T = new Template(PAYPAL_PI_PATH . '/templates');
$T->set_file('cart', 'buttons/btn_add_cart.thtml');
$T->set_var(array(
'item_name' => $args['item_name'],
'item_number' => $args['item_number'],
'short_description' => $args['short_description'],
'amount' => $args['amount'],
'pi_url' => PAYPAL_URL,
'item_type' => $args['item_type'],
'have_tax' => isset($args['tax']) ? 'true' : '',
'tax' => isset($args['tax']) ? $args['tax'] : 0,
'quantity' => isset($args['quantity']) ? $args['quantity'] : '',
'_ret_url' => isset($args['_ret_url']) ? $args['_ret_url'] : '',
) );
$output['add_cart'] = $T->parse('', 'cart');
}
return PLG_RET_OK;
}
/**
* Return the configured currency.
* This is an API function to allow other plugins to find out what
* currency we accept.
*
* @return string Our configured currency code.
*/
function service_getCurrency_paypal($args, &$output, &$svc_msg)
{
global $_PP_CONF;
$output = $_PP_CONF['currency'];
return PLG_RET_OK;
}
/**
* API function to return the url to a Paypal item.
* This returns the url to a Paypal-controlled item, such as the
* IPN transaction data. This is meant to provide a backlink for other
* plugins to use with their products.
*
* @param array $args Array of item information, at least 'type'
* @param array &$output Pointer to output array
* @param array &$svc_msg Unused
* @return integer Status code
*/
function service_getUrl_paypal($args, &$output, &$svc_msg)
{
if (!is_array($args)) {
$args = array('type' => $args);
}
$type = isset($args['type']) ? $args['type'] : '';
$url = '';
switch ($type) {
case 'ipn':
$id = isset($args['id']) ? $args['id'] : '';
if ($id != '') {
$url = PAYPAL_ADMIN_URL .
'/index.php?ipnlog=x&op=single&txn_id=' . $id;
}
break;
case 'checkout':
$url = PAYPAL_URL . '/index.php?checkout=x';
break;
}
if (!empty($url)) {
$output = $url;
return PLG_RET_OK;
} else {
$output = '';
return PLG_RET_ERROR;
}
}
/**
* Allow a plugin to push an item into the cart
*
* @param array $args Array of item information
* @param mixed &$output Output data
* @param mixed &$svc_msg Service message
* @return integer Status code
*/
function service_addCartItem_paypal($args, &$output, &$svc_msg)
{
global $ppGCart;
USES_paypal_class_cart();
$ppGCart = new ppCart();
$qty = isset($args['quantity']) ? (float)$args['quantity'] : 1;
if (!isset($args['item_number']) || empty($args['item_number'])) {
return PLG_RET_ERROR;
}
$item_name = isset($args['item_name']) ? $args['item_name'] : '';
$amount = isset($args['amount']) ? (float)$args['amount'] : 0;
$descrip = isset($args['short_description']) ? $args['short_description'] : '';
$options = isset($args['options']) ? $args['options'] : array();
$extras = isset($args['extras']) ? $args['extras'] : '';
// Option to add an item only if it is not currently in the cart.
// Cart::addItem will be called to increment the quantity if the
// unique flag is not set.
if (isset($args['unique']) && $args['unique']) {
if ($ppGCart->Contains($args['item_number'])) return PLG_RET_OK;
}
$ppGCart->addItem($args['item_number'], $item_name,
$descrip, $qty, $amount, $options, $extras);
return PLG_RET_OK;
}
/**
* Return a simple "checkout" button.
* Take optional "text" and "color" arguments.
*
* @param array $args Array of options.
* @param mixed &$output Output data
* @param mixed &$svc_msg Service message
* @return integer Status code
*/
function service_btnCheckout_paypal($args, &$output, &$svc_msg)
{
global $LANG_PP;
if (!is_array($args)) $args = array($args);
$text = isset($args['text']) ? $args['text'] : $LANG_PP['checkout'];
$color = isset($args['color']) ? $args['color'] : 'green';
$output = '<a href="' . PAYPAL_URL . '/index.php?checkout=x"><button type="button" id="ppcheckout" class="paypalButton ' . $color . '">'
. $text . '</button></a>';
return PLG_RET_OK;
}
/*
* Return a formatted amount according to the configured currency
* Accepts an array of "amount" => value, or single value as first argument.
*
* @param array $args Array of "amount" => amount value
* @param mixed &$output Output data
* @param mixed &$svc_msg Service message
* @return integer Status code
*/
function service_formatAmount_paypal($args, &$output, &$svc_msg)
{
global $_PP_CONF;
if (!is_array($args)) $args = array('amount' => $args);
$amount = isset($args['amount']) ? (float)$args['amount'] : 0;
USES_paypal_class_currency();
$Cur = new ppCurrency($_PP_CONF['currency']);
$output = $Cur->Format($amount);
return PLG_RET_OK;
}
?>