Skip to content

Commit

Permalink
chore: add api create payload from array
Browse files Browse the repository at this point in the history
  • Loading branch information
enggar appkey committed Jun 25, 2022
1 parent 0d4ee7d commit d96ea4c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Contract/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

interface Payload
{

/**
* Compose payload data
*
* @return self
*/
public static function compose(array $params): self;

/**
* Get the value of merchantId
*
Expand Down
24 changes: 24 additions & 0 deletions src/Contract/PaymentMethod/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
7 changes: 6 additions & 1 deletion src/LaravelSoftbank.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
class LaravelSoftbank
{



/**
* payload
*
* @return \Nekoding\LaravelSoftbank\PaymentMethod\SoftbankPayload
*/
public function payload(): Payload
{
return new SoftbankPayload();
Expand Down
37 changes: 37 additions & 0 deletions src/PaymentMethod/SoftbankPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/LaravelSoftbankTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());

}

}

0 comments on commit d96ea4c

Please sign in to comment.