1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Omnipay\Spreedly\Message\Concerns;
|
4
|
|
|
|
5
|
|
|
use Omnipay\Common\CreditCard;
|
6
|
|
|
use Omnipay\Common\Exception\InvalidRequestException;
|
7
|
|
|
use Omnipay\Spreedly\BankAccount;
|
8
|
|
|
|
9
|
|
|
trait HasPaymentMethodData
|
10
|
|
|
{
|
11
|
|
|
/**
|
12
|
|
|
* Get the bank account.
|
13
|
|
|
*
|
14
|
|
|
* @return BankAccount
|
15
|
|
|
*/
|
16
|
4 |
|
public function getBankAccount()
|
17
|
|
|
{
|
18
|
4 |
|
return $this->getParameter('bank_account');
|
|
|
|
|
19
|
|
|
}
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Sets the card.
|
23
|
|
|
*
|
24
|
|
|
* @param BankAccount $value
|
25
|
|
|
* @return AbstractRequest Provides a fluent interface
|
26
|
|
|
*/
|
27
|
4 |
|
public function setBankAccount($value)
|
28
|
|
|
{
|
29
|
4 |
|
if ($value && !$value instanceof BankAccount) {
|
30
|
4 |
|
$value = new BankAccount($value);
|
31
|
4 |
|
}
|
32
|
|
|
|
33
|
4 |
|
return $this->setParameter('bank_account', $value);
|
|
|
|
|
34
|
|
|
}
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* @return array
|
38
|
|
|
* @throws InvalidRequestException
|
39
|
|
|
* @throws \Omnipay\Common\Exception\InvalidCreditCardException
|
40
|
|
|
* @throws \Omnipay\Spreedly\Exception\InvalidPaymentMethodException
|
41
|
|
|
*/
|
42
|
22 |
|
protected function validateAndGetPaymentMethodData()
|
43
|
|
|
{
|
44
|
22 |
|
$data = array();
|
45
|
|
|
|
46
|
|
|
// Set data depending on payment method
|
47
|
22 |
|
if ($this->parameters->has('token')) {
|
48
|
|
|
// Token payment method
|
49
|
12 |
|
$data['payment_method_token'] = $this->getToken();
|
|
|
|
|
50
|
22 |
|
} elseif ($this->parameters->has('card')) {
|
|
|
|
|
51
|
|
|
// Card payment method
|
52
|
|
|
/** @var CreditCard $card */
|
53
|
10 |
|
$card = $this->getCard();
|
|
|
|
|
54
|
10 |
|
$card->validate();
|
55
|
|
|
|
56
|
10 |
|
$data['credit_card'] = array(
|
57
|
10 |
|
'first_name' => $card->getFirstName(),
|
58
|
10 |
|
'last_name' => $card->getLastName(),
|
59
|
10 |
|
'number' => $card->getNumber(),
|
60
|
10 |
|
'verification_value' => (string) $card->getCvv(),
|
61
|
10 |
|
'month' => (string) $card->getExpiryMonth(),
|
62
|
10 |
|
'year' => (string) $card->getExpiryYear(),
|
63
|
|
|
);
|
64
|
10 |
|
} elseif ($this->parameters->has('bank_account')) {
|
65
|
|
|
// Bank Account payment method
|
66
|
|
|
/** @var BankAccount $bankAccount */
|
67
|
4 |
|
$bankAccount = $this->getBankAccount();
|
68
|
4 |
|
$bankAccount->validate();
|
69
|
|
|
|
70
|
4 |
|
$data['bank_account'] = array(
|
71
|
4 |
|
'first_name' => $bankAccount->getFirstName(),
|
72
|
4 |
|
'last_name' => $bankAccount->getLastName(),
|
73
|
4 |
|
'bank_account_number' => (string) $bankAccount->getNumber(),
|
74
|
4 |
|
'bank_routing_number' => (string) $bankAccount->getRoutingNumber(),
|
75
|
4 |
|
'bank_account_type' => $bankAccount->getType(),
|
76
|
4 |
|
'bank_account_holder_type' => $bankAccount->getHolderType(),
|
77
|
|
|
);
|
78
|
4 |
|
} else {
|
79
|
|
|
// ToDo: Implement other payment methods (Android and Apple Pay...)
|
80
|
|
|
throw new InvalidRequestException("Missing payment method.");
|
81
|
|
|
}
|
82
|
|
|
|
83
|
22 |
|
return $data;
|
84
|
|
|
}
|
85
|
|
|
|
86
|
14 |
|
public function getRetainOnSuccess()
|
87
|
|
|
{
|
88
|
14 |
|
return $this->getParameter('retain_on_success');
|
|
|
|
|
89
|
|
|
}
|
90
|
|
|
|
91
|
6 |
|
public function setRetainOnSuccess($value)
|
92
|
|
|
{
|
93
|
6 |
|
return $this->setParameter('retain_on_success', $value);
|
|
|
|
|
94
|
|
|
}
|
95
|
|
|
}
|
96
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.