Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Allow creating Orders with addresses, line items and payments

Damian Legawiec2b4165b66f59
diff --git a/api/app/controllers/spree/api/v2/platform/orders_controller.rb b/api/app/controllers/spree/api/v2/platform/orders_controller.rb
index b850b17f53..350974c1df 100644
--- a/api/app/controllers/spree/api/v2/platform/orders_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/orders_controller.rb
@@ -7,6 +7,16 @@ module Spree
include CouponCodesHelper
+ def create
+ resource = current_store.orders.new(permitted_resource_params)
+
+ if resource.save
+ render_serialized_payload(201) { serialize_resource(resource) }
+ else
+ render_error_payload(resource.errors)
+ end
+ end
+
def update
result = update_service.call(
order: resource,
@@ -164,7 +174,9 @@ module Spree
bill_address_attributes: Spree::Address.json_api_permitted_attributes,
ship_address_attributes: Spree::Address.json_api_permitted_attributes,
line_items_attributes: Spree::LineItem.json_api_permitted_attributes,
- payments_attributes: Spree::Payment.json_api_permitted_attributes,
+ payments_attributes: Spree::Payment.json_api_permitted_attributes + [
+ source_attributes: Spree::CreditCard.json_api_permitted_attributes
+ ],
shipments_attributes: Spree::Shipment.json_api_permitted_attributes
]
end
diff --git a/api/spec/integration/api/v2/platform/orders_spec.rb b/api/spec/integration/api/v2/platform/orders_spec.rb
index 061d47052f..8c473a8a26 100644
--- a/api/spec/integration/api/v2/platform/orders_spec.rb
+++ b/api/spec/integration/api/v2/platform/orders_spec.rb
@@ -11,7 +11,35 @@ describe 'Orders API', swagger: true do
let(:persisted_order) { create(:order_with_line_items, state: :delivery) }
let(:id) { persisted_order.number }
let(:records_list) { create_list(:order, 2) }
- let(:valid_create_param_value) { build(:order).attributes }
+ let(:store) { Spree::Store.default }
+ let(:product) { create(:product, price: 10.0, stores: [store]) }
+ let(:country) { create(:country, states_required: true) }
+ let(:state) { create(:state, country: country) }
+ let(:address_attributes) { build(:address, country: country, state: state).attributes }
+ let(:order_attibutes) { attributes_for(:order) }
+ let(:payment_method) { create(:credit_card_payment_method, stores: [store]) }
+ let(:valid_create_param_value) do
+ order_attibutes.merge(
+ currency: 'USD',
+ bill_address_attributes: address_attributes,
+ ship_address_attributes: address_attributes,
+ line_items_attributes: [
+ attributes_for(:line_item, variant_id: product.master.id, currency: 'USD', quantity: 2)
+ ],
+ payments_attributes: [
+ {
+ payment_method_id: payment_method.id,
+ source_attributes: {
+ number: '4111111111111111',
+ month: 12,
+ year: Time.current.year + 1,
+ verification_value: '123',
+ name: 'Spree Commerce'
+ }
+ }
+ ]
+ )
+ end
let(:valid_update_param_value) do
{
total: 10.00,
diff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rb
index ece8d9a65d..687a7081ae 100644
--- a/core/app/models/spree/credit_card.rb
+++ b/core/app/models/spree/credit_card.rb
@@ -171,6 +171,15 @@ module Spree
)
end
+ def self.json_api_permitted_attributes
+ [
+ 'number', 'month', 'year', 'expiry', 'verification_value', 'first_name', 'last_name',
+ 'cc_type', 'gateway_customer_profile_id', 'gateway_payment_profile_id', 'last_digits',
+ 'name', 'encrypted_data', 'address_id', 'created_at', 'updated_at', 'user_id',
+ 'payment_method_id', 'default', 'deleted_at'
+ ]
+ end
+
private
def require_card_numbers?