1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Traits\PayPalAPI\PaymentMethodsTokens; |
4
|
|
|
|
5
|
|
|
trait Helpers |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
protected $payment_source = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $customer_source = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Set payment method token by token id. |
19
|
|
|
* |
20
|
|
|
* @param string $id |
21
|
|
|
* @param string $type |
22
|
|
|
* |
23
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
24
|
|
|
*/ |
25
|
|
|
public function setTokenSource(string $id, string $type): \Srmklive\PayPal\Services\PayPal |
26
|
|
|
{ |
27
|
|
|
$token_source = [ |
28
|
|
|
'id' => $id, |
29
|
|
|
'type' => $type, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
return $this->setPaymentSourceDetails('token', $token_source); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set payment method token customer id. |
37
|
|
|
* |
38
|
|
|
* @param string $id |
39
|
|
|
* |
40
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
41
|
|
|
*/ |
42
|
|
|
public function setCustomerSource(string $id): \Srmklive\PayPal\Services\PayPal |
43
|
|
|
{ |
44
|
|
|
$this->customer_source = [ |
45
|
|
|
'id' => $id, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set payment source data for credit card. |
53
|
|
|
* |
54
|
|
|
* @param array $data |
55
|
|
|
* |
56
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
57
|
|
|
*/ |
58
|
|
|
public function setPaymentSourceCard(array $data): \Srmklive\PayPal\Services\PayPal |
59
|
|
|
{ |
60
|
|
|
return $this->setPaymentSourceDetails('card', $data); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set payment source data for PayPal. |
65
|
|
|
* |
66
|
|
|
* @param array $data |
67
|
|
|
* |
68
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
69
|
|
|
*/ |
70
|
|
|
public function setPaymentSourcePayPal(array $data): \Srmklive\PayPal\Services\PayPal |
71
|
|
|
{ |
72
|
|
|
return $this->setPaymentSourceDetails('paypal', $data); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set payment source data for Venmo. |
77
|
|
|
* |
78
|
|
|
* @param array $data |
79
|
|
|
* |
80
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
81
|
|
|
*/ |
82
|
|
|
public function setPaymentSourceVenmo(array $data): \Srmklive\PayPal\Services\PayPal |
83
|
|
|
{ |
84
|
|
|
return $this->setPaymentSourceDetails('venmo', $data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set payment source details. |
89
|
|
|
* |
90
|
|
|
* @param string $source |
91
|
|
|
* @param array $data |
92
|
|
|
* |
93
|
|
|
* @return \Srmklive\PayPal\Services\PayPal |
94
|
|
|
*/ |
95
|
|
|
protected function setPaymentSourceDetails(string $source, array $data): \Srmklive\PayPal\Services\PayPal |
96
|
|
|
{ |
97
|
|
|
$this->payment_source[$source] = $data; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Send request for creating payment method token/source. |
104
|
|
|
* |
105
|
|
|
* @param bool $create_source |
106
|
|
|
* |
107
|
|
|
* @throws \Throwable |
108
|
|
|
* |
109
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
110
|
|
|
*/ |
111
|
|
|
public function sendPaymentMethodRequest(bool $create_source = false) |
112
|
|
|
{ |
113
|
|
|
$token_payload = ['payment_source' => $this->payment_source]; |
114
|
|
|
|
115
|
|
|
if (!empty($this->customer_source)) { |
116
|
|
|
$token_payload['customer'] = $this->customer_source; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return ($create_source === true) ? $this->createPaymentSetupToken(array_filter($token_payload)) : $this->createPaymentSourceToken(array_filter($token_payload)); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|