public function makePaymentAction(Request $request, $providerId, $productId) { $inputs = $request->get('inputs', []); $form = $this->createFormBuilder()->getForm(); $paymentResponse = null; $product = null; $provider = null; if (!$providerId) { $this->addFlash('warning', 'Select a provider and click on "Account Lookup" to make a payment.'); return $this->redirectToRoute('qpay_api_demo_providers'); } try { $api = $this->buildApiInstance(); $provider = $api->getProvider($providerId); $inputsInstances = []; foreach ($inputs as $label => $value) { $inputsInstances[] = new Input($label, $value); } $accountLookup = $api->accountLookup($providerId, $inputsInstances); foreach ($accountLookup->getCustomerInfos() as $customerInfo) { foreach ($customerInfo->getProductInfos() as $productInfo) { if ($productInfo->getProductId() === $productId) { $product = $productInfo; break 2; } } } if ($product === null) { throw new \RuntimeException('Invalid product'); } $readOnly = $product->getMinAmount() === $product->getMaxAmount(); $form->add('amount', $this->getBCFormType(NumberType::class), ['data' => $product->getDefaultPaymentAmount(), 'constraints' => [new GreaterThanOrEqual(['value' => $product->getMinAmount()]), new LessThanOrEqual(['value' => $product->getMaxAmount()])], 'attr' => ['readonly' => $readOnly, 'max' => $product->getMaxAmount(), 'min' => $product->getMinAmount()]]); $form->add('fee', $this->getBCFormType(NumberType::class), ['data' => 0, 'required' => false]); $form->add('correlationId', $this->getBCFormType(NumberType::class), ['data' => time()]); $form->add('Make Payment', $this->getBCFormType(SubmitType::class), ['attr' => ['class' => 'btn-danger']]); $form->handleRequest($request); if ($form->isValid()) { $payment = new Payment(); $payment->setProductInfo($product)->setAccountLookup($accountLookup)->setAmount($form->get('amount')->getData())->setFee($form->get('fee')->getData())->setInputs($inputsInstances)->setCorrelationId($form->get('correlationId')->getData()); $paymentResponse = $api->makePayment($payment); } } catch (\Exception $e) { $this->addFlash('danger', $e->getMessage()); } return $this->renderViewInLayout('QPayApiDemoBundle::make_payment.html.twig', ['provider' => $provider, 'product' => $product, 'form' => $form->createView(), 'requestStack' => isset($api) ? $api->getDebugger()->getRequestStack() : [], 'paymentResponse' => $paymentResponse]); }
/** * Make a payment * * @param Payment $payment * * @return MakePaymentResponse * @throws ApiException */ public function makePayment(Payment $payment) { $service = new MakePayment(); $customerInfo = $payment->getAccountLookup()->getCustomerInfos()[0]; $customerInfo->setCustomAttributes([]); $customerInfo->setPhoneNumbers([]); $customerInfo->setProductInfos([]); $customerInfo->setLastPaymentMade(0); $service->setCustomerInfo($customerInfo); $service->setProviderId($payment->getAccountLookup()->getProviderId()); $service->setPaymentMethod(0); $paymentInput = new PaymentInput(); $paymentInput->setProductId($payment->getProductInfo()->getProductId()); $paymentInput->setInputs($payment->getInputs()); $service->setPaymentInputs([$paymentInput]); $productPayment = new ProductPayment(); $productPayment->setProductId($paymentInput->getProductId()); $productPayment->setFee($payment->getFee()); $productPayment->setPaymentAmount($payment->getAmount()); $productPayment->setPOSTransactionId($payment->getCorrelationId()); $productPayment->setSalesTax(null); $service->setProductPayments([$productPayment]); /** @var MakePaymentResponse $response */ return $this->process($service); }
public function testMakePaymentPIN() { if (!$this->api->isModeMock()) { static::markTestSkipped('This test only run on MOCK mode.'); } $this->api->getProviders(); $inputs = []; $accountLookup = $this->api->accountLookup(367, $inputs); $product = $accountLookup->getCustomerInfos()[0]->getProductInfos()[0]; $payment = new Payment(); $payment->setProductInfo($product)->setAccountLookup($accountLookup)->setAmount(10)->setInputs($inputs)->setCorrelationId(mt_rand()); $response = $this->api->makePayment($payment); static::assertNotEmpty($response->getProductPayments()[0]->getPaymentResult()->getPIN()); }