Skip to content

Commit

Permalink
Merge pull request #7 from apruve/AP-5505
Browse files Browse the repository at this point in the history
Ap 5505 - adding in offline ordering and corporate account lookup
  • Loading branch information
Pro777 committed Aug 29, 2017
2 parents 42b6426 + a6dcdac commit 8a3d778
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Please use [Github issues](https://github.com/apruve/apruve-php/issues) to reque
Add this require to your `composer.json`:

"require": {
"apruve/apruve-php": "~1.3"
"apruve/apruve-php": "~1.4"
}

**NOTE**: Be sure to update the version as you update the version of apruve-php
Expand Down
25 changes: 21 additions & 4 deletions src/Apruve/ApruveObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@ public function __construct( $values = [], $client = null ) {
} else {
$this->client = $client;
}
foreach ( $values as $name => $value ) {
$this->$name = $value;
if ( is_array( $values ) || is_object( $values ) ) {
foreach ( $values as $name => $value ) {
if ( is_array( $value ) ) {
$this->$name = [];
$this->$name = $value;
} else {
$this->$name = $value;
}
}
}
}

public function toHashString() {
$ret = '';
$called_class = get_called_class();
foreach ( $called_class::$hash_order as $key ) {
$ret .= $this->$key;
if ( is_array( $this->$key ) ) {
foreach ( $this->$key as $item ) {
$ret .= $item->toHashString();
}
} else {
$ret .= $this->$key;
}
}

return $ret;
Expand All @@ -38,7 +51,11 @@ public function toJsonArray() {
if ( gettype( $this->$key ) == "array" ) {
$jsonArr[ $key ] = [];
foreach ( $this->$key as $item ) {
array_push( $jsonArr[ $key ], $item->toJsonArray() );
if ( is_array( $item ) || is_string( $item ) ) {
array_push( $jsonArr[ $key ], $item );
} else {
array_push( $jsonArr[ $key ], $item->toJsonArray() );
}
}
} else {
$jsonArr[ $key ] = $this->$key;
Expand Down
5 changes: 4 additions & 1 deletion src/Apruve/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ protected function initCurl( $url ) {
return new CurlRequest( $url );
}

public function get( $path ) {
public function get( $path, $payload = null ) {
$client = $this->restRequest( $path );
$client->setOption( CURLOPT_RETURNTRANSFER, true );
if ( ! empty( $payload ) ) {
$client->setOption( CURLOPT_POSTFIELDS, $payload );
}
$response = $client->execute();
$ret = [ $client->getInfo( CURLINFO_HTTP_CODE ), json_decode( $response, true ), $client->error() ];
$client->close();
Expand Down
41 changes: 41 additions & 0 deletions src/Apruve/CorporateAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// Invoice
namespace Apruve;

require_once 'ApruveObject.php';

class CorporateAccount extends ApruveObject {

protected static $ACCOUNTS_PATH = '/merchants/%s/corporate_accounts';
protected static $hash_order = [];
protected static $json_fields = [
'id',
'customer_uuid',
'merchant_uuid',
'type',
'payment_term_strategy_name',
'name',
'authorized_buyers',
];

var $id;
var $customer_uuid;
var $merchant_uuid;
var $type;
var $payment_term_strategy_name;
var $name;
var $authorized_buyers = [];

public static function get( $merchant_key, $email, $client = null ) {
if ( $client == null ) {
$client = new Client();
}
$response = $client->get( sprintf( self::$ACCOUNTS_PATH, $merchant_key ) . '?email=' . $email );

if ( $response[0] == 200 ) {
return new self( array_pop( $response[1] ), $client );
} else {
return $response[2];
}
}
}
23 changes: 21 additions & 2 deletions src/Apruve/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require_once 'ApruveObject.php';

class Order extends ApruveObject {
protected static $ORDERS_PATH = '/orders/';
protected static $ORDERS_PATH = '/orders';
protected static $hash_order = [
'merchant_id',
'merchant_order_id',
Expand All @@ -25,8 +25,13 @@ class Order extends ApruveObject {
'tax_cents',
'shipping_cents',
'expire_at',
'accepts_payment_terms',
'finalize_on_create',
'invoice_on_create',
'payment_term',
'order_items',
'shopper_id',
'customer_id'
];
private static $UPDATE_PATH = '/orders/%s';
private static $CANCEL_PATH = '/orders/%s/cancel';
Expand All @@ -41,8 +46,11 @@ class Order extends ApruveObject {
var $shipping_cents;
var $currency;
var $expire_at;
var $accepts_payment_terms;
var $finalize_on_create;
var $invoice_on_create = 'false';
var $order_items = [];
var $payment_term = [];
var $api_url;
var $view_url;
var $created_at;
Expand All @@ -56,14 +64,15 @@ public function __construct( $order = [], $client = null ) {
}
}
}

parent::__construct( $order, $client );
}

public static function get( $order_id, $client = null ) {
if ( $client == null ) {
$client = new Client();
}
$response = $client->get( self::$ORDERS_PATH . $order_id );
$response = $client->get( self::$ORDERS_PATH . '/' . $order_id );

if ( $response[0] == 200 ) {
$object = new self( $response[1], $client );
Expand Down Expand Up @@ -132,6 +141,16 @@ public function addOrderItem( $order_item ) {
}
}

public function save() {
$response = $this->client->post(
self::$ORDERS_PATH, $this->toJson() );
if ( $response[0] == 201 ) {
return new self( $response[1], $this->client );
} else {
return $response[2];
}
}

public function update() {
$response = $this->client->put( sprintf( self::$UPDATE_PATH, $this->id ),
$this->toJson() );
Expand Down
5 changes: 1 addition & 4 deletions src/Apruve/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class OrderItem extends ApruveObject {

protected static $hash_order = [
'title',
'plan_code',
'amount_cents',
'price_ea_cents',
'quantity',
Expand All @@ -21,7 +20,6 @@ class OrderItem extends ApruveObject {
];
protected static $json_fields = [
'title',
'plan_code',
'amount_cents',
'price_ea_cents',
'quantity',
Expand All @@ -35,8 +33,7 @@ class OrderItem extends ApruveObject {
var $id;
var $order_id;
var $title;
var $plan_code;
var $amount_cents;
var $price_total_cents;
var $quantity;
var $price_ea_cents;
var $merchant_notes;
Expand Down
96 changes: 96 additions & 0 deletions tests/Apruve/CorporateAccountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

require_once dirname( __FILE__ ) . '/../../src/Apruve/CorporateAccount.php';
require_once dirname( __FILE__ ) . '/../../src/Apruve/Client.php';
require_once dirname( __FILE__ ) . '/../../src/Apruve/Environment.php';

use Apruve\CorporateAccount;

class CorporateAccountTest extends PHPUnit_Framework_TestCase {

public function testPropertiesAreDefined() {
$vars = get_class_vars( get_class( $this->corporate_account ) );

$this->assertEquals( array_keys( $vars ), [
'id',
'customer_uuid',
'merchant_uuid',
'type',
'payment_term_strategy_name',
'name',
'authorized_buyers',
] );
$this->assertEquals( 7, count( $vars ) );

}

public function testToJson() {
$this->assertJsonStringEqualsJsonString( '{
"id": "asdf1234",
"customer_uuid": "b76ac505389e7814eb10fb4fdc33a50b",
"merchant_uuid": "5ca2ab51d10b490cba7b22934c9fe913",
"type": "corporate",
"payment_term_strategy_name": "Net30",
"name": "MyCorporateAccount",
"authorized_buyers": [
{
"id": "d9e10e59140513b61998e292088c8194",
"name": "Corporate Corbin",
"email" : "corporateuser@apruve.com"
}
]
}',
$this->corporate_account->toJson()
);
}

public function testGet() {
$client = $this->getMockBuilder( 'Apruve\Client' )
->setMethods( [ 'get' ] )
->getMock();
$client->expects( $this->Once() )
->method( 'get' )
->with( $this->equalTo( '/merchants/asdf1234/corporate_accounts?email=corporateuser@apruve.com' ) )
->will( $this->returnValue( [
200,
[
[
'id' => 'asdf1234',
'customer_uuid' => 'b76ac505389e7814eb10fb4fdc33a50b',
'merchant_uuid' => '5ca2ab51d10b490cba7b22934c9fe913',
'type' => 'corporate',
'payment_term_strategy_name' => 'Net30',
'name' => 'MyCorporateAccount',
]
]
] )
);

$i = CorporateAccount::get( 'asdf1234', 'corporateuser@apruve.com', $client );

$this->assertEquals( 'asdf1234', $i->id );
$this->assertEquals( 'Apruve\CorporateAccount', get_class( $i ) );
}

protected function setUp() {
Apruve\Client::init( 'a key', Apruve\Environment::DEV() );
$this->corporate_account = new CorporateAccount( [
'id' => 'asdf1234',
'customer_uuid' => 'b76ac505389e7814eb10fb4fdc33a50b',
'merchant_uuid' => '5ca2ab51d10b490cba7b22934c9fe913',
'type' => 'corporate',
'payment_term_strategy_name' => 'Net30',
'authorized_buyers' => [
[
'id' => 'd9e10e59140513b61998e292088c8194',
'name' => 'Corporate Corbin',
'email' => 'corporateuser@apruve.com'
]
],
'name' => 'MyCorporateAccount'
] );
}


}

31 changes: 14 additions & 17 deletions tests/Apruve/OrderItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public function testPropertiesAreDefined() {
'id',
'order_id',
'title',
'plan_code',
'amount_cents',
'price_total_cents',
'quantity',
'price_ea_cents',
'merchant_notes',
Expand All @@ -24,12 +23,12 @@ public function testPropertiesAreDefined() {
'vendor',
'view_product_url',
) );
$this->assertEquals( 13, count( $item_vars ) );
$this->assertEquals( 12, count( $item_vars ) );
}

public function testToHashString() {
$this->assertEquals(
'A titleplan340034001some notes.a description.some variation.skuACMEA Url.',
'A title340034001some notes.a description.some variation.skuACMEA Url.',
$this->item->toHashString()
);
}
Expand All @@ -38,7 +37,6 @@ public function testToJsonString() {
$this->assertJsonStringEqualsJsonString(
'{
"title": "A title",
"plan_code": "plan",
"amount_cents": 3400,
"price_ea_cents": 3400,
"quantity": 1,
Expand All @@ -54,18 +52,17 @@ public function testToJsonString() {

protected function setUp() {
$this->item = new OrderItem( [
'title' => 'A title',
'sku' => 'sku',
'plan_code' => 'plan',
'amount_cents' => 3400,
'price_ea_cents' => 3400,
'quantity' => 1,
'merchant_notes' => 'some notes.',
'description' => 'a description.',
'variant_info' => 'some variation.',
'vendor' => 'ACME',
'view_product_url' => 'A Url.',
'order_id' => '1234',
'title' => 'A title',
'sku' => 'sku',
'amount_cents' => 3400,
'price_ea_cents' => 3400,
'quantity' => 1,
'merchant_notes' => 'some notes.',
'description' => 'a description.',
'variant_info' => 'some variation.',
'vendor' => 'ACME',
'view_product_url' => 'A Url.',
'order_id' => '1234',
], $this->getMockBuilder( 'Apruve\Client' )
->setConstructorArgs( [ 'a key', Apruve\Environment::DEV() ] )
->getMock()
Expand Down

0 comments on commit 8a3d778

Please sign in to comment.