-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaymentCreditCard.module
238 lines (187 loc) · 7.48 KB
/
PaymentCreditCard.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
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
236
237
238
<?php
require_once(dirname(__FILE__) . "/PaymentAbstract.php");
// If your payment processor has a library, SDK, or similar, require it here.
class PaymentCreditCard extends PaymentAbstract {
public static function getModuleInfo()
{
return array(
"title" => "Credit Card Payments",
"version" => 001,
"summary" => "Generic payment method for sending credit card orders to a payment processor",
"singular" => false,
"autoload" => false,
'requires' => array(
"ShoppingCart",
"ShoppingCheckout"
)
);
}
public function init() {
$this->title = $this->_("Credit Card");
}
public function processPayment(Page $order) {
function renderForm() {
$states = array( "initial_value" => " - Select Province/State - ","AB" => "Alberta","BC" => "British Columbia","MB" => "Manitoba","NB" => "New Burnswick","NL" => "Newfoundland and Labrador","NS" => "Nova Scotia","NT" => "Northwest Territories","NU" => "Nunavut","ON" => "Ontario","PE" => "Prince Edward Island","QC" => "Quebec","SK" => "Saskatchewan","YT" => "Yukon","divider" => "---------------","AL" => "Alabama","AK" => "Alaska","AZ" => "Arizona","AR" => "Arkansas","CA" => "California","CO" => "Colorado","CT" => "Connecticut","DE" => "Delaware","DC" => "District of Columbia","FL" => "Florida","GA" => "Georgia","HI" => "Hawaii","ID" => "Idaho","IL" => "Illinois","IN" => "Indiana","IA" => "Iowa","KS" => "Kansas","KY" => "Kentucky","LA" => "Louisiana","ME" => "Maine","MD" => "Maryland","MA" => "Massachusetts","MI" => "Michigan","MN" => "Minnesota","MS" => "Mississippi","MO" => "Missouri","MT" => "Montana","NE" => "Nebraska","NV" => "Nevada","NH" => "New Hampshire","NJ" => "New Jersey","NM" => "New Mexico","NY" => "New York","NC" => "North Carolina","ND" => "North Dakota","OH" => "Ohio","OK" => "Oklahoma","OR" => "Oregon","PA" => "Pennsylvania","RI" => "Rhode Island","SC" => "South Carolina","SD" => "South Dakota","TN" => "Tennessee","TX" => "Texas","UT" => "Utah","VT" => "Vermont","VA" => "Virginia","WA" => "Washington","WV" => "West Virginia","WI" => "Wisconsin","WY" => "Wyoming");
$modules = wire("modules");
$out = "";
$form = $modules->get("InputfieldForm");
$form->action = "./?payment=submitted";
$form->method = "post";
$form->attr("id+name","cc_payment_form");
$field = $modules->get("InputfieldText");
$field->placeholder = "Name on Card";
$field->label = "Name on Card";
$field->attr("id+name","cc_name_on_card");
$field->required = 1;
$form->append($field);
$field = $modules->get("InputfieldText");
$field->label = "Credit Card Number";
$field->placeholder = "Card Number";
$field->attr("id+name","cc_card_number");
$field->required = 1;
$form->append($field);
$select = $modules->get("InputfieldSelect");
$select->label = "Expiration Month";
$select->attr("id+name","cc_expiration_month");
$select->addOption("", "Month");
for ($i = 1; $i < 13; ++$i)
{
$select->addOption($i, $i);
}
$select->value = "";
$select->required = 1;
$form->append($select);
$select = $modules->get("InputfieldSelect");
$select->label = "Expiration Year";
$select->attr("id+name","cc_expiration_year");
$select->addOption("", "Year");
// Your processor may need the full year for the expiry date; change these values if so.
for ($i = 14; $i < 30; ++$i)
{
$select->addOption($i, "20" . $i);
}
$select->value = "";
$select->required = 1;
$form->append($select);
$field = $modules->get("InputfieldText");
$field->label = "Card Security Number";
$field->placeholder = "Card Security Number";
$field->attr("id+name","cc_security_number");
$field->required = 1;
$form->append($field);
$field = $modules->get("InputfieldText");
$field->label = "Street Address";
$field->placeholder = "Address";
$field->attr("id+name","cc_street_address");
$field->required = 1;
$form->append($field);
$field = $modules->get("InputfieldText");
$field->label = "Zip / Postal Code";
$field->placeholder = "Zip / Postal Code";
$field->attr("id+name","cc_postalzip");
$field->required = 1;
$form->append($field);
$select = $modules->get("InputfieldSelect");
$select->label = "State / Province";
$select->attr("id+name","cc_state_province");
foreach ($states as $key=>$value)
{
$select->addOption($key, $value);
}
$select->required = 1;
$form->append($select);
$submit = $modules->get("InputfieldSubmit");
$submit->attr("value","submit");
$submit->attr("id+name","submit");
$form->append($submit);
$out .= $form->render();
echo ($out);
}
// Validate the credit card number. Use your own validation method if preferred, or let your payment processor handle validation
function validate_credit_card($number)
{
$doubledNumber = "";
$odd = false;
for($i = strlen($number)-1; $i >=0; $i--)
{
$doubledNumber .= ($odd) ? $number[$i] * 2 : $number[$i];
$odd = !$odd;
}
$sum = 0;
for($i = 0; $i < strlen($doubledNumber); $i++)
$sum += (int) $doubledNumber[$i];
if (($sum % 10 ==0) && ($sum != 0))
{
return true;
}
else
{
return false;
}
}
$total = 0;
$error = false;
// Calculate the total amount we need to submit to the payment processor
foreach($order->children("check_access=0") as $p)
{
$item_total = $p->sc_qty * $p->sc_price;
$total = $total + $item_total;
}
if ($this->input->get->payment == "submitted")
{
$credit_card_info = array();
foreach ($this->input->post as $key=>$value)
{
if (($value == ''))
{
$error = true;
}
}
$name_on_card = wire('sanitizer')->text($this->input->post->cc_name_on_card);
$credit_card_number = wire('sanitizer')->text($this->input->post->cc_card_number);
$expiration_year = wire('sanitizer')->text($this->input->post->cc_expiration_year);
$expiration_month = wire('sanitizer')->text($this->input->post->cc_expiration_month);
$ccv = wire('sanitizer')->text($this->input->post->cc_security_number);
$street_address = wire('sanitizer')->text($this->input->post->cc_street_address);
$state = wire('sanitizer')->text($this->input->post->cc_state_province);
$validate_cc = validate_credit_card($credit_card_number);
if ($validate_cc !== true || $error == true)
{
echo 'Please make sure the card information is valid';
renderForm();
}
else
{
$credit_card_info["name_on_card"] = $name_on_card;
$credit_card_info["credit_card_number"] = $credit_card_number;
$credit_card_info["ccv"] = $ccv;
$credit_card_info["expiration_month"] = $expiration_month;
$credit_card_info["expiration_year"] = $expiration_year;
$credit_card_info["address"] = $street_address;
$credit_card_info["state"] = $state;
$credit_card_info["postalzip"] = $postalzip;
$total = $total;
/*Put the code for sending $credit_card_info and $total to the payment processor here, something like this:
$result = $payment->process($credit_card_info, $total);
Change the following condition to match whatever your payment processor returns.
*/
if ($result == false || $result == "Declined")
{
echo "We were unable to process your transaction. Please double-check your information and try again.";
}
else
{
$order->setOutputFormatting(false);
$order->sc_paid = time();
$order->removeStatus(Page::statusUnpublished);
$order->save();
$this->session->redirect($this->completedUrl);
}
}
}
else
{
renderForm();
}
}
}