1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onend\PayPal\Payment\Client; |
4
|
|
|
|
5
|
|
|
use Guzzle\Http\Message\Response; |
6
|
|
|
|
7
|
|
|
use Onend\PayPal\Common\Client\AbstractAuthenticatedClient; |
8
|
|
|
use Onend\PayPal\Common\Enum\Endpoint; |
9
|
|
|
use Onend\PayPal\Common\Enum\RequestFormat; |
10
|
|
|
use Onend\PayPal\Payment\Model\Payment; |
11
|
|
|
use Onend\PayPal\Payment\Model\PaymentList; |
12
|
|
|
|
13
|
|
|
class PaymentClient extends AbstractAuthenticatedClient |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Create a payment |
17
|
|
|
* |
18
|
|
|
* @param Payment $payment |
19
|
|
|
* |
20
|
|
|
* @return Payment |
21
|
|
|
*/ |
22
|
|
|
public function createPayment(Payment $payment) |
23
|
|
|
{ |
24
|
|
|
$request = $this->post( |
25
|
|
|
Endpoint::CREATE_PAYMENT, |
26
|
|
|
[], |
27
|
|
|
$this->getSerializer()->serialize($payment, RequestFormat::JSON) |
28
|
|
|
); |
29
|
|
|
$response = $this->send($request); |
30
|
|
|
|
31
|
|
|
return $this->factoryPaymentResponse($response); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute an approved PayPal payment by Payment |
36
|
|
|
* |
37
|
|
|
* @param Payment $payment |
38
|
|
|
* |
39
|
|
|
* @return Payment |
40
|
|
|
*/ |
41
|
|
|
public function executePayment(Payment $payment) |
42
|
|
|
{ |
43
|
|
|
return $this->executePaymentByIds($payment->getId(), $payment->getPayer()->getPayerInfo()->getPayerId()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Execute an approved PayPal payment by payerId and paymentId |
48
|
|
|
* |
49
|
|
|
* @param string $paymentId |
50
|
|
|
* @param string $payerId |
51
|
|
|
* |
52
|
|
|
* @return Payment |
53
|
|
|
*/ |
54
|
|
|
public function executePaymentByIds($paymentId, $payerId) |
55
|
|
|
{ |
56
|
|
|
$data = json_encode(["payer_id" => $payerId]); |
57
|
|
|
$request = $this->post(Endpoint::EXECUTE_PAYMENT, null, $data, ["paymenId" => $paymentId]); |
58
|
|
|
$response = $this->send($request); |
59
|
|
|
|
60
|
|
|
return $this->factoryPaymentResponse($response); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Look up a payment resource |
65
|
|
|
* |
66
|
|
|
* @param string $paymentId |
67
|
|
|
* |
68
|
|
|
* @return Payment |
69
|
|
|
*/ |
70
|
|
|
public function lookupPayment($paymentId) |
71
|
|
|
{ |
72
|
|
|
$request = $this->get(Endpoint::LOOKUP_PAYMENT, null, ["paymenId" => $paymentId]); |
73
|
|
|
$response = $this->send($request); |
74
|
|
|
|
75
|
|
|
return $this->factoryPaymentResponse($response); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return PaymentList |
80
|
|
|
*/ |
81
|
|
|
public function listPayments() |
82
|
|
|
{ |
83
|
|
|
$request = $this->get(Endpoint::LIST_PAYMENTS); |
84
|
|
|
$response = $this->send($request); |
85
|
|
|
|
86
|
|
|
return $this->getSerializer()->deserialize( |
|
|
|
|
87
|
|
|
$response->getBody(true), |
88
|
|
|
PaymentList::getClass(), |
89
|
|
|
RequestFormat::JSON |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $response |
95
|
|
|
* |
96
|
|
|
* @return Payment |
97
|
|
|
*/ |
98
|
|
|
protected function factoryPaymentResponse(Response $response) |
99
|
|
|
{ |
100
|
|
|
return $this->getSerializer()->deserialize($response->getBody(true), Payment::getClass(), RequestFormat::JSON); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.