From 996d02302f556fd8ab82b21c8b96d4f66fa1a8f1 Mon Sep 17 00:00:00 2001 From: Sarai Misidjan Date: Mon, 17 Apr 2023 13:23:37 +0200 Subject: [PATCH 1/2] create payment object and return id --- Installation/installation.json | 2 +- src/Service/PaymentService.php | 60 +++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/Installation/installation.json b/Installation/installation.json index 8a7b61a..ebc8fcb 100644 --- a/Installation/installation.json +++ b/Installation/installation.json @@ -8,7 +8,7 @@ "version": "0.0.2", "description": "The Huwelijksplanner Front-end Application. (Domain is used for redirecting after a payment, this might change)", "domains": [ - "utrecht-huwelijksplanner.frameless.io" + "api.huwelijksplanner.online" ] } ], diff --git a/src/Service/PaymentService.php b/src/Service/PaymentService.php index 2dd4a3f..dbf68a8 100644 --- a/src/Service/PaymentService.php +++ b/src/Service/PaymentService.php @@ -298,12 +298,16 @@ public function createMolliePayment(array $paymentArray): ?array * * @return ObjectEntity Huwelijk object. */ - private function validateHuwelijkId(array $query): ObjectEntity + private function validateHuwelijkId(array $query): ?ObjectEntity { - if (isset($query['resource']) === false || Uuid::isValid($query['resource']) === false) { - throw new BadRequestHttpException('No huwelijk id given or false id in the query parameter resource.'); + if (isset($query['resource']) === false) { + return null; }//end if + if (Uuid::isValid($query['resource']) === false) { + throw new BadRequestHttpException('False id in the query parameter resource.'); + } + $huwelijkObject = $this->entityManager->find('App:ObjectEntity', $query['resource']); if ($huwelijkObject instanceof ObjectEntity === false) { throw new BadRequestHttpException('Cannot find huwelijk with given id: '.$query['resource']); @@ -324,34 +328,44 @@ public function createPayment(): ?array // @TODO add the values amount from huwelijk object etc to array $paymentSchema = $this->gatewayResourceService->getSchema('https://huwelijksplanner.nl/schemas/hp.mollie.schema.json', 'common-gateway/huwelijksplanner-bundle'); + $redirectUrl = null; + $application = $this->entityManager->getRepository('App:Application')->findOneBy(['reference' => 'https://huwelijksplanner.nl/application/hp.frontend.application.json']); + if ($application !== null && $application->getDomains() !== null && count($application->getDomains()) > 0) { + $domain = $application->getDomains()[0]; + $redirectUrl = 'https://'.$domain.'/voorgenomen-huwelijk/betalen/betaalstatus-verificatie'; + } + $huwelijkObject = $this->validateHuwelijkId($this->data['query']); + if ($huwelijkObject === null) { + return null; + } // Get all prices from the products $productPrices = $this->getSDGProductPrices($huwelijkObject->toArray()); - // Calculate new price - $kosten = 'EUR '.$this->calculatePrice($productPrices, 'EUR'); - $explodedAmount = explode(' ', $kosten); + $paymentObject = new ObjectEntity($paymentSchema); + $paymentArray = [ + 'amount' => [ + 'currency' => 'EUR', + 'value' => $this->calculatePrice($productPrices, 'EUR'), // Calculate new price + ], + 'description' => 'Payment made for huwelijk with id: '.$huwelijkObject->getId()->toString(), + 'redirectUrl' => $redirectUrl, + 'webhookUrl' => $this->configuration['webhookUrl'], + 'method' => $this->configuration['method'], + 'status' => 'paid' // @TODO temporary set the status to paid + ]; + $paymentObject->hydrate($paymentArray); + $this->entityManager->persist($paymentObject); + $this->entityManager->flush(); - // $paymentArray = [ - // 'amount' => [ - // 'currency' => $explodedAmount[0], - // 'value' => $explodedAmount[1], - // ], - // 'description' => 'Payment made for huwelijk with id: '.$huwelijkObject->getId()->toString(), - // 'redirectUrl' => $this->configuration['redirectUrl'], - // 'webhookUrl' => $this->configuration['webhookUrl'], - // 'method' => $this->configuration['method'], - // ]; // return $this->createMolliePayment($paymentArray); // todo: temporary, redirect to return [redirectUrl]. Instead of this $paymentArray and return^ - $domain = 'utrecht-huwelijksplanner.frameless.io'; - $application = $this->entityManager->getRepository('App:Application')->findOneBy(['reference' => 'https://huwelijksplanner.nl/application/hp.frontend.application.json']); - if ($application !== null && $application->getDomains() !== null && count($application->getDomains()) > 0) { - $domain = $application->getDomains()[0]; - } - return ['redirectUrl' => 'https://'.$domain.'/voorgenomen-huwelijk/betalen/betaalstatus-verificatie']; + return [ + 'paymentId' => $paymentObject->getId()->toString(), + 'redirectUrl' => $paymentObject->getValue('redirectUrl') // @TODO set redirectUrl to the checkout url + ]; }//end createPayment() @@ -389,7 +403,7 @@ public function createPaymentHandler(?array $data=[], ?array $configuration=[]): // ]; // }//end if if ($payment !== null) { - $this->data['response'] = new Response(\Safe\json_encode($payment), 200); + $this->data['response'] = new Response(json_encode($payment), 200); // $this->data['response'] = new Response(\Safe\json_encode(['checkout' => $payment['_links']['checkout']]), 200); } From 17f6f17a6ab41129aa8315d8169af9dd2aec4966 Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Mon, 17 Apr 2023 11:24:06 +0000 Subject: [PATCH 2/2] Update src from PHP Codesniffer --- src/Service/PaymentService.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Service/PaymentService.php b/src/Service/PaymentService.php index dbf68a8..c1e3177 100644 --- a/src/Service/PaymentService.php +++ b/src/Service/PaymentService.php @@ -328,10 +328,10 @@ public function createPayment(): ?array // @TODO add the values amount from huwelijk object etc to array $paymentSchema = $this->gatewayResourceService->getSchema('https://huwelijksplanner.nl/schemas/hp.mollie.schema.json', 'common-gateway/huwelijksplanner-bundle'); - $redirectUrl = null; + $redirectUrl = null; $application = $this->entityManager->getRepository('App:Application')->findOneBy(['reference' => 'https://huwelijksplanner.nl/application/hp.frontend.application.json']); if ($application !== null && $application->getDomains() !== null && count($application->getDomains()) > 0) { - $domain = $application->getDomains()[0]; + $domain = $application->getDomains()[0]; $redirectUrl = 'https://'.$domain.'/voorgenomen-huwelijk/betalen/betaalstatus-verificatie'; } @@ -344,16 +344,18 @@ public function createPayment(): ?array $productPrices = $this->getSDGProductPrices($huwelijkObject->toArray()); $paymentObject = new ObjectEntity($paymentSchema); - $paymentArray = [ + $paymentArray = [ 'amount' => [ 'currency' => 'EUR', - 'value' => $this->calculatePrice($productPrices, 'EUR'), // Calculate new price + 'value' => $this->calculatePrice($productPrices, 'EUR'), + // Calculate new price ], 'description' => 'Payment made for huwelijk with id: '.$huwelijkObject->getId()->toString(), 'redirectUrl' => $redirectUrl, 'webhookUrl' => $this->configuration['webhookUrl'], 'method' => $this->configuration['method'], - 'status' => 'paid' // @TODO temporary set the status to paid + 'status' => 'paid', + // @TODO temporary set the status to paid ]; $paymentObject->hydrate($paymentArray); $this->entityManager->persist($paymentObject); @@ -361,10 +363,10 @@ public function createPayment(): ?array // return $this->createMolliePayment($paymentArray); // todo: temporary, redirect to return [redirectUrl]. Instead of this $paymentArray and return^ - return [ - 'paymentId' => $paymentObject->getId()->toString(), - 'redirectUrl' => $paymentObject->getValue('redirectUrl') // @TODO set redirectUrl to the checkout url + 'paymentId' => $paymentObject->getId()->toString(), + 'redirectUrl' => $paymentObject->getValue('redirectUrl'), + // @TODO set redirectUrl to the checkout url ]; }//end createPayment()