1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
*@author nicolaas[at]sunnysideup.co.nz |
5
|
|
|
*@description: OrderNumber and PaymentID |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
**/ |
9
|
|
|
|
10
|
|
|
class DpsPxPayPayment extends EcommercePayment |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
private static $db = array( |
|
|
|
|
13
|
|
|
'TxnRef' => 'Text', |
14
|
|
|
'DebugMessage' => 'HTMLText' |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
protected $Currency = ""; |
18
|
|
|
public function setCurrency($s) |
19
|
|
|
{ |
20
|
|
|
$this->Currency = $s; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// DPS Information |
24
|
|
|
|
25
|
|
|
private static $privacy_link = 'http://www.paymentexpress.com/privacypolicy.htm'; |
|
|
|
|
26
|
|
|
|
27
|
|
|
private static $logo = 'payment_dps/images/dps_paymentexpress_small.png'; |
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
// URLs |
31
|
|
|
|
32
|
|
|
// Please set from YAML. See _config/payment_dps.yml.example |
33
|
|
|
private static $credit_cards = array( |
34
|
|
|
/*'Visa' => 'ecommerce/images/paymentmethods/visa.jpg', |
|
|
|
|
35
|
|
|
'MasterCard' => 'ecommerce/images/paymentmethods/mastercard.jpg', |
36
|
|
|
'American Express' => 'ecommerce/images/paymentmethods/american-express.gif', |
37
|
|
|
'Dinners Club' => 'ecommerce/images/paymentmethods/dinners-club.jpg', |
38
|
|
|
'JCB' => 'ecommerce/images/paymentmethods/jcb.jpg'*/ |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
public static function remove_credit_card($creditCard) |
42
|
|
|
{ |
43
|
|
|
unset(self::$credit_cards[$creditCard]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private static $email_debug = false; |
|
|
|
|
47
|
|
|
|
48
|
|
|
public function getCMSFields() |
49
|
|
|
{ |
50
|
|
|
$fields = parent::getCMSFields(); |
51
|
|
|
$fields->replaceField("DebugMessage", new ReadonlyField("DebugMessage", "Debug info")); |
52
|
|
|
return $fields; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getPaymentFormFields() |
56
|
|
|
{ |
57
|
|
|
$logo = '<img src="' . $this->config()->get("logo"). '" alt="Credit card payments powered by DPS"/>'; |
58
|
|
|
$privacyLink = '<a href="' . $this->config()->get("privacy_link"). '" target="_blank" title="Read DPS\'s privacy policy">' . $logo . '</a><br/>'; |
59
|
|
|
$paymentsList = ''; |
60
|
|
|
if ($cards = $this->config()->get("credit_cards")) { |
61
|
|
|
foreach ($cards as $name => $image) { |
62
|
|
|
$paymentsList .= '<img src="' . $image . '" alt="' . $name . '"/>'; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$fields = new FieldList( |
66
|
|
|
new LiteralField('DPSInfo', $privacyLink), |
67
|
|
|
new LiteralField('DPSPaymentsList', $paymentsList) |
68
|
|
|
); |
69
|
|
|
return $fields; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getPaymentFormRequirements() |
73
|
|
|
{ |
74
|
|
|
return array(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $data The form request data - see OrderForm |
79
|
|
|
* @param OrderForm $form The form object submitted on |
80
|
|
|
* |
81
|
|
|
* @return EcommercePayment_Result |
82
|
|
|
*/ |
83
|
|
|
public function processPayment($data, $form) |
84
|
|
|
{ |
85
|
|
|
$order = $this->Order(); |
|
|
|
|
86
|
|
|
//if currency has been pre-set use this |
87
|
|
|
$currency = $this->Amount->Currency; |
|
|
|
|
88
|
|
|
//if amout has been pre-set, use this |
89
|
|
|
$amount = $this->Amount->Amount; |
|
|
|
|
90
|
|
|
if ($order && $order->exists()) { |
91
|
|
|
//amount may need to be adjusted to total outstanding |
92
|
|
|
//or amount may not have been set yet |
93
|
|
|
$amount = $order->TotalOutstanding(); |
94
|
|
|
//get currency from Order |
95
|
|
|
//this is better than the pre-set currency one |
96
|
|
|
//which may have been set to the default |
97
|
|
|
$currencyObject = $order->CurrencyUsed(); |
98
|
|
|
if ($currencyObject) { |
99
|
|
|
$currency = $currencyObject->Code; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
if (!$amount && !empty($data["Amount"])) { |
103
|
|
|
$amount = floatval($data["Amount"]); |
104
|
|
|
} |
105
|
|
|
if (!$currency && !empty($data["Currency"])) { |
106
|
|
|
$currency = floatval($data["Currency"]); |
107
|
|
|
} |
108
|
|
|
//final backup for currency |
109
|
|
|
if (!$currency) { |
110
|
|
|
$currency = EcommercePayment::site_currency(); |
111
|
|
|
} |
112
|
|
|
$this->Amount->Currency = $currency; |
|
|
|
|
113
|
|
|
$this->Amount->Amount = $amount; |
|
|
|
|
114
|
|
|
//no need to write here, as it will be done by BuildURL |
115
|
|
|
//$this->write(); |
|
|
|
|
116
|
|
|
$url = $this->buildURL($amount, $currency); |
117
|
|
|
return $this->executeURL($url); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* @param Float $amount |
123
|
|
|
* @param String $currency - e.g. NZD |
124
|
|
|
* @return String |
125
|
|
|
* |
126
|
|
|
*/ |
127
|
|
|
protected function buildURL($amount, $currency) |
128
|
|
|
{ |
129
|
|
|
$commsObject = new DpsPxPayComs(); |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* order details |
133
|
|
|
**/ |
134
|
|
|
$commsObject->setTxnType(DpsPxPayComs::get_txn_type()); |
135
|
|
|
$commsObject->setMerchantReference($this->ID); |
136
|
|
|
//replace any character that is NOT [0-9] or dot (.) |
137
|
|
|
|
138
|
|
|
$commsObject->setAmountInput(floatval(preg_replace("/[^0-9\.]/", "", $amount))); |
139
|
|
|
$commsObject->setCurrencyInput($currency); |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* details of the redirection |
143
|
|
|
**/ |
144
|
|
|
$commsObject->setUrlFail(DpsPxPayPayment_Handler::absolute_complete_link()); |
145
|
|
|
$commsObject->setUrlSuccess(DpsPxPayPayment_Handler::absolute_complete_link()); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* process payment data (check if it is OK and go forward if it is... |
149
|
|
|
**/ |
150
|
|
|
$url = $commsObject->startPaymentProcess(); |
151
|
|
|
$debugMessage = $commsObject->getDebugMessage(); |
152
|
|
|
$this->DebugMessage = $debugMessage; |
|
|
|
|
153
|
|
|
$this->write(); |
154
|
|
|
if ($this->config()->get("email_debug")) { |
155
|
|
|
$from = Email::config()->admin_email; |
156
|
|
|
$to = Email::config()->admin_email; |
157
|
|
|
$subject = "DPS Debug Information"; |
158
|
|
|
$body = $debugMessage; |
159
|
|
|
$email = new Email($from, $to, $subject, $body); |
160
|
|
|
$email->send(); |
161
|
|
|
} |
162
|
|
|
return $url; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function executeURL($url) |
166
|
|
|
{ |
167
|
|
|
$url = str_replace("&", "&", $url); |
168
|
|
|
$url = str_replace("&&", "&", $url); |
169
|
|
|
//$url = str_replace("==", "", $url); |
|
|
|
|
170
|
|
|
if ($url) { |
171
|
|
|
/** |
172
|
|
|
* build redirection page |
173
|
|
|
**/ |
174
|
|
|
$page = new SiteTree(); |
175
|
|
|
$page->Title = 'Redirection to DPS...'; |
176
|
|
|
$page->Logo = '<img src="' . $this->config()->get("logo") . '" alt="Payments powered by DPS"/>'; |
177
|
|
|
$page->Form = $this->DPSForm($url); |
178
|
|
|
$controller = new ContentController($page); |
179
|
|
|
Requirements::clear(); |
180
|
|
|
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js"); |
181
|
|
|
//Requirements::block(THIRDPARTY_DIR."/jquery/jquery.js"); |
|
|
|
|
182
|
|
|
//Requirements::javascript(Director::protocol()."ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"); |
|
|
|
|
183
|
|
|
return EcommercePayment_Processing::create($controller->renderWith('PaymentProcessingPage')); |
184
|
|
|
} else { |
185
|
|
|
$page = new SiteTree(); |
186
|
|
|
$page->Title = 'Sorry, DPS can not be contacted at the moment ...'; |
187
|
|
|
$page->Logo = 'Sorry, an error has occured in contacting the Payment Processing Provider, please try again in a few minutes...'; |
188
|
|
|
$page->Form = $this->DPSForm($url); |
189
|
|
|
$controller = new ContentController($page); |
190
|
|
|
Requirements::clear(); |
191
|
|
|
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js"); |
192
|
|
|
//Requirements::block(THIRDPARTY_DIR."/jquery/jquery.js"); |
|
|
|
|
193
|
|
|
//Requirements::javascript(Director::protocol()."ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"); |
|
|
|
|
194
|
|
|
return EcommercePayment_Failure::create($controller->renderWith('PaymentProcessingPage')); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function DPSForm($url) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$urlWithoutAmpersand = Convert::raw2js(str_replace('&', '&', $url)); |
|
|
|
|
201
|
|
|
return <<<HTML |
202
|
|
|
<form id="PaymentFormDPS" method="post" action="$url"> |
203
|
|
|
<input type="submit" value="pay now" /> |
204
|
|
|
</form> |
205
|
|
|
<script type="text/javascript"> |
206
|
|
|
jQuery(document).ready(function() { |
207
|
|
|
if(!jQuery.browser.msie) { |
208
|
|
|
jQuery("#PaymentFormDPS").submit(); |
209
|
|
|
} |
210
|
|
|
}); |
211
|
|
|
</script> |
212
|
|
|
HTML; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.