-
-
Save EmanueleMinotto/11170093 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Foo\XXXBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use JMS\Payment\CoreBundle\Plugin\Exception\CommunicationException; | |
use JMS\Payment\CoreBundle\Entity\Payment; | |
use JMS\Payment\CoreBundle\Plugin\PluginInterface; | |
use JMS\Payment\CoreBundle\Entity\FinancialTransaction; | |
/** | |
* @author Kotlyar Maksim <[email protected]> | |
* @since 4/20/12 | |
*/ | |
class PaymentSyncApprovingStatusCommand extends ContainerAwareCommand | |
{ | |
/** | |
* Configure command, set parameters definition and help. | |
*/ | |
protected function configure() | |
{ | |
$this | |
->setName('app:payment:sync-approving-status') | |
->setDescription('Asks payment gateway about payment status') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
/** | |
* @var $approvingPayment \JMS\Payment\CoreBundle\Entity\Payment | |
*/ | |
foreach ($this->getPaymentRepository()->findApproving() as $approvingPayment) { | |
try { | |
if ($approvingPayment->getPaymentInstruction()->getPaymentSystemName() != 'paypal_express_checkout') { | |
$output->writeln("\tSkipped. Not a paypal express checkout payment"); | |
continue; | |
} | |
if (false == $pendingTransaction = $approvingPayment->getPendingTransaction()) { | |
$output->writeln("\tError. pending payment does not have pending transaction."); | |
continue; | |
} | |
$transactionId = $pendingTransaction->getReferenceNumber(); | |
$output->writeln('Sync payment (id: '.$approvingPayment->getId().', transaction: '.$transactionId.'):'); | |
$response = $this->getPaypalClient()->requestGetTransactionDetails($transactionId); | |
if (false == $response->isSuccess()) { | |
$output->writeln("\tSkipped. Request is not success. ".$response->getErrorsAsString()); | |
continue; | |
} | |
$paymentStatus = $response->body->get('PAYMENTSTATUS'); | |
$transaction = new FinancialTransaction(); | |
$transaction->setReferenceNumber($transactionId); | |
$transaction->setTransactionType(FinancialTransaction::TRANSACTION_TYPE_APPROVE); | |
$transaction->setPayment($approvingPayment); | |
$transaction->setRequestedAmount(0); | |
$approvingPayment->addTransaction($transaction); | |
if (in_array($paymentStatus, array('Completed', 'Processed'))) { | |
$approvingPayment->setApprovedAmount($approvingPayment->getApprovingAmount()); | |
$approvingPayment->setDepositedAmount($approvingPayment->getDepositingAmount()); | |
$approvingPayment->setApprovingAmount(0.0); | |
$approvingPayment->setDepositingAmount(0.0); | |
$approvingPayment->setState(Payment::STATE_APPROVED); | |
$transaction->setState(FinancialTransaction::STATE_SUCCESS); | |
$transaction->setResponseCode($paymentStatus); | |
$transaction->setReasonCode($response->body->get('REASONCODE')); | |
$output->writeln("\tApproved."); | |
} else if ('Pending' === $paymentStatus) { | |
$transaction->setResponseCode($paymentStatus); | |
$transaction->setReasonCode($response->body->get('PENDINGREASON')); | |
$transaction->setState(FinancialTransaction::STATE_PENDING); | |
$output->writeln("\tAppronvig. Reason: ".$response->body->get('PENDINGREASON')); | |
} else if (in_array($paymentStatus, array('Failed', 'Expired', 'Denied', 'Canceled-Reversal'))) { | |
$approvingPayment->setState(Payment::STATE_FAILED); | |
$transaction->setResponseCode($paymentStatus); | |
$transaction->setReasonCode($response->body->get('REASONCODE')); | |
$transaction->setState(FinancialTransaction::STATE_FAILED); | |
$output->writeln("\tFailed. Status: {$paymentStatus}, Reason: ".$response->body->get('REASONCODE')); | |
} else { | |
$transaction->setResponseCode($paymentStatus); | |
$transaction->setReasonCode($response->body->get('REASONCODE')); | |
$transaction->setState(FinancialTransaction::STATE_FAILED); | |
$output->writeln("\tNot supported. Status: {$paymentStatus}"); | |
} | |
$this->getEntityManager()->persist($transaction); | |
$this->getEntityManager()->persist($approvingPayment); | |
$this->getEntityManager()->flush(); | |
} catch (CommunicationException $e) { | |
$output->writeln("\tSkipped. Request status is not OK"); | |
} catch (\Exception $e) { | |
$output->writeln("\tSkipped. An exception thrown. ".$e->getMessage()); | |
} | |
} | |
} | |
/** | |
* @return \Rj\PaymentBundle\Entity\PaymentRepository | |
*/ | |
protected function getPaymentRepository() | |
{ | |
return $this->getContainer()->get('payment.repository.payment'); | |
} | |
/** | |
* @return \JMS\Payment\PaypalBundle\Paypal\Client | |
*/ | |
protected function getPaypalClient() | |
{ | |
return $this->getContainer()->get('payment.paypal.client'); | |
} | |
/** | |
* @return \Doctrine\ORM\EntityManager | |
*/ | |
protected function getEntityManager() | |
{ | |
return $this->getContainer()->get('doctrine.orm.entity_manager'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment