|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* "Abstract" class for a number of different payment |
|
4
|
|
|
* types allowing a user to pay for something on a site. |
|
5
|
|
|
* |
|
6
|
|
|
* |
|
7
|
|
|
* This can't be an abstract class because sapphire doesn't |
|
8
|
|
|
* support abstract DataObject classes. |
|
9
|
|
|
* |
|
10
|
|
|
* @package payment |
|
11
|
|
|
*/ |
|
12
|
|
|
class EcommercePayment_Stripe_RecordACustomer extends EcommercePayment_Stripe |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Perform payment processing for the type of |
|
18
|
|
|
* payment. For example, if this was a credit card |
|
19
|
|
|
* payment type, you would perform the data send |
|
20
|
|
|
* off to the payment gateway on this function for |
|
21
|
|
|
* your payment subclass. |
|
22
|
|
|
* |
|
23
|
|
|
* This is used by {@link OrderForm} when it is |
|
24
|
|
|
* submitted. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $data The form request data - see OrderForm |
|
27
|
|
|
* @param OrderForm $form The form object submitted on |
|
28
|
|
|
* |
|
29
|
|
|
* @return EcommercePaymentResult |
|
|
|
|
|
|
30
|
|
|
*/ |
|
31
|
|
|
public function processPayment($data, $form) |
|
32
|
|
|
{ |
|
33
|
|
|
//get variables |
|
34
|
|
|
$this->retrieveVariables(); |
|
35
|
|
|
if ( |
|
36
|
|
|
$this->_processing_member && |
|
|
|
|
|
|
37
|
|
|
$this->_processing_member->CreditCardHasBeenRecorded() |
|
|
|
|
|
|
38
|
|
|
) { |
|
39
|
|
|
//save basic info |
|
40
|
|
|
//no idea why we need this!!! |
|
41
|
|
|
if ($this->_processing_amount) { |
|
42
|
|
|
$this->Amount->Amount = $this->_processing_amount / 100; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
$this->Status = "Pending"; |
|
|
|
|
|
|
45
|
|
|
$returnObject = EcommercePayment_Success::create(); |
|
46
|
|
|
} else { |
|
47
|
|
|
$billingAddress = $this->_processing_order->CreateOrReturnExistingAddress("BillingAddress"); |
|
48
|
|
|
$this->instantiateAPI(); |
|
49
|
|
|
//first create the customer |
|
50
|
|
|
$requestData = array( |
|
51
|
|
|
"description" => $this->_processing_statement_description, |
|
52
|
|
|
"metadata" => $this->_processing_metadata, |
|
53
|
|
|
"email" => $billingAddress->Email, |
|
54
|
|
|
"source" => array( |
|
55
|
|
|
"object" => "card", |
|
56
|
|
|
"exp_month" => $this->_processing_card['exp_month'], |
|
|
|
|
|
|
57
|
|
|
"exp_year" => $this->_processing_card['exp_year'], |
|
|
|
|
|
|
58
|
|
|
"number" => $this->_processing_card['number'], |
|
|
|
|
|
|
59
|
|
|
"name" => $this->_processing_card['name'], |
|
|
|
|
|
|
60
|
|
|
"address_line1" => $billingAddress->Address, |
|
61
|
|
|
"address_line2" => $billingAddress->Address2, |
|
62
|
|
|
"address_state" => $billingAddress->RegionCode, |
|
63
|
|
|
"address_city" => $billingAddress->City, |
|
64
|
|
|
"address_zip" => $billingAddress->PostalCode, |
|
65
|
|
|
"address_country" => $billingAddress->RegionCode |
|
66
|
|
|
) |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$responseData = \Stripe\Customer::create( |
|
70
|
|
|
$requestData, |
|
71
|
|
|
$this->_processing_idempotency_key |
|
|
|
|
|
|
72
|
|
|
); |
|
73
|
|
|
$this->removeCardDetails(); |
|
74
|
|
|
$requestData["card"]["number"] = $this->_processing_truncated_card; |
|
|
|
|
|
|
75
|
|
|
$this->recordTransaction($requestData, $responseData); |
|
76
|
|
|
|
|
77
|
|
|
//save basic info |
|
78
|
|
|
//no idea why we need this!!! |
|
79
|
|
|
if ($this->_processing_amount) { |
|
80
|
|
|
$this->Amount->Amount = $this->_processing_amount / 100; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
if ( |
|
83
|
|
|
$responseData && |
|
84
|
|
|
isset($responseData->id) && |
|
85
|
|
|
$this->_processing_member instanceof Member |
|
|
|
|
|
|
86
|
|
|
) { |
|
87
|
|
|
$this->Status = "Pending"; |
|
|
|
|
|
|
88
|
|
|
$this->_processing_member->StripeCustomerID = $responseData->id; |
|
|
|
|
|
|
89
|
|
|
$this->_processing_member->CreditCardDescription = $this->_processing_card_description; |
|
|
|
|
|
|
90
|
|
|
$this->_processing_member->write(); |
|
|
|
|
|
|
91
|
|
|
$returnObject = EcommercePayment_Success::create(); |
|
92
|
|
|
} else { |
|
93
|
|
|
$this->Status = "Failure"; |
|
|
|
|
|
|
94
|
|
|
$returnObject = EcommercePayment_Failure::create(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
$this->write(); |
|
98
|
|
|
return $returnObject; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return the payment form fields that should |
|
103
|
|
|
* be shown on the checkout order form for the |
|
104
|
|
|
* payment type. Example: for {@link DPSPayment}, |
|
105
|
|
|
* this would be a set of fields to enter your |
|
106
|
|
|
* credit card details. |
|
107
|
|
|
* |
|
108
|
|
|
* @return FieldList |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getPaymentFormFields() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->retrieveVariables(); |
|
113
|
|
|
if ($this->_processing_member && $this->_processing_member->CreditCardHasBeenRecorded()) { |
|
|
|
|
|
|
114
|
|
|
return FieldList::create( |
|
115
|
|
|
ReadonlyField::create("CreditCardOnFile", _t("Stripe.CREDIT_CARD_ON_FILE", "Credit Card on file"), $this->memberCurrentCardDescription()) |
|
116
|
|
|
); |
|
117
|
|
|
} else { |
|
118
|
|
|
return parent::getPaymentFormFields(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* return null | string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function memberCurrentCardDescription() |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$this->retrieveVariables(); |
|
128
|
|
|
if ($this->_processing_member && $this->_processing_member->CreditCardHasBeenRecorded()) { |
|
|
|
|
|
|
129
|
|
|
$this->instantiateAPI(); |
|
130
|
|
|
$customer = \Stripe\Customer::retrieve($this->_processing_member->StripeCustomerID); |
|
|
|
|
|
|
131
|
|
|
if ($customer && (!isset($customer->deleted) || (isset($customer->deleted) && !$customer->deleted))) { |
|
132
|
|
|
if ($customer->id == $this->_processing_member->StripeCustomerID) { |
|
|
|
|
|
|
133
|
|
|
return $this->_processing_member->CreditCardDescription; |
|
|
|
|
|
|
134
|
|
|
} else { |
|
135
|
|
|
return _t("Stripe.ERROR_RETRIEVING_DETAILS #2", "Error retrieving details"); |
|
136
|
|
|
} |
|
137
|
|
|
} else { |
|
138
|
|
|
return _t("Stripe.ERROR_RETRIEVING_DETAILS #1", "Error retrieving details"); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
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.