Skip to content

Commit

Permalink
Merge branch 'main' into feature/order-metafields-api
Browse files Browse the repository at this point in the history
Conflicts:
	RELEASE_NOTES.md
  • Loading branch information
jswift committed Nov 12, 2020
2 parents 193afb5 + c202954 commit 49051cd
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ Running tests: `composer run-script test`

#### Payment Methods

- Payment Access Token
- Payment Methods
- ☑️ Payment Access Token
- ☑️ Payment Methods

#### Scripts

Expand Down
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## New Features

- Implement new Order Metafields api
- Implement V3 Payment endpoints (Create Payment Access Token, Get Accepted Payment Methods)

## Minor Update

Expand Down
6 changes: 6 additions & 0 deletions src/BigCommerce/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BigCommerce\ApiV3\Orders\OrdersApi;
use BigCommerce\ApiV3\Customers\CustomersApi;
use BigCommerce\ApiV3\Payments\PaymentsProcessingApi;
use BigCommerce\ApiV3\PriceLists\PriceListsApi;
use BigCommerce\ApiV3\Themes\ThemesApi;
use GuzzleHttp\HandlerStack;
Expand Down Expand Up @@ -128,4 +129,9 @@ public function order(int $orderId): OrdersApi
{
return new OrdersApi($this, $orderId);
}

public function payments(): PaymentsProcessingApi
{
return new PaymentsProcessingApi($this);
}
}
42 changes: 42 additions & 0 deletions src/BigCommerce/Payments/PaymentsProcessingApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace BigCommerce\ApiV3\Payments;

use BigCommerce\ApiV3\Api\V3ApiBase;
use BigCommerce\ApiV3\ResourceModels\Order\Order;
use BigCommerce\ApiV3\ResponseModels\Payment\AcceptedPaymentMethodsResponse;
use BigCommerce\ApiV3\ResponseModels\Payment\PaymentAccessTokenResponse;
use GuzzleHttp\RequestOptions;

class PaymentsProcessingApi extends V3ApiBase
{
private const PAYMENTS_ENDPOINT = 'payments/';
private const ACCESS_TOKENS_ENDPOINT = self::PAYMENTS_ENDPOINT . 'access_tokens';
private const PAYMENT_METHODS_ENDPOINT = self::PAYMENTS_ENDPOINT . 'methods';

public function createToken(Order $order): PaymentAccessTokenResponse
{
$response = $this->getClient()->getRestClient()->post(
self::ACCESS_TOKENS_ENDPOINT,
[
RequestOptions::JSON => json_encode($order),
]
);

return new PaymentAccessTokenResponse($response);
}

public function paymentMethods(int $orderId): AcceptedPaymentMethodsResponse
{
$response = $this->getClient()->getRestClient()->get(
self::PAYMENT_METHODS_ENDPOINT,
[
RequestOptions::QUERY => [
'order_id' => $orderId,
]
]
);

return new AcceptedPaymentMethodsResponse($response);
}
}
15 changes: 15 additions & 0 deletions src/BigCommerce/ResourceModels/Order/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace BigCommerce\ApiV3\ResourceModels\Order;

class Order
{
public int $id;
public bool $is_recurring;

public function __construct(int $id, string $is_recurring)
{
$this->id = $id;
$this->is_recurring = $is_recurring;
}
}
10 changes: 10 additions & 0 deletions src/BigCommerce/ResourceModels/Payment/PaymentAccessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace BigCommerce\ApiV3\ResourceModels\Payment;

use BigCommerce\ApiV3\ResourceModels\ResourceModel;

class PaymentAccessToken extends ResourceModel
{
public string $id;
}
15 changes: 15 additions & 0 deletions src/BigCommerce/ResourceModels/Payment/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace BigCommerce\ApiV3\ResourceModels\Payment;

use BigCommerce\ApiV3\ResourceModels\ResourceModel;

class PaymentMethod extends ResourceModel
{
public string $id;
public string $name;
public array $stored_instruments;
public array $supported_instruments;
public bool $test_mode;
public string $type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace BigCommerce\ApiV3\ResponseModels\Payment;

use BigCommerce\ApiV3\ResourceModels\Payment\PaymentMethod;
use BigCommerce\ApiV3\ResponseModels\PaginatedResponse;

class AcceptedPaymentMethodsResponse extends PaginatedResponse
{
/**
* @return PaymentMethod[]
*/
public function getMethods(): array
{
return $this->getData();
}

protected function resourceClass(): string
{
return PaymentMethod::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace BigCommerce\ApiV3\ResponseModels\Payment;

use BigCommerce\ApiV3\ResourceModels\Payment\PaymentAccessToken;
use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse;
use stdClass;

class PaymentAccessTokenResponse extends SingleResourceResponse
{
private PaymentAccessToken $token;

public function getToken(): PaymentAccessToken
{
return $this->token;
}

protected function addData(stdClass $rawData): void
{
$this->token = new PaymentAccessToken($rawData);
}
}
27 changes: 27 additions & 0 deletions tests/BigCommerce/Payments/PaymentsProcessingApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace BigCommerce\Tests\Payments;

use BigCommerce\ApiV3\ResourceModels\Order\Order;
use BigCommerce\Tests\BigCommerceApiTest;

class PaymentsProcessingApiTest extends BigCommerceApiTest
{
public function testCanCreatePaymentToken(): void
{
$this->setReturnData('payments__create_payment_access_token.json', 201);
$order = new Order(1, false);

$token = $this->getApi()->payments()->createToken($order)->getToken();
$this->assertEquals('abcdefg', $token->id);
}

public function testCanGetPaymentMethodsForOrder(): void
{
$this->setReturnData('payments__get_accepted_payment_methods.json');
$methods = $this->getApi()->payments()->paymentMethods(123)->getMethods();

$this->assertCount(1, $methods);
$this->assertEquals('stripe.card', $methods[0]->id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"id": "abcdefg"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"data": [
{
"id": "stripe.card",
"name": "Stripe",
"test_mode": true,
"type": "card",
"supported_instruments": [
{
"instrument_type": "VISA",
"verification_value_required": true
},
{
"instrument_type": "MASTERCARD",
"verification_value_required": true
},
{
"instrument_type": "AMEX",
"verification_value_required": true
},
{
"instrument_type": "DISCOVER",
"verification_value_required": true
},
{
"instrument_type": "JCB",
"verification_value_required": true
},
{
"instrument_type": "DINERS_CLUB",
"verification_value_required": true
},
{
"instrument_type": "STORED_CARD",
"verification_value_required": true
}
],
"stored_instruments": [
{
"type": "stored_card",
"brand": "VISA",
"expiry_month": 9,
"expiry_year": 2020,
"issuer_identification_number": "424242",
"last_4": "4242",
"token": "050a1e5c982e5905288ec5ec33f292772762033a0704f46fccb16bf1940b51ef",
"is_default": true
}
]
}
],
"meta": {}
}

0 comments on commit 49051cd

Please sign in to comment.