From d96ea4c763f9604ed5b54f4377a34aa5334bd001 Mon Sep 17 00:00:00 2001 From: enggar appkey Date: Sat, 25 Jun 2022 10:13:21 +0800 Subject: [PATCH] chore: add api create payload from array --- src/Contract/Payload.php | 8 +++++ src/Contract/PaymentMethod/CreditCard.php | 24 +++++++++++++++ src/LaravelSoftbank.php | 7 ++++- src/PaymentMethod/SoftbankPayload.php | 37 +++++++++++++++++++++++ tests/LaravelSoftbankTest.php | 34 +++++++++++++++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) diff --git a/src/Contract/Payload.php b/src/Contract/Payload.php index 262fcad..3756a59 100644 --- a/src/Contract/Payload.php +++ b/src/Contract/Payload.php @@ -7,6 +7,14 @@ interface Payload { + + /** + * Compose payload data + * + * @return self + */ + public static function compose(array $params): self; + /** * Get the value of merchantId * diff --git a/src/Contract/PaymentMethod/CreditCard.php b/src/Contract/PaymentMethod/CreditCard.php index 9df509d..42f4851 100644 --- a/src/Contract/PaymentMethod/CreditCard.php +++ b/src/Contract/PaymentMethod/CreditCard.php @@ -24,5 +24,29 @@ public abstract function partialRefundTransaction(Payload $payload): Response; * @return Response */ public abstract function saveCard(Payload $payload): Response; + + /** + * saveCardToken + * + * @param \Nekoding\LaravelSoftbank\Contract\Payload $payload + * @return Response + */ + // public abstract function saveCardToken(Payload $payload): Response; + + /** + * updateCard + * + * @param \Nekoding\LaravelSoftbank\Contract\Payload $payload + * @return Response + */ + // public abstract function updateCard(Payload $payload): Response; + + /** + * deleteCard + * + * @param \Nekoding\LaravelSoftbank\Contract\Payload $payload + * @return Response + */ + // public abstract function deleteCard(Payload $payload): Response; } \ No newline at end of file diff --git a/src/LaravelSoftbank.php b/src/LaravelSoftbank.php index 1f32e83..1beeaac 100644 --- a/src/LaravelSoftbank.php +++ b/src/LaravelSoftbank.php @@ -10,7 +10,12 @@ class LaravelSoftbank { - + + /** + * payload + * + * @return \Nekoding\LaravelSoftbank\PaymentMethod\SoftbankPayload + */ public function payload(): Payload { return new SoftbankPayload(); diff --git a/src/PaymentMethod/SoftbankPayload.php b/src/PaymentMethod/SoftbankPayload.php index 3652428..d1fe7db 100644 --- a/src/PaymentMethod/SoftbankPayload.php +++ b/src/PaymentMethod/SoftbankPayload.php @@ -5,6 +5,18 @@ use Nekoding\LaravelSoftbank\Contract\Payload; use Symfony\Component\Serializer\Annotation\SerializedName; use Symfony\Component\Serializer\Annotation\Ignore; +use Doctrine\Common\Annotations\AnnotationReader; +use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Encoder\XmlEncoder; +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; +use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; +use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; +use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; +use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; +use Symfony\Component\Serializer\Serializer; class SoftbankPayload implements Payload { @@ -106,6 +118,31 @@ public function getMerchantId(): ?string { return $this->merchantId; } + + /** + * compose payload parameters + * + * @param array $params + * @return self + * + */ + public static function compose(array $params): Payload + { + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); + + $metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory); + + $objectNormalizer = new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter); + + $normalizers = [$objectNormalizer, new ArrayDenormalizer(), new GetSetMethodNormalizer()]; + + $serializer = new Serializer($normalizers, []); + + /** + * @return self + */ + return $serializer->denormalize($params, self::class); + } /** * Set the value of merchantId diff --git a/tests/LaravelSoftbankTest.php b/tests/LaravelSoftbankTest.php index 44224ea..76e8c85 100644 --- a/tests/LaravelSoftbankTest.php +++ b/tests/LaravelSoftbankTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Http; use Nekoding\LaravelSoftbank\LaravelSoftbank; use Nekoding\LaravelSoftbank\PaymentMethod\CreditCard\CreditCardPayload; +use Nekoding\LaravelSoftbank\PaymentMethod\SoftbankPayload; use Orchestra\Testbench\Concerns\CreatesApplication; class LaravelSoftbankTest extends TestCase @@ -581,4 +582,37 @@ public function test_payment_using_saved_card_failed() $this->assertNotNull($res->getErrorMessages()); } + public function test_compose_array_params() + { + + $params = [ + 'merchant_id' => '12345', + 'service_id' => '001', + 'cust_code' => '001', + 'order_id' => '001', + 'item_id' => '001', + 'amount' => 001, + 'request_date' => '20220720110909', + 'sps_hashcode' => sha1( + 'merchant_id' . + 'service_id' . + 'customer id' . + 'order id' . + 'item id' . + 'amount' . + 'request date' . + 'hash key' + ) + ]; + + $softbankPayload = SoftbankPayload::compose($params); + + $this->assertTrue($softbankPayload instanceof SoftbankPayload); + + $this->assertEquals($params['merchant_id'], $softbankPayload->getMerchantId()); + $this->assertEquals($params['service_id'], $softbankPayload->getServiceId()); + $this->assertEquals($params['cust_code'], $softbankPayload->getCustomerCode()); + + } + } \ No newline at end of file