I am trying to write a plugin in PHP for my client that allows the user to book a date through a third party system and pay the deposit, then generates an invoice for the balance.
Where I am getting suck is applying the deposit to the order. Since I can’t apply a payment that doesn’t total the amount due for the order, I thought I’d apply a OrderLineItemDiscount in the amount of the deposit, but that changes the tax basis, which is not what I want. So I tried CatalogDiscount where I can set the modifyTaxBasis to DO_NOT_MODIFY_TAX_BASIS, but then I get a missing “scope” error. But “scope” is not allowed in the catalogDiscount model.
I’m at a loss. Anyone have any ideas?
- Using the PHP sdk
public function applyDeposit($response){
session_start();
msdlog($response);
// dotenv is used to read from the ‘.env’ file created for credentials
$dotenv = Dotenv\Dotenv::createImmutable(DIR);
$dotenv->load();
$orderResponse = unserialize($_SESSION[‘orderResponse’]);
$order_id = $orderResponse->order->id;
extract($_POST);
$square_client = new SquareClient([
'accessToken' => $_ENV['SQUARE_ACCESS_TOKEN'],
'environment' => $_ENV['ENVIRONMENT'],
'userAgentDetail' => 'bookAndPayParty',
]);
$order_api = $square_client->getOrdersApi();
/*$line_item_discounts[] = Models\Builders\OrderLineItemDiscountBuilder::init()
->name('Deposit')
->type('FIXED_AMOUNT')
->amountMoney(Models\Builders\MoneyBuilder::init()
->amount($response->payment->amount_money->amount)
->currency($response->payment->amount_money->currency)
->build())
->scope(Models\OrderLineItemDiscountScope::ORDER)
->build();*/
$line_item_discounts[] = Models\Builders\CatalogDiscountBuilder::init()
->name('Deposit')
->discountType('FIXED_AMOUNT')
->amountMoney(Models\Builders\MoneyBuilder::init()
->amount($response->payment->amount_money->amount)
->currency($response->payment->amount_money->currency)
->build())
->scope('ORDER') <---error
->modifyTaxBasis('DO_NOT_MODIFY_TAX_BASIS')
->build();
$update_order_request = Models\Builders\UpdateOrderRequestBuilder::init()
->order(Models\Builders\OrderBuilder::init($this->getId())
->id($order_id)
->version($orderResponse->order->version)
->discounts($line_item_discounts)
->build()
)
->idempotencyKey($idempotencyKey)
->build();
try {
$apiResponse = $order_api->updateOrder($order_id,$update_order_request);
if ( $apiResponse->isSuccess() ) {
$response = json_decode($apiResponse->getBody());
msdlog($response);
return json_encode($response);
} else {
$errors = $apiResponse->getErrors();
msdlog('Error on applyDeposit');
msdlog($errors);
return false;
}
} catch (ApiException $e) {
msdlog($e);
return false;
}
}