|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverShop\Checkout\Component; |
|
4
|
|
|
|
|
5
|
|
|
use Omnipay\Common\Helper; |
|
6
|
|
|
use SilverShop\Checkout\Checkout; |
|
7
|
|
|
use SilverShop\Model\Order; |
|
8
|
|
|
use SilverStripe\Forms\LiteralField; |
|
9
|
|
|
use SilverStripe\Omnipay\GatewayFieldsFactory; |
|
10
|
|
|
use SilverStripe\Omnipay\GatewayInfo; |
|
11
|
|
|
use SilverStripe\ORM\ValidationException; |
|
12
|
|
|
use SilverStripe\ORM\ValidationResult; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This component should only ever be used on SSL encrypted pages! |
|
16
|
|
|
*/ |
|
17
|
|
|
class OnsitePayment extends CheckoutComponent |
|
18
|
|
|
{ |
|
19
|
|
|
public function getFormFields(Order $order) |
|
20
|
|
|
{ |
|
21
|
|
|
$gateway = Checkout::get($order)->getSelectedPaymentMethod(); |
|
22
|
|
|
$gatewayfieldsfactory = new GatewayFieldsFactory($gateway, ['Card']); |
|
23
|
|
|
$fields = $gatewayfieldsfactory->getCardFields(); |
|
24
|
|
|
if ($gateway === 'Dummy') { |
|
25
|
|
|
$fields->unshift( |
|
26
|
|
|
LiteralField::create( |
|
27
|
|
|
'dummypaymentmessage', |
|
28
|
|
|
'<p class=\"message good\">Dummy data has been added to the form for testing convenience.</p>' |
|
29
|
|
|
) |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $fields; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getRequiredFields(Order $order) |
|
37
|
|
|
{ |
|
38
|
|
|
$gateway = Checkout::get($order)->getSelectedPaymentMethod(); |
|
39
|
|
|
$required = GatewayInfo::requiredFields($gateway); |
|
40
|
|
|
$fieldsFactory = new GatewayFieldsFactory($gateway, ['Card']); |
|
41
|
|
|
return $fieldsFactory->getFieldName(array_combine($required, $required)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function validateData(Order $order, array $data) |
|
45
|
|
|
{ |
|
46
|
|
|
$result = ValidationResult::create(); |
|
47
|
|
|
//TODO: validate credit card data |
|
48
|
|
|
if (!Helper::validateLuhn($data['number'])) { |
|
49
|
|
|
$result->addError(_t(__CLASS__ . '.InvalidCreditCard', 'Credit card is invalid')); |
|
50
|
|
|
throw new ValidationException($result); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getData(Order $order) |
|
55
|
|
|
{ |
|
56
|
|
|
$data = []; |
|
57
|
|
|
$gateway = Checkout::get($order)->getSelectedPaymentMethod(); |
|
58
|
|
|
//provide valid dummy credit card data |
|
59
|
|
|
if ($gateway === 'Dummy') { |
|
60
|
|
|
$data = array_merge( |
|
61
|
|
|
[ |
|
62
|
|
|
'name' => 'Joe Bloggs', |
|
63
|
|
|
'number' => '4242424242424242', |
|
64
|
|
|
'cvv' => 123, |
|
65
|
|
|
], |
|
66
|
|
|
$data |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
return $data; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function setData(Order $order, array $data) |
|
73
|
|
|
{ |
|
74
|
|
|
//create payment? |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function providesPaymentData() |
|
78
|
|
|
{ |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|