/** * {@inheritdoc} * * @param $request Capture */ public function execute($request) { RequestNotSupportedException::assertSupports($this, $request); /** @var $payment SyliusPaymentInterface */ $payment = $request->getModel(); /** @var OrderInterface $order */ $order = $payment->getOrder(); $this->gateway->execute($status = new GetStatus($payment)); if ($status->isNew()) { try { $this->gateway->execute($convert = new Convert($payment, 'array', $request->getToken())); $payment->setDetails($convert->getResult()); } catch (RequestNotSupportedException $e) { $totalAmount = $order->getTotal(); $payumPayment = new PayumPayment(); $payumPayment->setNumber($order->getNumber()); $payumPayment->setTotalAmount($totalAmount); $payumPayment->setCurrencyCode($order->getCurrencyCode()); $payumPayment->setClientEmail($order->getCustomer()->getEmail()); $payumPayment->setClientId($order->getCustomer()->getId()); $payumPayment->setDescription(sprintf('Payment contains %d items for a total of %01.2f', $order->getItems()->count(), round($totalAmount / 100, 2))); $payumPayment->setDetails($payment->getDetails()); $this->gateway->execute($convert = new Convert($payumPayment, 'array', $request->getToken())); $payment->setDetails($convert->getResult()); } } $details = ArrayObject::ensureArrayObject($payment->getDetails()); try { $request->setModel($details); $this->gateway->execute($request); } finally { $payment->setDetails((array) $details); } }
/** * {@inheritDoc} * * @param $request Authorize */ public function execute($request) { RequestNotSupportedException::assertSupports($this, $request); /** @var $payment Payment */ $payment = $request->getModel(); $this->gateway->execute($status = new GetHumanStatus($payment)); if ($status->isNew()) { $this->gateway->execute(new ObtainMissingDetailsRequest($payment, $request->getToken())); try { $this->gateway->execute($convert = new Convert($payment, 'array', $request->getToken())); $payment->setDetails($convert->getResult()); } catch (RequestNotSupportedException $e) { $payumPayment = new PayumPayment(); $payumPayment->setNumber($payment->getNumber()); $payumPayment->setTotalAmount($payment->getTotalAmount()); $payumPayment->setCurrencyCode($payment->getCurrencyCode()); $payumPayment->setClientEmail($payment->getPayer()->getEmail()); $payumPayment->setClientId($payment->getPayer()->getId() ?: $payment->getPayer()->getEmail()); $payumPayment->setDescription($payment->getDescription() ?: sprintf('Payment %s', $payment->getNumber())); $payumPayment->setCreditCard($payment->getCreditCard()); $payumPayment->setDetails($payment->getDetails()); $this->gateway->execute($convert = new Convert($payumPayment, 'array', $request->getToken())); $payment->setDetails($convert->getResult()); } } $details = ArrayObject::ensureArrayObject($payment->getDetails()); try { $request->setModel($details); $this->gateway->execute($request); } finally { $payment->setDetails((array) $details); } }
/** * @test */ public function shouldNotOverrideProvidesValue() { $payment = new Payment(); $payment->setNumber('1234'); $payment->setCurrencyCode('USD'); $payment->setTotalAmount(123); $payment->setDescription('the description'); $payment->setClientId('theClientId'); $payment->setClientEmail('theClientEmail'); $payment->setDetails(array('Ds_Merchant_MerchantURL' => 'a_merchant_url', 'Ds_Merchant_TransactionType' => 1, 'Ds_Merchant_ConsumerLanguage' => '002')); $apiMock = $this->createApiMock(); $apiMock->expects($this->once())->method('ensureCorrectOrderNumber')->with($payment->getNumber())->willReturn($payment->getNumber()); $apiMock->expects($this->once())->method('getISO4127')->with($payment->getCurrencyCode())->willReturn(840); $apiMock->expects($this->once())->method('getMerchantCode')->willReturn('a_merchant_code'); $apiMock->expects($this->once())->method('getMerchantTerminalCode')->willReturn('001'); $tokenMock = $this->getMock('Payum\\Core\\Security\\TokenInterface'); $action = new ConvertPaymentAction(); $action->setApi($apiMock); $action->execute(new Convert($payment, 'array', $tokenMock)); $details = $payment->getDetails(); $this->assertNotEmpty($details); $this->assertArrayHasKey('Ds_Merchant_MerchantURL', $details); $this->assertEquals('a_merchant_url', $details['Ds_Merchant_MerchantURL']); $this->assertArrayHasKey('Ds_Merchant_TransactionType', $details); $this->assertEquals(1, $details['Ds_Merchant_TransactionType']); $this->assertArrayHasKey('Ds_Merchant_ConsumerLanguage', $details); $this->assertEquals('002', $details['Ds_Merchant_ConsumerLanguage']); }
/** * @test */ public function shouldNotOverwriteAlreadySetExtraDetails() { $order = new Payment(); $order->setCurrencyCode('USD'); $order->setTotalAmount(123); $order->setDescription('the description'); $order->setDetails(array('foo' => 'fooVal')); $action = new ConvertPaymentAction(); $action->execute($convert = new Convert($order, 'array')); $details = $convert->getResult(); $this->assertNotEmpty($details); $this->assertArrayHasKey('foo', $details); $this->assertEquals('fooVal', $details['foo']); }
/** * @test */ public function shouldNotOverwriteAlreadySetExtraDetails() { $gatewayMock = $this->getMock('Payum\\Core\\GatewayInterface'); $gatewayMock->expects($this->once())->method('execute')->with($this->isInstanceOf('Payum\\Core\\Request\\GetCurrency'))->willReturnCallback(function (GetCurrency $request) { $request->name = 'US Dollar'; $request->alpha3 = 'USD'; $request->numeric = 123; $request->exp = 2; $request->country = 'US'; }); $order = new Payment(); $order->setCurrencyCode('USD'); $order->setTotalAmount(123); $order->setDescription('the description'); $order->setDetails(array('foo' => 'fooVal')); $action = new ConvertPaymentAction(); $action->setGateway($gatewayMock); $action->execute($convert = new Convert($order, 'array')); $details = $convert->getResult(); $this->assertNotEmpty($details); $this->assertArrayHasKey('foo', $details); $this->assertEquals('fooVal', $details['foo']); }
/** * @test */ public function shouldCorrectlyConvertOrderToDetailsAndSetItBack() { $payment = new Payment(); $payment->setNumber('theNumber'); $payment->setCurrencyCode('EUR'); $payment->setTotalAmount(1000); $payment->setClientId('theClientId'); $payment->setClientEmail('theClientEmail'); $action = new ConvertPaymentAction(); $action->execute($convert = new Convert($payment, 'array')); $details = $convert->getResult(); $this->assertNotEmpty($details); $this->assertArrayHasKey('merchantReference', $details); $this->assertEquals('theNumber', $details['merchantReference']); $this->assertArrayHasKey('paymentAmount', $details); $this->assertEquals(1000, $details['paymentAmount']); $this->assertArrayHasKey('shopperEmail', $details); $this->assertEquals('theClientEmail', $details['shopperEmail']); $this->assertArrayHasKey('currencyCode', $details); $this->assertEquals('EUR', $details['currencyCode']); $this->assertArrayHasKey('shopperReference', $details); $this->assertEquals('theClientId', $details['shopperReference']); }