Skip to content

Commit

Permalink
Merge pull request #54 from Xaxxis/master
Browse files Browse the repository at this point in the history
v2.4.4 Fix bug feature sanitize, error when request body without optional params
  • Loading branch information
Zaki Ibrahim authored Aug 12, 2021
2 parents 41520bf + 494915d commit f4713de
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Midtrans/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static function remoteCall($url, $server_key, $data_hash, $post = true)
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json',
'User-Agent: midtrans-php-v2.4.2',
'User-Agent: midtrans-php-v2.4.4',
'Authorization: Basic ' . base64_encode($server_key . ':')
),
CURLOPT_RETURNTRANSFER => 1
Expand Down
39 changes: 21 additions & 18 deletions Midtrans/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,25 @@ private static function fieldItemDetails(&$items)

private static function fieldCustomerDetails(&$field)
{
$first_name = new self;
$field['first_name'] = $first_name
->maxLength(20)
->apply($field['first_name']);
if (isset($field['first_name'])) {
$first_name = new self;
$field['first_name'] = $first_name->maxLength(255)->apply($field['first_name']);
}

if (isset($field['last_name'])) {
$last_name = new self;
$field['last_name'] = $last_name
->maxLength(20)
->apply($field['last_name']);
$field['last_name'] = $last_name->maxLength(255)->apply($field['last_name']);
}

if (isset($field['email'])) {
$email = new self;
$field['email'] = $email->maxLength(255)->apply($field['email']);
}
$email = new self;
$field['email'] = $email
->maxLength(45)
->apply($field['email']);

static::fieldPhone($field['phone']);
if (isset($field['phone'])) {
$phone = new self;
$field['phone'] = $phone->maxLength(255)->apply($field['phone']);
}

if (!empty($field['billing_address']) || !empty($field['shipping_address'])) {
$keys = array('billing_address', 'shipping_address');
Expand All @@ -83,11 +86,11 @@ private static function fieldCustomerDetails(&$field)
private static function fieldBillingAddress(&$field)
{
$fields = array(
'first_name' => 20,
'last_name' => 20,
'address' => 200,
'city' => 20,
'country_code' => 10
'first_name' => 255,
'last_name' => 255,
'address' => 255,
'city' => 255,
'country_code' => 3
);

foreach ($fields as $key => $value) {
Expand Down Expand Up @@ -118,7 +121,7 @@ private static function fieldShippingAddress(&$field)

private static function fieldPhone(&$field)
{
$plus = substr($field, 0, 1) === '+' ? true : false;
$plus = substr($field, 0, 1) === '+';
$self = new self;
$field = $self
->whitelist('\\d\\-\\(\\) ')
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "midtrans/midtrans-php",
"description": "PHP Wrapper for Midtrans Payment API.",
"homepage": "https://midtrans.com",
"version": "2.4.3",
"version": "2.4.4",
"type": "library",
"license":"MIT",
"authors": [
Expand Down
82 changes: 82 additions & 0 deletions tests/MidtransSanitizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php


namespace Midtrans;

use Midtrans\utility\MtChargeFixture;

class MidtransSanitizerTest extends \PHPUnit_Framework_TestCase
{

public function testSanitizeWithoutOptionalRequest()
{
$params = MtChargeFixture::build('vtweb');
unset($params['customer_details']);

Sanitizer::jsonRequest($params);
$this->assertEquals(false, isset($params['customer_details']));
}

public function testSanitizeWithoutOptionalCustDetails()
{
$params = MtChargeFixture::build('vtweb');
unset($params['customer_details']['first_name']);
unset($params['customer_details']['last_name']);
unset($params['customer_details']['email']);
unset($params['customer_details']['billing_address']);
unset($params['customer_details']['shipping_address']);

Sanitizer::jsonRequest($params);

$this->assertEquals(false, isset($params['customer_details']['first_name']));
$this->assertEquals(false, isset($params['customer_details']['last_name']));
$this->assertEquals(false, isset($params['customer_details']['email']));
$this->assertEquals(false, isset($params['customer_details']['billing_address']));
$this->assertEquals(false, isset($params['customer_details']['shipping_address']));
}

public function testSanitizeWithoutOptionalInBillingAddress()
{
$params = MtChargeFixture::build('vtweb');
unset($params['customer_details']['billing_address']['first_name']);
unset($params['customer_details']['billing_address']['last_name']);
unset($params['customer_details']['billing_address']['phone']);
unset($params['customer_details']['billing_address']['address']);
unset($params['customer_details']['billing_address']['city']);
unset($params['customer_details']['billing_address']['postal_code']);
unset($params['customer_details']['billing_address']['country_code']);

Sanitizer::jsonRequest($params);

$this->assertEquals(false, isset($params['customer_details']['billing_address']['first_name']));
$this->assertEquals(false, isset($params['customer_details']['billing_address']['last_name']));
$this->assertEquals(false, isset($params['customer_details']['billing_address']['phone']));
$this->assertEquals(false, isset($params['customer_details']['billing_address']['address']));
$this->assertEquals(false, isset($params['customer_details']['billing_address']['city']));
$this->assertEquals(false, isset($params['customer_details']['billing_address']['postal_code']));
$this->assertEquals(false, isset($params['customer_details']['billing_address']['country_code']));
}

public function testSanitizeWithoutOptionalInShippingAddress()
{
$params = MtChargeFixture::build('vtweb');
unset($params['customer_details']['shipping_address']['first_name']);
unset($params['customer_details']['shipping_address']['last_name']);
unset($params['customer_details']['shipping_address']['phone']);
unset($params['customer_details']['shipping_address']['address']);
unset($params['customer_details']['shipping_address']['city']);
unset($params['customer_details']['shipping_address']['postal_code']);
unset($params['customer_details']['shipping_address']['country_code']);

Sanitizer::jsonRequest($params);

$this->assertEquals(false, isset($params['customer_details']['shipping_address']['first_name']));
$this->assertEquals(false, isset($params['customer_details']['shipping_address']['last_name']));
$this->assertEquals(false, isset($params['customer_details']['shipping_address']['phone']));
$this->assertEquals(false, isset($params['customer_details']['shipping_address']['address']));
$this->assertEquals(false, isset($params['customer_details']['shipping_address']['city']));
$this->assertEquals(false, isset($params['customer_details']['shipping_address']['postal_code']));
$this->assertEquals(false, isset($params['customer_details']['shipping_address']['country_code']));
}

}

0 comments on commit f4713de

Please sign in to comment.