|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\Braintree\Business\Payment\Transaction; |
|
9
|
|
|
|
|
10
|
|
|
use Braintree\Exception\NotFound; |
|
11
|
|
|
use Braintree\Result\Error; |
|
12
|
|
|
use Braintree\Result\Successful; |
|
13
|
|
|
use Braintree\Transaction as BraintreeTransaction; |
|
14
|
|
|
use SprykerEco\Zed\Braintree\Business\Payment\Method\ApiConstants; |
|
15
|
|
|
|
|
16
|
|
|
class AuthorizeTransaction extends AbstractTransaction |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function getTransactionType() |
|
22
|
|
|
{ |
|
23
|
|
|
return ApiConstants::SALE; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function getTransactionCode() |
|
30
|
|
|
{ |
|
31
|
|
|
return ApiConstants::TRANSACTION_CODE_AUTHORIZE; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return \Braintree\Result\Error|\Braintree\Transaction |
|
36
|
|
|
*/ |
|
37
|
|
|
public function doTransaction() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->authorize(); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param \Braintree\Transaction $response |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function isTransactionSuccessful($response) |
|
48
|
|
|
{ |
|
49
|
|
|
return ($response->success && $response->transaction->processorResponseCode === ApiConstants::PAYMENT_CODE_AUTHORIZE_SUCCESS); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return \Braintree\Result\Successful|\Braintree\Result\Error |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function authorize() |
|
56
|
|
|
{ |
|
57
|
|
|
try { |
|
58
|
|
|
$transaction = $this->findTransaction(); |
|
59
|
|
|
} catch (NotFound $e) { |
|
60
|
|
|
$message = sprintf('Could not find payment with the transaction id "%s"', $this->getTransactionIdentifier()); |
|
61
|
|
|
|
|
62
|
|
|
return new Error(['message' => $message, 'errors' => []]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return new Successful($transaction); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return \Braintree\Transaction |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function findTransaction() |
|
72
|
|
|
{ |
|
73
|
|
|
return BraintreeTransaction::find($this->getTransactionIdentifier()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|