-
Notifications
You must be signed in to change notification settings - Fork 2
/
PaymentPaypal.module
149 lines (120 loc) · 6.03 KB
/
PaymentPaypal.module
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
<?php
require_once(dirname(__FILE__) . '/PaymentAbstract.php');
class PaymentPaypal extends PaymentAbstract implements ConfigurableModule {
public static function getModuleInfo()
{
return array(
'title' => 'Paypal Payment Method',
'version' => 001,
'summary' => 'Paypal payment method for ProcessWire Shop',
'singular' => false,
'autoload' => false
);
}
public function init() {
$this->title = __("Paypal (also credit card)");
}
/*
*
* returns nothing. You should edit and save $order page. If payment was succesful,
* then do $order->removeStatus(Page::statusUnpublished) and remember to save the order!
*
* If order was also paid, then do $order->sc_paid = time();
*
* If order was not paid, but it was succesful (like invoice, money on delivery etc)
* then just publish the order, but do not set sc_paid value.
*
* After you have manipulated the order, then just to redirect to $this->completedUrl
*
*
* @param Page $order keeps the page object for the order
*
*/
public function processPayment(Page $order) {
$out = '';
if ($this->input->urlSegment(2) == "return") {
$verify_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_notify-validate&' . http_build_query( $_POST );
if( !strstr( file_get_contents( $verify_url ), 'VERIFIED' ) ) {
$out .= 'error when validating your payment.';
} else {
$order->setOutputFormatting(false);
$order->sc_paid = time();
$order->sc_paymentid = $this->sanitizer->text($_POST['txn_id']);
$order->removeStatus(Page::statusUnpublished);
$order->save();
$this->session->redirect($this->completedUrl);
}
}
else if ($this->input->urlSegment(2) == "cancel") {
$this->session->redirect($this->completedUrl);
} else {
// Let's generate the form code for Paypal data
$http = ($this->config->https) ? 'https://' : 'http://';
$returnUrl = $http . $this->config->httpHost . $this->currentUrl . "return/";
$cancelUrl = $http . $this->config->httpHost . $this->currentUrl . "cancel/";
$out .= '<form id = "paypal_checkout" action = "https://www.paypal.com/cgi-bin/webscr" method = "post">';
$out .= '<input name = "cmd" value = "_cart" type = "hidden">
<input name = "upload" value = "1" type = "hidden">
<input name = "no_note" value = "0" type = "hidden">
<input name = "bn" value = "PP-BuyNowBF" type = "hidden">
<input name = "tax" value = "0" type = "hidden">
<input name = "rm" value = "2" type = "hidden">
<input name = "charset" value = "utf-8" type = "hidden">';
$out .= '<input type="hidden" name="first_name" value="'. $order->sc_firstname .'">
<input type="hidden" name="last_name" value="'. $order->sc_lastname .'">
<input type="hidden" name="address1" value="'. $order->sc_streetaddress .'">
<input type="hidden" name="city" value="'. $order->sc_city .'">
<input type="hidden" name="zip" value="'. $order->sc_zip .'">
<input type="hidden" name="email" value="'. $order->email .'">';
$out .= '<input name = "business" value = "' . $this->business .'" type = "hidden">
<input name = "handling_cart" value = "0" type = "hidden">
<input name = "currency_code" value = "'. $this->currency .'" type = "hidden">
<input name = "lc" value = "'. $this->location .'" type = "hidden">
<input name = "return" value = "'. $returnUrl .'" type = "hidden">
<input name = "cbt" value = "'. $this->returntxt .'" type = "hidden">
<input name = "cancel_return" value = "'. $cancelUrl .'" type = "hidden">
<input name = "custom" value = "" type = "hidden">';
foreach($order->children('check_access=0') as $i => $p) {
$i = $i + 1;
$out .= '<input name = "item_name_'. $i .'" value = "'. $p->title .'" type = "hidden">';
$out .= '<input name = "quantity_'. $i .'" value = "'. $p->sc_qty .'" type = "hidden">';
$out .= '<input name = "amount_'. $i .'" value = "'. $p->sc_price .'" type = "hidden">';
$out .= '<input name = "shipping_'. $i .'" value = "0" type = "hidden">';
}
$out .= '<input id = "ppcheckoutbtn" value = "'. __("Click here to proceed to Paypal.com") .'" class = "button" type = "submit">';
$out .= '</form>';
$out .= "<script>document.forms['paypal_checkout'].submit();</script>";
}
return $out;
}
static public function getModuleConfigInputfields(Array $data) {
// this is a container for fields, basically like a fieldset
$fields = new InputfieldWrapper();
// since this is a static function, we can't use $this->modules, so get them from the global wire() function
$modules = wire('modules');
if(empty($data['currency'])) $data['currency'] = 'EUR';
if(empty($data['merchantSecret'])) $data['merchantSecret'] = '6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ';
$field = $modules->get("InputfieldText");
$field->attr('name', 'business');
$field->attr('value', $data['business']);
$field->label = "Paypal email";
$fields->add($field);
$field = $modules->get("InputfieldText");
$field->attr('name', 'currency');
$field->attr('value', $data['currency']);
$field->label = "Currency used (Paypal currency code)";
$fields->add($field);
$field = $modules->get("InputfieldText");
$field->attr('name', 'location');
$field->attr('value', $data['location']);
$field->label = "Location";
$field->description = "Location code, ie. GB";
$fields->add($field);
$field = $modules->get("InputfieldText");
$field->attr('name', 'returntxt');
$field->attr('value', $data['returntxt']);
$field->label = "Text on 'back to mysite.com' button at Paypal.com";
$fields->add($field);
return $fields;
}
}