|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Omnipay\Common\CreditCard; |
|
4
|
|
|
use Omnipay\Common\Message\ResponseInterface; |
|
5
|
|
|
|
|
6
|
|
|
class PurchaseService extends PaymentService |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Attempt to make a payment. |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $data returnUrl/cancelUrl + customer creditcard and billing/shipping details. |
|
13
|
|
|
* Some keys (e.g. "amount") are overwritten with data from the associated {@link $payment}. |
|
14
|
|
|
* If this array is constructed from user data (e.g. a form submission), please take care |
|
15
|
|
|
* to whitelist accepted fields, in order to ensure sensitive gateway parameters like "freeShipping" can't be set. |
|
16
|
|
|
* If using {@link Form->getData()}, only fields which exist in the form are returned, |
|
17
|
|
|
* effectively whitelisting against arbitrary user input. |
|
18
|
|
|
* @return ResponseInterface omnipay's response class, specific to the chosen gateway. |
|
19
|
|
|
*/ |
|
20
|
|
|
public function purchase($data = array()) { |
|
21
|
|
|
if ($this->payment->Status !== "Created") { |
|
|
|
|
|
|
22
|
|
|
return null; //could be handled better? send payment response? |
|
23
|
|
|
} |
|
24
|
|
|
if (!$this->payment->isInDB()) { |
|
25
|
|
|
$this->payment->write(); |
|
26
|
|
|
} |
|
27
|
|
|
//update success/fail urls |
|
28
|
|
|
$this->update($data); |
|
29
|
|
|
|
|
30
|
|
|
//set the client IP address, if not already set |
|
31
|
|
|
if(!isset($data['clientIp'])){ |
|
32
|
|
|
$data['clientIp'] = Controller::curr()->getRequest()->getIP(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$gatewaydata = array_merge($data, array( |
|
36
|
|
|
'amount' => (float) $this->payment->MoneyAmount, |
|
|
|
|
|
|
37
|
|
|
'currency' => $this->payment->MoneyCurrency, |
|
|
|
|
|
|
38
|
|
|
//set all gateway return/cancel/notify urls to PaymentGatewayController endpoint |
|
39
|
|
|
'returnUrl' => $this->getEndpointURL("complete", $this->payment->Identifier), |
|
|
|
|
|
|
40
|
|
|
'cancelUrl' => $this->getEndpointURL("cancel", $this->payment->Identifier), |
|
|
|
|
|
|
41
|
|
|
'notifyUrl' => $this->getEndpointURL("notify", $this->payment->Identifier) |
|
|
|
|
|
|
42
|
|
|
)); |
|
43
|
|
|
|
|
44
|
|
|
// Often, the shop will want to pass in a transaction ID (order #, etc), but if there's |
|
45
|
|
|
// not one we need to set it as Ominpay requires this. |
|
46
|
|
|
if(!isset($gatewaydata['transactionId'])){ |
|
47
|
|
|
$gatewaydata['transactionId'] = $this->payment->Identifier; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// We only look for a card if we aren't already provided with a token |
|
51
|
|
|
// Increasingly we can expect tokens or nonce's to be more common (e.g. Stripe and Braintree) |
|
52
|
|
|
if (empty($gatewaydata['token'])) { |
|
53
|
|
|
$gatewaydata['card'] = $this->getCreditCard($data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->extend('onBeforePurchase', $gatewaydata); |
|
57
|
|
|
$request = $this->oGateway()->purchase($gatewaydata); |
|
|
|
|
|
|
58
|
|
|
$this->extend('onAfterPurchase', $request); |
|
59
|
|
|
|
|
60
|
|
|
$message = $this->createMessage('PurchaseRequest', $request); |
|
61
|
|
|
$message->SuccessURL = $this->returnurl; |
|
62
|
|
|
$message->FailureURL = $this->cancelurl; |
|
63
|
|
|
$message->write(); |
|
64
|
|
|
|
|
65
|
|
|
$gatewayresponse = $this->createGatewayResponse(); |
|
66
|
|
|
try { |
|
67
|
|
|
$response = $this->response = $request->send(); |
|
68
|
|
|
$this->extend('onAfterSendPurchase', $request, $response); |
|
69
|
|
|
$gatewayresponse->setOmnipayResponse($response); |
|
70
|
|
|
//update payment model |
|
71
|
|
|
if (GatewayInfo::is_manual($this->payment->Gateway)) { |
|
|
|
|
|
|
72
|
|
|
//initiate manual payment |
|
73
|
|
|
$this->createMessage('AuthorizedResponse', $response); |
|
74
|
|
|
$this->payment->Status = 'Authorized'; |
|
|
|
|
|
|
75
|
|
|
$this->payment->write(); |
|
76
|
|
|
$gatewayresponse->setMessage("Manual payment authorised"); |
|
77
|
|
View Code Duplication |
} elseif ($response->isSuccessful()) { |
|
|
|
|
|
|
78
|
|
|
//successful payment |
|
79
|
|
|
$this->createMessage('PurchasedResponse', $response); |
|
80
|
|
|
$this->payment->Status = 'Captured'; |
|
|
|
|
|
|
81
|
|
|
$gatewayresponse->setMessage("Payment successful"); |
|
82
|
|
|
$this->payment->write(); |
|
83
|
|
|
$this->payment->extend('onCaptured', $gatewayresponse); |
|
84
|
|
|
} elseif ($response->isRedirect()) { |
|
85
|
|
|
// redirect to off-site payment gateway |
|
86
|
|
|
$this->createMessage('PurchaseRedirectResponse', $response); |
|
87
|
|
|
$this->payment->Status = 'Authorized'; |
|
|
|
|
|
|
88
|
|
|
$this->payment->write(); |
|
89
|
|
|
$gatewayresponse->setMessage("Redirecting to gateway"); |
|
90
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
91
|
|
|
//handle error |
|
92
|
|
|
$this->createMessage('PurchaseError', $response); |
|
93
|
|
|
$gatewayresponse->setMessage( |
|
94
|
|
|
"Error (".$response->getCode()."): ".$response->getMessage() |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} catch (Omnipay\Common\Exception\OmnipayException $e) { |
|
98
|
|
|
$this->createMessage('PurchaseError', $e); |
|
|
|
|
|
|
99
|
|
|
$gatewayresponse->setMessage($e->getMessage()); |
|
100
|
|
|
} |
|
101
|
|
|
$gatewayresponse->setRedirectURL($this->getRedirectURL()); |
|
102
|
|
|
|
|
103
|
|
|
return $gatewayresponse; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Finalise this payment, after off-site external processing. |
|
108
|
|
|
* This is ususally only called by PaymentGatewayController. |
|
109
|
|
|
* @return GatewayResponse encapsulated response info |
|
110
|
|
|
*/ |
|
111
|
|
|
public function completePurchase($data = array()) { |
|
112
|
|
|
$gatewayresponse = $this->createGatewayResponse(); |
|
113
|
|
|
|
|
114
|
|
|
//set the client IP address, if not already set |
|
115
|
|
|
if(!isset($data['clientIp'])){ |
|
116
|
|
|
$data['clientIp'] = Controller::curr()->getRequest()->getIP(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$gatewaydata = array_merge($data, array( |
|
120
|
|
|
'amount' => (float) $this->payment->MoneyAmount, |
|
|
|
|
|
|
121
|
|
|
'currency' => $this->payment->MoneyCurrency |
|
|
|
|
|
|
122
|
|
|
)); |
|
123
|
|
|
|
|
124
|
|
|
$this->payment->extend('onBeforeCompletePurchase', $gatewaydata); |
|
125
|
|
|
$request = $this->oGateway()->completePurchase($gatewaydata); |
|
|
|
|
|
|
126
|
|
|
$this->payment->extend('onAfterCompletePurchase', $request); |
|
127
|
|
|
|
|
128
|
|
|
$this->createMessage('CompletePurchaseRequest', $request); |
|
129
|
|
|
$response = null; |
|
130
|
|
|
try { |
|
131
|
|
|
$response = $this->response = $request->send(); |
|
132
|
|
|
$this->extend('onAfterSendCompletePurchase', $request, $response); |
|
133
|
|
|
$gatewayresponse->setOmnipayResponse($response); |
|
134
|
|
View Code Duplication |
if ($response->isSuccessful()) { |
|
|
|
|
|
|
135
|
|
|
$this->createMessage('PurchasedResponse', $response); |
|
136
|
|
|
$this->payment->Status = 'Captured'; |
|
|
|
|
|
|
137
|
|
|
$this->payment->write(); |
|
138
|
|
|
$this->payment->extend('onCaptured', $gatewayresponse); |
|
139
|
|
|
} else { |
|
140
|
|
|
$this->createMessage('CompletePurchaseError', $response); |
|
141
|
|
|
} |
|
142
|
|
|
} catch (Omnipay\Common\Exception\OmnipayException $e) { |
|
143
|
|
|
$this->createMessage("CompletePurchaseError", $e); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $gatewayresponse; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function cancelPurchase() { |
|
150
|
|
|
//TODO: do lookup? / try to complete purchase? |
|
151
|
|
|
//TODO: omnipay void call |
|
152
|
|
|
$this->payment->Status = 'Void'; |
|
|
|
|
|
|
153
|
|
|
$this->payment->write(); |
|
154
|
|
|
$this->createMessage('VoidRequest', array( |
|
155
|
|
|
"Message" => "The payment was cancelled." |
|
156
|
|
|
)); |
|
157
|
|
|
|
|
158
|
|
|
//return response |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return \Omnipay\Common\CreditCard |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getCreditCard($data) { |
|
165
|
|
|
return new CreditCard($data); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.