1 | <?php |
||
7 | class AdaptivePayments |
||
8 | { |
||
9 | use PayPalAPIRequest; |
||
10 | |||
11 | /** |
||
12 | * PayPal Processor Constructor. |
||
13 | */ |
||
14 | public function __construct() |
||
19 | |||
20 | /** |
||
21 | * Set AdaptivePayments API endpoints & options. |
||
22 | * |
||
23 | * @return void |
||
24 | */ |
||
25 | public function setAdaptivePaymentsOptions() |
||
26 | { |
||
27 | if ($this->mode == 'sandbox') { |
||
28 | $this->config['api_url'] = 'https://svcs.sandbox.paypal.com/AdaptivePayments'; |
||
29 | $this->config['gateway_url'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; |
||
30 | } else { |
||
31 | $this->config['api_url'] = 'https://svcs.paypal.com/AdaptivePayments'; |
||
32 | $this->config['gateway_url'] = 'https://www.paypal.com/cgi-bin/webscr'; |
||
33 | } |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Set Adaptive Payments API request headers. |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | private function setHeaders() |
||
42 | { |
||
43 | $headers = [ |
||
44 | 'X-PAYPAL-SECURITY-USERID' => $this->config['username'], |
||
45 | 'X-PAYPAL-SECURITY-PASSWORD' => $this->config['password'], |
||
46 | 'X-PAYPAL-SECURITY-SIGNATURE' => $this->config['signature'], |
||
47 | 'X-PAYPAL-REQUEST-DATA-FORMAT' => 'JSON', |
||
48 | 'X-PAYPAL-RESPONSE-DATA-FORMAT' => 'JSON', |
||
49 | 'X-PAYPAL-APPLICATION-ID' => $this->config['app_id'], |
||
50 | ]; |
||
51 | |||
52 | return $headers; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Set Adaptive Payments API request envelope. |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | private function setEnvelope() |
||
61 | { |
||
62 | $envelope = [ |
||
63 | 'errorLanguage' => 'en_US', |
||
64 | 'detailLevel' => 'ReturnAll', |
||
65 | ]; |
||
66 | |||
67 | return $envelope; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Function to perform Adaptive Payments API's PAY operation. |
||
72 | * |
||
73 | * @param array $data |
||
74 | * |
||
75 | * @throws \Exception |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function createPayRequest($data) |
||
80 | { |
||
81 | if (empty($data['return_url']) && empty($data['cancel_url'])) { |
||
82 | throw new \Exception('Return & Cancel URL should be specified'); |
||
83 | } |
||
84 | |||
85 | $this->setRequestData([ |
||
86 | 'actionType' => 'PAY', |
||
87 | 'currencyCode' => $this->currency, |
||
88 | 'receiverList' => [ |
||
89 | 'receiver' => $data['receivers'], |
||
90 | ], |
||
91 | 'returnUrl' => $data['return_url'], |
||
92 | 'cancelUrl' => $data['cancel_url'], |
||
93 | 'requestEnvelope' => $this->setEnvelope(), |
||
94 | 'feesPayer' => $data['payer'], |
||
95 | ])->filter(function ($value, $key) use ($data) { |
||
96 | return (($key === 'feesPayer') && empty($value)) ?: $value; |
||
97 | }); |
||
98 | |||
99 | return $this->doPayPalRequest('Pay'); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Function to perform Adaptive Payments API's SetPaymentOptions operation. |
||
104 | * |
||
105 | * @param string $payKey |
||
106 | * @param array $receivers |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function setPaymentOptions($payKey, $receivers) |
||
111 | { |
||
112 | $this->setRequestData([ |
||
113 | 'requestEnvelope' => $this->setEnvelope(), |
||
114 | 'payKey' => $payKey, |
||
115 | ]); |
||
116 | |||
117 | $receiverOptions = collect($receivers)->map(function ($receiver) { |
||
118 | $item = []; |
||
119 | |||
120 | $item['receiver'] = [ |
||
121 | 'email' => $receiver['email'], |
||
122 | ]; |
||
123 | |||
124 | $item['invoiceData']['item'] = collect($receiver['invoice_data'])->map(function ($invoice) { |
||
125 | return $invoice; |
||
126 | })->toArray(); |
||
127 | |||
128 | $item['description'] = $receiver['description']; |
||
129 | |||
130 | return $item; |
||
131 | })->toArray(); |
||
132 | |||
133 | $this->post = $this->post->merge([ |
||
134 | 'receiverOptions' => $receiverOptions |
||
135 | ]); |
||
136 | |||
137 | return $this->doPayPalRequest('SetPaymentOptions'); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Function to perform Adaptive Payments API's GetPaymentOptions operation. |
||
142 | * |
||
143 | * @param string $payKey |
||
144 | * @param bool $details |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function getPaymentOptions($payKey, $details = false) |
||
149 | { |
||
150 | $operation = ($details) ? 'PaymentDetails' : 'GetPaymentOptions'; |
||
151 | |||
152 | $this->setRequestData([ |
||
153 | 'requestEnvelope' => $this->setEnvelope(), |
||
154 | 'payKey' => $payKey, |
||
155 | ]); |
||
156 | |||
157 | return $this->doPayPalRequest($operation); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Function to perform Adaptive Payments API's PaymentDetails operation. |
||
162 | * |
||
163 | * @param string $payKey |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | public function getPaymentDetails($payKey) |
||
168 | { |
||
169 | return $this->getPaymentOptions($payKey, true); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Get PayPal redirect url for processing payment. |
||
174 | * |
||
175 | * @param string $option |
||
176 | * @param string $payKey |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getRedirectUrl($option, $payKey) |
||
192 | |||
193 | /** |
||
194 | * Create request payload to be sent to PayPal. |
||
195 | * |
||
196 | * @param string $method |
||
197 | */ |
||
198 | private function createRequestPayload($method) |
||
199 | { |
||
204 | |||
205 | /** |
||
206 | * Perform PayPal API request & return response. |
||
207 | * |
||
208 | * @throws \Exception |
||
209 | * |
||
210 | * @return \Psr\Http\Message\StreamInterface |
||
211 | */ |
||
212 | private function makeHttpRequest() |
||
227 | |||
228 | /** |
||
229 | * Function To Perform PayPal API Request. |
||
230 | * |
||
231 | * @param string $method |
||
232 | * |
||
233 | * @throws \Exception |
||
234 | * |
||
235 | * @return array|mixed|\Psr\Http\Message\StreamInterface |
||
236 | */ |
||
237 | private function doPayPalRequest($method) |
||
255 | } |
||
256 |