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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreeinclude 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 updateresult = update_service.call(order: resource,@@ -164,7 +174,9 @@ module Spreebill_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]enddiff --git a/api/spec/integration/api/v2/platform/orders_spec.rb b/api/spec/integration/api/v2/platform/orders_spec.rbindex 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 dolet(: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'+ }+ }+ ]+ )+ endlet(: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.rbindex 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+privatedef require_card_numbers?