/** * @param PaymentInterface $payment * @throws InvalidPaymentException */ public function check(PaymentInterface $payment) { if ($payment->getTransaction()) { throw new InvalidPaymentException('Payment has already been received.'); } $credentials = new OAuthTokenCredential($this->options['client_id'], $this->options['secret']); $apiContext = new ApiContext($credentials); $apiContext->setConfig(['mode' => $this->options['mode']]); $paypalPayment = Payment::get($payment->getExtraData('paypal_payment_id'), $apiContext); $payer = $paypalPayment->getPayer(); if (!$payer || 'verified' !== strtolower($payer->getStatus())) { throw new InvalidPaymentException('Payer not verified.'); } if ('created' == $paypalPayment->getState()) { $execution = new PaymentExecution(); $execution->setPayerId($paypalPayment->getPayer()->getPayerInfo()->getPayerId()); $paypalPayment->execute($execution, $apiContext); } if ('approved' != $paypalPayment->getState()) { throw new InvalidPaymentException('Invalid payment state.'); } $math = new NativeMath(); $controlSum = 0; foreach ($paypalPayment->getTransactions() as $transaction) { if ($transaction->getAmount()->getCurrency() != $payment->getAccount()->getCurrency()) { throw new InvalidPaymentException('Invalid payment currency.'); } $controlSum = $math->sum($controlSum, $transaction->getAmount()->getTotal()); } if (!$math->eq($payment->getPaymentSum(), $controlSum)) { throw new InvalidPaymentException('Invalid payment sum.'); } }