CheckoutTest.php 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Illuminate\Support\Facades\Mail;
  4. use Webkul\Admin\Mail\Order\CreatedNotification as AdminOrderCreatedNotification;
  5. use Webkul\Checkout\Models\Cart;
  6. use Webkul\Checkout\Models\CartAddress;
  7. use Webkul\Checkout\Models\CartItem;
  8. use Webkul\Checkout\Models\CartPayment;
  9. use Webkul\Checkout\Models\CartShippingRate;
  10. use Webkul\Customer\Models\Customer;
  11. use Webkul\Customer\Models\CustomerAddress;
  12. use Webkul\Faker\Helpers\Product as ProductFaker;
  13. use Webkul\Product\Models\ProductInventoryIndex;
  14. use Webkul\Product\Models\ProductOrderedInventory;
  15. use Webkul\Sales\Models\Order;
  16. use Webkul\Sales\Models\OrderAddress;
  17. use Webkul\Sales\Models\OrderItem;
  18. use Webkul\Sales\Models\OrderPayment;
  19. use Webkul\Shop\Mail\Order\CreatedNotification as ShopOrderCreatedNotification;
  20. use function Pest\Laravel\postJson;
  21. it('should handle certain validation errors when storing the guest user address for cart billing and shipping', function () {
  22. // Arrange.
  23. $product = (new ProductFaker([
  24. 'attributes' => [
  25. 5 => 'new',
  26. 26 => 'guest_checkout',
  27. ],
  28. 'attribute_value' => [
  29. 'new' => [
  30. 'boolean_value' => true,
  31. ],
  32. 'guest_checkout' => [
  33. 'boolean_value' => true,
  34. ],
  35. ],
  36. ]))
  37. ->getSimpleProductFactory()
  38. ->create();
  39. $cart = Cart::factory()->create();
  40. $additional = [
  41. 'product_id' => $product->id,
  42. 'rating' => '0',
  43. 'is_buy_now' => '0',
  44. 'quantity' => '1',
  45. ];
  46. CartItem::factory()->create([
  47. 'cart_id' => $cart->id,
  48. 'product_id' => $product->id,
  49. 'sku' => $product->sku,
  50. 'quantity' => $additional['quantity'],
  51. 'name' => $product->name,
  52. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  53. 'price_incl_tax' => $convertedPrice,
  54. 'base_price' => $price,
  55. 'base_price_incl_tax' => $price,
  56. 'total' => $total = $convertedPrice * $additional['quantity'],
  57. 'total_incl_tax' => $total,
  58. 'base_total' => $price * $additional['quantity'],
  59. 'weight' => $product->weight ?? 0,
  60. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  61. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  62. 'type' => $product->type,
  63. 'additional' => $additional,
  64. ]);
  65. cart()->setCart($cart);
  66. // Act and Assert.
  67. postJson(route('shop.checkout.onepage.addresses.store'), [
  68. 'shipping' => [],
  69. 'billing' => [],
  70. ])
  71. ->assertJsonValidationErrorFor('billing.first_name')
  72. ->assertJsonValidationErrorFor('billing.last_name')
  73. ->assertJsonValidationErrorFor('billing.email')
  74. ->assertJsonValidationErrorFor('billing.address')
  75. ->assertJsonValidationErrorFor('billing.city')
  76. ->assertJsonValidationErrorFor('billing.phone')
  77. ->assertJsonValidationErrorFor('shipping.first_name')
  78. ->assertJsonValidationErrorFor('shipping.last_name')
  79. ->assertJsonValidationErrorFor('shipping.email')
  80. ->assertJsonValidationErrorFor('shipping.address')
  81. ->assertJsonValidationErrorFor('shipping.city')
  82. ->assertJsonValidationErrorFor('shipping.phone')
  83. ->assertUnprocessable();
  84. });
  85. it('should handle certain validation errors when storing the customer address for cart billing and shipping', function () {
  86. // Arrange.
  87. $product = (new ProductFaker([
  88. 'attributes' => [
  89. 5 => 'new',
  90. ],
  91. 'attribute_value' => [
  92. 'new' => [
  93. 'boolean_value' => true,
  94. ],
  95. ],
  96. ]))
  97. ->getSimpleProductFactory()
  98. ->create();
  99. $customer = Customer::factory()->create();
  100. $cart = Cart::factory()->create([
  101. 'customer_id' => $customer->id,
  102. 'customer_first_name' => $customer->first_name,
  103. 'customer_last_name' => $customer->last_name,
  104. 'customer_email' => $customer->email,
  105. 'is_guest' => 0,
  106. ]);
  107. $additional = [
  108. 'product_id' => $product->id,
  109. 'rating' => '0',
  110. 'is_buy_now' => '0',
  111. 'quantity' => '1',
  112. ];
  113. CartItem::factory()->create([
  114. 'cart_id' => $cart->id,
  115. 'product_id' => $product->id,
  116. 'sku' => $product->sku,
  117. 'quantity' => $additional['quantity'],
  118. 'name' => $product->name,
  119. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  120. 'price_incl_tax' => $convertedPrice,
  121. 'base_price' => $price,
  122. 'base_price_incl_tax' => $price,
  123. 'total' => $total = $convertedPrice * $additional['quantity'],
  124. 'total_incl_tax' => $total,
  125. 'base_total' => $price * $additional['quantity'],
  126. 'weight' => $product->weight ?? 0,
  127. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  128. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  129. 'type' => $product->type,
  130. 'additional' => $additional,
  131. ]);
  132. cart()->setCart($cart);
  133. // Act and Assert.
  134. $this->loginAsCustomer($customer);
  135. postJson(route('shop.checkout.onepage.addresses.store'), [
  136. 'shipping' => [],
  137. 'billing' => [],
  138. ])
  139. ->assertJsonValidationErrorFor('billing.first_name')
  140. ->assertJsonValidationErrorFor('billing.last_name')
  141. ->assertJsonValidationErrorFor('billing.email')
  142. ->assertJsonValidationErrorFor('billing.address')
  143. ->assertJsonValidationErrorFor('billing.city')
  144. ->assertJsonValidationErrorFor('billing.phone')
  145. ->assertJsonValidationErrorFor('shipping.first_name')
  146. ->assertJsonValidationErrorFor('shipping.last_name')
  147. ->assertJsonValidationErrorFor('shipping.email')
  148. ->assertJsonValidationErrorFor('shipping.address')
  149. ->assertJsonValidationErrorFor('shipping.city')
  150. ->assertJsonValidationErrorFor('shipping.phone')
  151. ->assertUnprocessable();
  152. });
  153. it('should store the shipping address as the billing address when use_for_shipping key is set to true in billing for guest user', function () {
  154. // Arrange.
  155. $product = (new ProductFaker([
  156. 'attributes' => [
  157. 5 => 'new',
  158. 26 => 'guest_checkout',
  159. ],
  160. 'attribute_value' => [
  161. 'new' => [
  162. 'boolean_value' => true,
  163. ],
  164. 'guest_checkout' => [
  165. 'boolean_value' => true,
  166. ],
  167. ],
  168. ]))
  169. ->getSimpleProductFactory()
  170. ->create();
  171. $cart = Cart::factory()->create();
  172. $additional = [
  173. 'product_id' => $product->id,
  174. 'rating' => '0',
  175. 'is_buy_now' => '0',
  176. 'quantity' => '1',
  177. ];
  178. CartItem::factory()->create([
  179. 'cart_id' => $cart->id,
  180. 'product_id' => $product->id,
  181. 'sku' => $product->sku,
  182. 'quantity' => $additional['quantity'],
  183. 'name' => $product->name,
  184. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  185. 'price_incl_tax' => $convertedPrice,
  186. 'base_price' => $price,
  187. 'base_price_incl_tax' => $price,
  188. 'total' => $total = $convertedPrice * $additional['quantity'],
  189. 'total_incl_tax' => $total,
  190. 'base_total' => $price * $additional['quantity'],
  191. 'weight' => $product->weight ?? 0,
  192. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  193. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  194. 'type' => $product->type,
  195. 'additional' => $additional,
  196. ]);
  197. $customerAddress = CustomerAddress::factory()->create()->toArray();
  198. $customerAddress = CustomerAddress::factory()->create()->toArray();
  199. cart()->setCart($cart);
  200. // Act and Assert.
  201. $response = postJson(route('shop.checkout.onepage.addresses.store'), [
  202. 'billing' => $billingAddress = [
  203. ...$customerAddress,
  204. 'use_for_shipping' => 1,
  205. 'address' => [fake()->address()],
  206. ],
  207. ])
  208. ->assertOk()
  209. ->assertJsonPath('redirect', false)
  210. ->assertJsonPath('data.shippingMethods.flatrate.carrier_title', 'Flat Rate')
  211. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier', 'flatrate')
  212. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier_title', 'Flat Rate')
  213. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method', 'flatrate_flatrate')
  214. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method_title', 'Flat Rate')
  215. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.price', 10)
  216. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.base_price', 10)
  217. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  218. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier', 'free')
  219. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier_title', 'Free Shipping')
  220. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  221. ->assertJsonPath('data.shippingMethods.free.rates.0.method', 'free_free')
  222. ->assertJsonPath('data.shippingMethods.free.rates.0.method_title', 'Free Shipping')
  223. ->assertJsonPath('data.shippingMethods.free.rates.0.price', 0)
  224. ->assertJsonPath('data.shippingMethods.free.rates.0.base_price', 0);
  225. $response->assertJsonPath('data.shippingMethods.flatrate.rates.0.cart_address_id', $cart->shipping_address->id);
  226. $response->assertJsonPath('data.shippingMethods.free.rates.0.cart_address_id', $cart->shipping_address->id);
  227. $this->assertModelWise([
  228. CartAddress::class => [
  229. [
  230. 'address' => implode("\n", $billingAddress['address']),
  231. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  232. 'cart_id' => $cart->id,
  233. 'use_for_shipping' => $billingAddress['use_for_shipping'],
  234. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  235. ],
  236. ],
  237. CartAddress::class => [
  238. [
  239. 'address' => implode("\n", $billingAddress['address']),
  240. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  241. 'cart_id' => $cart->id,
  242. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  243. ],
  244. ],
  245. ]);
  246. });
  247. it('should store the billing and shipping address for guest user', function () {
  248. // Arrange.
  249. $product = (new ProductFaker([
  250. 'attributes' => [
  251. 5 => 'new',
  252. 26 => 'guest_checkout',
  253. ],
  254. 'attribute_value' => [
  255. 'new' => [
  256. 'boolean_value' => true,
  257. ],
  258. 'guest_checkout' => [
  259. 'boolean_value' => true,
  260. ],
  261. ],
  262. ]))
  263. ->getSimpleProductFactory()
  264. ->create();
  265. $cart = Cart::factory()->create();
  266. $additional = [
  267. 'product_id' => $product->id,
  268. 'rating' => '0',
  269. 'is_buy_now' => '0',
  270. 'quantity' => '1',
  271. ];
  272. CartItem::factory()->create([
  273. 'cart_id' => $cart->id,
  274. 'product_id' => $product->id,
  275. 'sku' => $product->sku,
  276. 'quantity' => $additional['quantity'],
  277. 'name' => $product->name,
  278. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  279. 'price_incl_tax' => $convertedPrice,
  280. 'base_price' => $price,
  281. 'base_price_incl_tax' => $price,
  282. 'total' => $total = $convertedPrice * $additional['quantity'],
  283. 'total_incl_tax' => $total,
  284. 'base_total' => $price * $additional['quantity'],
  285. 'weight' => $product->weight ?? 0,
  286. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  287. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  288. 'type' => $product->type,
  289. 'additional' => $additional,
  290. ]);
  291. $customerAddress = CustomerAddress::factory()->create()->toArray();
  292. $customerAddress = CustomerAddress::factory()->create()->toArray();
  293. cart()->setCart($cart);
  294. // Act and Assert.
  295. $response = postJson(route('shop.checkout.onepage.addresses.store'), [
  296. 'billing' => $billingAddress = [
  297. ...$customerAddress,
  298. 'address' => [fake()->address()],
  299. 'use_for_shipping' => 0,
  300. ],
  301. 'shipping' => $shippingAddress = [
  302. ...$customerAddress,
  303. 'address' => [fake()->address()],
  304. ],
  305. ])
  306. ->assertOk()
  307. ->assertJsonPath('redirect', false)
  308. ->assertJsonPath('data.shippingMethods.flatrate.carrier_title', 'Flat Rate')
  309. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier', 'flatrate')
  310. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier_title', 'Flat Rate')
  311. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method', 'flatrate_flatrate')
  312. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method_title', 'Flat Rate')
  313. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.price', 10)
  314. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.base_price', 10)
  315. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  316. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier', 'free')
  317. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier_title', 'Free Shipping')
  318. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  319. ->assertJsonPath('data.shippingMethods.free.rates.0.method', 'free_free')
  320. ->assertJsonPath('data.shippingMethods.free.rates.0.method_title', 'Free Shipping')
  321. ->assertJsonPath('data.shippingMethods.free.rates.0.price', 0)
  322. ->assertJsonPath('data.shippingMethods.free.rates.0.base_price', 0);
  323. $response->assertJsonPath('data.shippingMethods.flatrate.rates.0.cart_address_id', $cart->shipping_address->id);
  324. $response->assertJsonPath('data.shippingMethods.free.rates.0.cart_address_id', $cart->shipping_address->id);
  325. $this->assertModelWise([
  326. CartAddress::class => [
  327. [
  328. 'address' => implode("\n", $billingAddress['address']),
  329. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  330. 'cart_id' => $cart->id,
  331. 'use_for_shipping' => $billingAddress['use_for_shipping'],
  332. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  333. ],
  334. ],
  335. CartAddress::class => [
  336. [
  337. 'address' => implode("\n", $shippingAddress['address']),
  338. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  339. 'cart_id' => $cart->id,
  340. ...Arr::only($shippingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  341. ],
  342. ],
  343. ]);
  344. });
  345. it('should store the billing address for non stockable items for guest user', function () {
  346. // Arrange.
  347. $product = (new ProductFaker([
  348. 'attributes' => [
  349. 5 => 'new',
  350. 26 => 'guest_checkout',
  351. ],
  352. 'attribute_value' => [
  353. 'new' => [
  354. 'boolean_value' => true,
  355. ],
  356. 'guest_checkout' => [
  357. 'boolean_value' => true,
  358. ],
  359. ],
  360. ]))
  361. ->getVirtualProductFactory()
  362. ->create();
  363. $cart = Cart::factory()->create();
  364. $additional = [
  365. 'product_id' => $product->id,
  366. 'rating' => '0',
  367. 'is_buy_now' => '0',
  368. 'quantity' => '1',
  369. ];
  370. CartItem::factory()->create([
  371. 'cart_id' => $cart->id,
  372. 'product_id' => $product->id,
  373. 'sku' => $product->sku,
  374. 'quantity' => $additional['quantity'],
  375. 'name' => $product->name,
  376. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  377. 'price_incl_tax' => $convertedPrice,
  378. 'base_price' => $price,
  379. 'base_price_incl_tax' => $price,
  380. 'total' => $total = $convertedPrice * $additional['quantity'],
  381. 'total_incl_tax' => $total,
  382. 'base_total' => $price * $additional['quantity'],
  383. 'weight' => $product->weight ?? 0,
  384. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  385. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  386. 'type' => $product->type,
  387. 'additional' => $additional,
  388. ]);
  389. $customerAddress = CustomerAddress::factory()->create()->toArray();
  390. $customerAddress = CustomerAddress::factory()->create()->toArray();
  391. cart()->setCart($cart);
  392. // Act and Assert.
  393. postJson(route('shop.checkout.onepage.addresses.store'), [
  394. 'billing' => $billingAddress = [
  395. ...$customerAddress,
  396. 'address' => [fake()->address()],
  397. 'use_for_shipping' => 1,
  398. ],
  399. ])
  400. ->assertOk()
  401. ->assertJsonPath('data.payment_methods.0.method', 'moneytransfer')
  402. ->assertJsonPath('data.payment_methods.0.method_title', 'Money Transfer')
  403. ->assertJsonPath('data.payment_methods.0.description', 'Money Transfer')
  404. ->assertJsonPath('data.payment_methods.0.sort', 2)
  405. ->assertJsonPath('data.payment_methods.1.method', 'paypal_standard')
  406. ->assertJsonPath('data.payment_methods.1.method_title', 'PayPal Standard')
  407. ->assertJsonPath('data.payment_methods.1.description', 'PayPal Standard')
  408. ->assertJsonPath('data.payment_methods.1.sort', 3)
  409. ->assertJsonPath('data.payment_methods.2.method', 'paypal_smart_button')
  410. ->assertJsonPath('data.payment_methods.2.method_title', 'PayPal Smart Button')
  411. ->assertJsonPath('data.payment_methods.2.description', 'PayPal')
  412. ->assertJsonPath('data.payment_methods.2.sort', 4)
  413. ->assertJsonPath('redirect', false);
  414. $this->assertModelWise([
  415. CartAddress::class => [
  416. [
  417. 'address' => implode("\n", $billingAddress['address']),
  418. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  419. 'cart_id' => $cart->id,
  420. 'use_for_shipping' => $billingAddress['use_for_shipping'],
  421. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  422. ],
  423. ],
  424. ]);
  425. });
  426. it('should store the shipping address as the billing address when use_for_shipping key is set to true in billing for customer', function () {
  427. // Arrange.
  428. $product = (new ProductFaker([
  429. 'attributes' => [
  430. 5 => 'new',
  431. ],
  432. 'attribute_value' => [
  433. 'new' => [
  434. 'boolean_value' => true,
  435. ],
  436. ],
  437. ]))
  438. ->getSimpleProductFactory()
  439. ->create();
  440. $customer = Customer::factory()->create();
  441. $cart = Cart::factory()->create([
  442. 'customer_id' => $customer->id,
  443. 'customer_first_name' => $customer->first_name,
  444. 'customer_last_name' => $customer->last_name,
  445. 'customer_email' => $customer->email,
  446. 'is_guest' => 0,
  447. ]);
  448. $additional = [
  449. 'product_id' => $product->id,
  450. 'rating' => '0',
  451. 'is_buy_now' => '0',
  452. 'quantity' => '1',
  453. ];
  454. CartItem::factory()->create([
  455. 'cart_id' => $cart->id,
  456. 'product_id' => $product->id,
  457. 'sku' => $product->sku,
  458. 'quantity' => $additional['quantity'],
  459. 'name' => $product->name,
  460. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  461. 'price_incl_tax' => $convertedPrice,
  462. 'base_price' => $price,
  463. 'base_price_incl_tax' => $price,
  464. 'total' => $total = $convertedPrice * $additional['quantity'],
  465. 'total_incl_tax' => $total,
  466. 'base_total' => $price * $additional['quantity'],
  467. 'weight' => $product->weight ?? 0,
  468. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  469. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  470. 'type' => $product->type,
  471. 'additional' => $additional,
  472. ]);
  473. $customerAddress = CustomerAddress::factory()->create()->toArray();
  474. $customerAddress = CustomerAddress::factory()->create()->toArray();
  475. cart()->setCart($cart);
  476. // Act and Assert.
  477. $this->loginAsCustomer($customer);
  478. $response = postJson(route('shop.checkout.onepage.addresses.store'), [
  479. 'billing' => $billingAddress = [
  480. ...$customerAddress,
  481. 'address' => [fake()->address()],
  482. 'use_for_shipping' => 1,
  483. ],
  484. ])
  485. ->assertOk()
  486. ->assertJsonPath('redirect', false)
  487. ->assertJsonPath('data.shippingMethods.flatrate.carrier_title', 'Flat Rate')
  488. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier', 'flatrate')
  489. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier_title', 'Flat Rate')
  490. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method', 'flatrate_flatrate')
  491. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method_title', 'Flat Rate')
  492. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.price', 10)
  493. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.base_price', 10)
  494. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  495. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier', 'free')
  496. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier_title', 'Free Shipping')
  497. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  498. ->assertJsonPath('data.shippingMethods.free.rates.0.method', 'free_free')
  499. ->assertJsonPath('data.shippingMethods.free.rates.0.method_title', 'Free Shipping')
  500. ->assertJsonPath('data.shippingMethods.free.rates.0.price', 0)
  501. ->assertJsonPath('data.shippingMethods.free.rates.0.base_price', 0);
  502. $response->assertJsonPath('data.shippingMethods.flatrate.rates.0.cart_address_id', $cart->shipping_address->id);
  503. $response->assertJsonPath('data.shippingMethods.free.rates.0.cart_address_id', $cart->shipping_address->id);
  504. $this->assertModelWise([
  505. CartAddress::class => [
  506. [
  507. 'address' => implode("\n", $billingAddress['address']),
  508. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  509. 'cart_id' => $cart->id,
  510. 'use_for_shipping' => $billingAddress['use_for_shipping'],
  511. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  512. ],
  513. ],
  514. CartAddress::class => [
  515. [
  516. 'address' => implode("\n", $billingAddress['address']),
  517. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  518. 'cart_id' => $cart->id,
  519. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  520. ],
  521. ],
  522. ]);
  523. });
  524. it('should store the billing and shipping address for customer', function () {
  525. // Arrange.
  526. $product = (new ProductFaker([
  527. 'attributes' => [
  528. 5 => 'new',
  529. ],
  530. 'attribute_value' => [
  531. 'new' => [
  532. 'boolean_value' => true,
  533. ],
  534. ],
  535. ]))
  536. ->getSimpleProductFactory()
  537. ->create();
  538. $customer = Customer::factory()->create();
  539. $cart = Cart::factory()->create([
  540. 'customer_id' => $customer->id,
  541. 'customer_first_name' => $customer->first_name,
  542. 'customer_last_name' => $customer->last_name,
  543. 'customer_email' => $customer->email,
  544. 'is_guest' => 0,
  545. ]);
  546. $additional = [
  547. 'product_id' => $product->id,
  548. 'rating' => '0',
  549. 'is_buy_now' => '0',
  550. 'quantity' => '1',
  551. ];
  552. CartItem::factory()->create([
  553. 'cart_id' => $cart->id,
  554. 'product_id' => $product->id,
  555. 'sku' => $product->sku,
  556. 'quantity' => $additional['quantity'],
  557. 'name' => $product->name,
  558. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  559. 'price_incl_tax' => $convertedPrice,
  560. 'base_price' => $price,
  561. 'base_price_incl_tax' => $price,
  562. 'total' => $total = $convertedPrice * $additional['quantity'],
  563. 'total_incl_tax' => $total,
  564. 'base_total' => $price * $additional['quantity'],
  565. 'weight' => $product->weight ?? 0,
  566. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  567. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  568. 'type' => $product->type,
  569. 'additional' => $additional,
  570. ]);
  571. $customerAddress = CustomerAddress::factory()->create()->toArray();
  572. $customerAddress = CustomerAddress::factory()->create()->toArray();
  573. cart()->setCart($cart);
  574. // Act and Assert.
  575. $this->loginAsCustomer($customer);
  576. $response = postJson(route('shop.checkout.onepage.addresses.store'), [
  577. 'billing' => $billingAddress = [
  578. ...$customerAddress,
  579. 'address' => [fake()->address()],
  580. 'use_for_shipping' => 0,
  581. ],
  582. 'shipping' => $shippingAddress = [
  583. ...$customerAddress,
  584. 'address' => [fake()->address()],
  585. ],
  586. ])
  587. ->assertOk()
  588. ->assertJsonPath('redirect', false)
  589. ->assertJsonPath('data.shippingMethods.flatrate.carrier_title', 'Flat Rate')
  590. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier', 'flatrate')
  591. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier_title', 'Flat Rate')
  592. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method', 'flatrate_flatrate')
  593. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method_title', 'Flat Rate')
  594. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.price', 10)
  595. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.base_price', 10)
  596. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  597. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier', 'free')
  598. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier_title', 'Free Shipping')
  599. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  600. ->assertJsonPath('data.shippingMethods.free.rates.0.method', 'free_free')
  601. ->assertJsonPath('data.shippingMethods.free.rates.0.method_title', 'Free Shipping')
  602. ->assertJsonPath('data.shippingMethods.free.rates.0.price', 0)
  603. ->assertJsonPath('data.shippingMethods.free.rates.0.base_price', 0);
  604. $response->assertJsonPath('data.shippingMethods.flatrate.rates.0.cart_address_id', $cart->shipping_address->id);
  605. $response->assertJsonPath('data.shippingMethods.free.rates.0.cart_address_id', $cart->shipping_address->id);
  606. $this->assertModelWise([
  607. CartAddress::class => [
  608. [
  609. 'address' => implode("\n", $billingAddress['address']),
  610. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  611. 'cart_id' => $cart->id,
  612. 'use_for_shipping' => $billingAddress['use_for_shipping'],
  613. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  614. ],
  615. ],
  616. CartAddress::class => [
  617. [
  618. 'address' => implode("\n", $shippingAddress['address']),
  619. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  620. 'cart_id' => $cart->id,
  621. ...Arr::only($shippingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  622. ],
  623. ],
  624. ]);
  625. });
  626. it('should store the billing address for non stockable items for customer', function () {
  627. // Arrange.
  628. $product = (new ProductFaker([
  629. 'attributes' => [
  630. 5 => 'new',
  631. ],
  632. 'attribute_value' => [
  633. 'new' => [
  634. 'boolean_value' => true,
  635. ],
  636. ],
  637. ]))
  638. ->getVirtualProductFactory()
  639. ->create();
  640. $customer = Customer::factory()->create();
  641. $cart = Cart::factory()->create([
  642. 'customer_id' => $customer->id,
  643. 'customer_first_name' => $customer->first_name,
  644. 'customer_last_name' => $customer->last_name,
  645. 'customer_email' => $customer->email,
  646. 'is_guest' => 0,
  647. ]);
  648. $additional = [
  649. 'product_id' => $product->id,
  650. 'rating' => '0',
  651. 'is_buy_now' => '0',
  652. 'quantity' => '1',
  653. ];
  654. CartItem::factory()->create([
  655. 'cart_id' => $cart->id,
  656. 'product_id' => $product->id,
  657. 'sku' => $product->sku,
  658. 'quantity' => $additional['quantity'],
  659. 'name' => $product->name,
  660. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  661. 'price_incl_tax' => $convertedPrice,
  662. 'base_price' => $price,
  663. 'base_price_incl_tax' => $price,
  664. 'total' => $total = $convertedPrice * $additional['quantity'],
  665. 'total_incl_tax' => $total,
  666. 'base_total' => $price * $additional['quantity'],
  667. 'weight' => $product->weight ?? 0,
  668. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  669. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  670. 'type' => $product->type,
  671. 'additional' => $additional,
  672. ]);
  673. $customerAddress = CustomerAddress::factory()->create()->toArray();
  674. $customerAddress = CustomerAddress::factory()->create()->toArray();
  675. cart()->setCart($cart);
  676. // Act and Assert.
  677. $this->loginAsCustomer($customer);
  678. postJson(route('shop.checkout.onepage.addresses.store'), [
  679. 'billing' => $billingAddress = [
  680. ...$customerAddress,
  681. 'address' => [fake()->address()],
  682. 'use_for_shipping' => 1,
  683. ],
  684. ])
  685. ->assertOk()
  686. ->assertJsonPath('data.payment_methods.0.method', 'moneytransfer')
  687. ->assertJsonPath('data.payment_methods.0.method_title', 'Money Transfer')
  688. ->assertJsonPath('data.payment_methods.0.description', 'Money Transfer')
  689. ->assertJsonPath('data.payment_methods.0.sort', 2)
  690. ->assertJsonPath('data.payment_methods.1.method', 'paypal_standard')
  691. ->assertJsonPath('data.payment_methods.1.method_title', 'PayPal Standard')
  692. ->assertJsonPath('data.payment_methods.1.description', 'PayPal Standard')
  693. ->assertJsonPath('data.payment_methods.1.sort', 3)
  694. ->assertJsonPath('data.payment_methods.2.method', 'paypal_smart_button')
  695. ->assertJsonPath('data.payment_methods.2.method_title', 'PayPal Smart Button')
  696. ->assertJsonPath('data.payment_methods.2.description', 'PayPal')
  697. ->assertJsonPath('data.payment_methods.2.sort', 4)
  698. ->assertJsonPath('redirect', false);
  699. $this->assertModelWise([
  700. CartAddress::class => [
  701. [
  702. 'address' => implode("\n", $billingAddress['address']),
  703. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  704. 'cart_id' => $cart->id,
  705. 'use_for_shipping' => $billingAddress['use_for_shipping'],
  706. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  707. ],
  708. ],
  709. ]);
  710. });
  711. it('should fails the certain validation errors when use for shipping is set to false in billing address and shipping address not provided for customer', function () {
  712. // Arrange.
  713. $product = (new ProductFaker([
  714. 'attributes' => [
  715. 5 => 'new',
  716. ],
  717. 'attribute_value' => [
  718. 'new' => [
  719. 'boolean_value' => true,
  720. ],
  721. ],
  722. ]))
  723. ->getSimpleProductFactory()
  724. ->create();
  725. $customer = Customer::factory()->create();
  726. $cart = Cart::factory()->create([
  727. 'customer_id' => $customer->id,
  728. 'customer_first_name' => $customer->first_name,
  729. 'customer_last_name' => $customer->last_name,
  730. 'customer_email' => $customer->email,
  731. 'is_guest' => 0,
  732. ]);
  733. $additional = [
  734. 'product_id' => $product->id,
  735. 'rating' => '0',
  736. 'is_buy_now' => '0',
  737. 'quantity' => '1',
  738. ];
  739. CartItem::factory()->create([
  740. 'cart_id' => $cart->id,
  741. 'product_id' => $product->id,
  742. 'sku' => $product->sku,
  743. 'quantity' => $additional['quantity'],
  744. 'name' => $product->name,
  745. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  746. 'price_incl_tax' => $convertedPrice,
  747. 'base_price' => $price,
  748. 'base_price_incl_tax' => $price,
  749. 'total' => $total = $convertedPrice * $additional['quantity'],
  750. 'total_incl_tax' => $total,
  751. 'base_total' => $price * $additional['quantity'],
  752. 'weight' => $product->weight ?? 0,
  753. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  754. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  755. 'type' => $product->type,
  756. 'additional' => $additional,
  757. ]);
  758. $customerAddress = CustomerAddress::factory()->create()->toArray();
  759. $customerAddress = CustomerAddress::factory()->create()->toArray();
  760. cart()->setCart($cart);
  761. // Act and Assert.
  762. $this->loginAsCustomer($customer);
  763. postJson(route('shop.checkout.onepage.addresses.store'), [
  764. 'billing' => [
  765. ...$customerAddress,
  766. 'address' => [fake()->address()],
  767. 'use_for_shipping' => false,
  768. ],
  769. ])
  770. ->assertJsonValidationErrorFor('shipping.first_name')
  771. ->assertJsonValidationErrorFor('shipping.last_name')
  772. ->assertJsonValidationErrorFor('shipping.email')
  773. ->assertJsonValidationErrorFor('shipping.address')
  774. ->assertJsonValidationErrorFor('shipping.city')
  775. ->assertJsonValidationErrorFor('shipping.phone')
  776. ->assertUnprocessable();
  777. });
  778. it('should fails the certain validation errors when use for shipping is set to false in billing address and shipping address not provided for guest user', function () {
  779. // Arrange.
  780. $product = (new ProductFaker([
  781. 'attributes' => [
  782. 5 => 'new',
  783. 26 => 'guest_checkout',
  784. ],
  785. 'attribute_value' => [
  786. 'new' => [
  787. 'boolean_value' => true,
  788. ],
  789. 'guest_checkout' => [
  790. 'boolean_value' => true,
  791. ],
  792. ],
  793. ]))
  794. ->getSimpleProductFactory()
  795. ->create();
  796. $cart = Cart::factory()->create();
  797. $additional = [
  798. 'product_id' => $product->id,
  799. 'rating' => '0',
  800. 'is_buy_now' => '0',
  801. 'quantity' => '1',
  802. ];
  803. CartItem::factory()->create([
  804. 'cart_id' => $cart->id,
  805. 'product_id' => $product->id,
  806. 'sku' => $product->sku,
  807. 'quantity' => $additional['quantity'],
  808. 'name' => $product->name,
  809. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  810. 'price_incl_tax' => $convertedPrice,
  811. 'base_price' => $price,
  812. 'base_price_incl_tax' => $price,
  813. 'total' => $total = $convertedPrice * $additional['quantity'],
  814. 'total_incl_tax' => $total,
  815. 'base_total' => $price * $additional['quantity'],
  816. 'weight' => $product->weight ?? 0,
  817. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  818. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  819. 'type' => $product->type,
  820. 'additional' => $additional,
  821. ]);
  822. $customerAddress = CustomerAddress::factory()->create()->toArray();
  823. $customerAddress = CustomerAddress::factory()->create()->toArray();
  824. cart()->setCart($cart);
  825. // Act and Assert.
  826. postJson(route('shop.checkout.onepage.addresses.store'), [
  827. 'billing' => [
  828. ...$customerAddress,
  829. 'address' => [fake()->address()],
  830. 'use_for_shipping' => false,
  831. ],
  832. ])
  833. ->assertJsonValidationErrorFor('shipping.first_name')
  834. ->assertJsonValidationErrorFor('shipping.last_name')
  835. ->assertJsonValidationErrorFor('shipping.email')
  836. ->assertJsonValidationErrorFor('shipping.address')
  837. ->assertJsonValidationErrorFor('shipping.city')
  838. ->assertJsonValidationErrorFor('shipping.phone')
  839. ->assertUnprocessable();
  840. });
  841. it('should fails the validation error when shipping method not providing when store the shipping method for the guest user', function () {
  842. // Arrange.
  843. $product = (new ProductFaker([
  844. 'attributes' => [
  845. 5 => 'new',
  846. 26 => 'guest_checkout',
  847. ],
  848. 'attribute_value' => [
  849. 'new' => [
  850. 'boolean_value' => true,
  851. ],
  852. 'guest_checkout' => [
  853. 'boolean_value' => true,
  854. ],
  855. ],
  856. ]))
  857. ->getSimpleProductFactory()
  858. ->create();
  859. $cart = Cart::factory()->create();
  860. $additional = [
  861. 'product_id' => $product->id,
  862. 'rating' => '0',
  863. 'is_buy_now' => '0',
  864. 'quantity' => '1',
  865. ];
  866. CartItem::factory()->create([
  867. 'cart_id' => $cart->id,
  868. 'product_id' => $product->id,
  869. 'sku' => $product->sku,
  870. 'quantity' => $additional['quantity'],
  871. 'name' => $product->name,
  872. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  873. 'price_incl_tax' => $convertedPrice,
  874. 'base_price' => $price,
  875. 'base_price_incl_tax' => $price,
  876. 'total' => $total = $convertedPrice * $additional['quantity'],
  877. 'total_incl_tax' => $total,
  878. 'base_total' => $price * $additional['quantity'],
  879. 'weight' => $product->weight ?? 0,
  880. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  881. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  882. 'type' => $product->type,
  883. 'additional' => $additional,
  884. ]);
  885. CartAddress::factory()->create([
  886. 'cart_id' => $cart->id,
  887. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  888. ]);
  889. CartAddress::factory()->create([
  890. 'cart_id' => $cart->id,
  891. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  892. ]);
  893. cart()->setCart($cart);
  894. // Act and Assert.
  895. postJson(route('shop.checkout.onepage.shipping_methods.store'))
  896. ->assertJsonValidationErrorFor('shipping_method')
  897. ->assertUnprocessable();
  898. });
  899. it('should fails the validation error when shipping method not providing when store the shipping method for the customer', function () {
  900. // Arrange.
  901. $product = (new ProductFaker([
  902. 'attributes' => [
  903. 5 => 'new',
  904. ],
  905. 'attribute_value' => [
  906. 'new' => [
  907. 'boolean_value' => true,
  908. ],
  909. ],
  910. ]))
  911. ->getSimpleProductFactory()
  912. ->create();
  913. $customer = Customer::factory()->create();
  914. $cart = Cart::factory()->create([
  915. 'customer_id' => $customer->id,
  916. 'customer_first_name' => $customer->first_name,
  917. 'customer_last_name' => $customer->last_name,
  918. 'customer_email' => $customer->email,
  919. 'is_guest' => 0,
  920. ]);
  921. $additional = [
  922. 'product_id' => $product->id,
  923. 'rating' => '0',
  924. 'is_buy_now' => '0',
  925. 'quantity' => '1',
  926. ];
  927. CartItem::factory()->create([
  928. 'cart_id' => $cart->id,
  929. 'product_id' => $product->id,
  930. 'sku' => $product->sku,
  931. 'quantity' => $additional['quantity'],
  932. 'name' => $product->name,
  933. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  934. 'price_incl_tax' => $convertedPrice,
  935. 'base_price' => $price,
  936. 'base_price_incl_tax' => $price,
  937. 'total' => $total = $convertedPrice * $additional['quantity'],
  938. 'total_incl_tax' => $total,
  939. 'base_total' => $price * $additional['quantity'],
  940. 'weight' => $product->weight ?? 0,
  941. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  942. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  943. 'type' => $product->type,
  944. 'additional' => $additional,
  945. ]);
  946. CartAddress::factory()->create([
  947. 'cart_id' => $cart->id,
  948. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  949. ]);
  950. CartAddress::factory()->create([
  951. 'cart_id' => $cart->id,
  952. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  953. ]);
  954. cart()->setCart($cart);
  955. // Act and Assert.
  956. $this->loginAsCustomer($customer);
  957. postJson(route('shop.checkout.onepage.shipping_methods.store'))
  958. ->assertJsonValidationErrorFor('shipping_method')
  959. ->assertUnprocessable();
  960. });
  961. it('should store the shipping method for guest user', function () {
  962. // Arrange.
  963. $product = (new ProductFaker([
  964. 'attributes' => [
  965. 5 => 'new',
  966. 26 => 'guest_checkout',
  967. ],
  968. 'attribute_value' => [
  969. 'new' => [
  970. 'boolean_value' => true,
  971. ],
  972. 'guest_checkout' => [
  973. 'boolean_value' => true,
  974. ],
  975. ],
  976. ]))
  977. ->getSimpleProductFactory()
  978. ->create();
  979. $cart = Cart::factory()->create();
  980. $additional = [
  981. 'product_id' => $product->id,
  982. 'rating' => '0',
  983. 'is_buy_now' => '0',
  984. 'quantity' => '1',
  985. ];
  986. CartItem::factory()->create([
  987. 'cart_id' => $cart->id,
  988. 'product_id' => $product->id,
  989. 'sku' => $product->sku,
  990. 'quantity' => $additional['quantity'],
  991. 'name' => $product->name,
  992. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  993. 'price_incl_tax' => $convertedPrice,
  994. 'base_price' => $price,
  995. 'base_price_incl_tax' => $price,
  996. 'total' => $total = $convertedPrice * $additional['quantity'],
  997. 'total_incl_tax' => $total,
  998. 'base_total' => $price * $additional['quantity'],
  999. 'weight' => $product->weight ?? 0,
  1000. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1001. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1002. 'type' => $product->type,
  1003. 'additional' => $additional,
  1004. ]);
  1005. CartAddress::factory()->create([
  1006. 'cart_id' => $cart->id,
  1007. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1008. ]);
  1009. CartAddress::factory()->create([
  1010. 'cart_id' => $cart->id,
  1011. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1012. ]);
  1013. cart()->setCart($cart);
  1014. // Act and Assert.
  1015. postJson(route('shop.checkout.onepage.shipping_methods.store'), [
  1016. 'shipping_method' => 'free_free',
  1017. ])
  1018. ->assertOk()
  1019. ->assertJsonPath('payment_methods.0.method', 'cashondelivery')
  1020. ->assertJsonPath('payment_methods.0.method_title', 'Cash On Delivery')
  1021. ->assertJsonPath('payment_methods.0.description', 'Cash On Delivery')
  1022. ->assertJsonPath('payment_methods.0.sort', 1)
  1023. ->assertJsonPath('payment_methods.1.method', 'moneytransfer')
  1024. ->assertJsonPath('payment_methods.1.method_title', 'Money Transfer')
  1025. ->assertJsonPath('payment_methods.1.description', 'Money Transfer')
  1026. ->assertJsonPath('payment_methods.1.sort', 2)
  1027. ->assertJsonPath('payment_methods.2.method', 'paypal_standard')
  1028. ->assertJsonPath('payment_methods.2.method_title', 'PayPal Standard')
  1029. ->assertJsonPath('payment_methods.2.description', 'PayPal Standard')
  1030. ->assertJsonPath('payment_methods.2.sort', 3)
  1031. ->assertJsonPath('payment_methods.3.method', 'paypal_smart_button')
  1032. ->assertJsonPath('payment_methods.3.method_title', 'PayPal Smart Button')
  1033. ->assertJsonPath('payment_methods.3.description', 'PayPal')
  1034. ->assertJsonPath('payment_methods.3.sort', 4);
  1035. });
  1036. it('should store the shipping method for customer', function () {
  1037. // Arrange.
  1038. $product = (new ProductFaker([
  1039. 'attributes' => [
  1040. 5 => 'new',
  1041. ],
  1042. 'attribute_value' => [
  1043. 'new' => [
  1044. 'boolean_value' => true,
  1045. ],
  1046. ],
  1047. ]))
  1048. ->getSimpleProductFactory()
  1049. ->create();
  1050. $customer = Customer::factory()->create();
  1051. $cart = Cart::factory()->create([
  1052. 'customer_id' => $customer->id,
  1053. 'customer_first_name' => $customer->first_name,
  1054. 'customer_last_name' => $customer->last_name,
  1055. 'customer_email' => $customer->email,
  1056. 'is_guest' => 0,
  1057. ]);
  1058. $additional = [
  1059. 'product_id' => $product->id,
  1060. 'rating' => '0',
  1061. 'is_buy_now' => '0',
  1062. 'quantity' => '1',
  1063. ];
  1064. CartItem::factory()->create([
  1065. 'cart_id' => $cart->id,
  1066. 'product_id' => $product->id,
  1067. 'sku' => $product->sku,
  1068. 'quantity' => $additional['quantity'],
  1069. 'name' => $product->name,
  1070. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1071. 'price_incl_tax' => $convertedPrice,
  1072. 'base_price' => $price,
  1073. 'base_price_incl_tax' => $price,
  1074. 'total' => $total = $convertedPrice * $additional['quantity'],
  1075. 'total_incl_tax' => $total,
  1076. 'base_total' => $price * $additional['quantity'],
  1077. 'weight' => $product->weight ?? 0,
  1078. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1079. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1080. 'type' => $product->type,
  1081. 'additional' => $additional,
  1082. ]);
  1083. CartAddress::factory()->create([
  1084. 'cart_id' => $cart->id,
  1085. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1086. ]);
  1087. CartAddress::factory()->create([
  1088. 'cart_id' => $cart->id,
  1089. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1090. ]);
  1091. cart()->setCart($cart);
  1092. // Act and Assert.
  1093. $this->loginAsCustomer($customer);
  1094. postJson(route('shop.checkout.onepage.shipping_methods.store'), [
  1095. 'shipping_method' => 'free_free',
  1096. ])
  1097. ->assertOk()
  1098. ->assertJsonPath('payment_methods.0.method', 'cashondelivery')
  1099. ->assertJsonPath('payment_methods.0.method_title', 'Cash On Delivery')
  1100. ->assertJsonPath('payment_methods.0.description', 'Cash On Delivery')
  1101. ->assertJsonPath('payment_methods.0.sort', 1)
  1102. ->assertJsonPath('payment_methods.1.method', 'moneytransfer')
  1103. ->assertJsonPath('payment_methods.1.method_title', 'Money Transfer')
  1104. ->assertJsonPath('payment_methods.1.description', 'Money Transfer')
  1105. ->assertJsonPath('payment_methods.1.sort', 2)
  1106. ->assertJsonPath('payment_methods.2.method', 'paypal_standard')
  1107. ->assertJsonPath('payment_methods.2.method_title', 'PayPal Standard')
  1108. ->assertJsonPath('payment_methods.2.description', 'PayPal Standard')
  1109. ->assertJsonPath('payment_methods.2.sort', 3)
  1110. ->assertJsonPath('payment_methods.3.method', 'paypal_smart_button')
  1111. ->assertJsonPath('payment_methods.3.method_title', 'PayPal Smart Button')
  1112. ->assertJsonPath('payment_methods.3.description', 'PayPal')
  1113. ->assertJsonPath('payment_methods.3.sort', 4);
  1114. });
  1115. it('should fails the validation error when store the payment method for guest user', function () {
  1116. // Arrange.
  1117. $product = (new ProductFaker([
  1118. 'attributes' => [
  1119. 5 => 'new',
  1120. 26 => 'guest_checkout',
  1121. ],
  1122. 'attribute_value' => [
  1123. 'new' => [
  1124. 'boolean_value' => true,
  1125. ],
  1126. 'guest_checkout' => [
  1127. 'boolean_value' => true,
  1128. ],
  1129. ],
  1130. ]))
  1131. ->getSimpleProductFactory()
  1132. ->create();
  1133. $cart = Cart::factory()->create();
  1134. $additional = [
  1135. 'product_id' => $product->id,
  1136. 'rating' => '0',
  1137. 'is_buy_now' => '0',
  1138. 'quantity' => '1',
  1139. ];
  1140. CartItem::factory()->create([
  1141. 'cart_id' => $cart->id,
  1142. 'product_id' => $product->id,
  1143. 'sku' => $product->sku,
  1144. 'quantity' => $additional['quantity'],
  1145. 'name' => $product->name,
  1146. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1147. 'price_incl_tax' => $convertedPrice,
  1148. 'base_price' => $price,
  1149. 'base_price_incl_tax' => $price,
  1150. 'total' => $total = $convertedPrice * $additional['quantity'],
  1151. 'total_incl_tax' => $total,
  1152. 'base_total' => $price * $additional['quantity'],
  1153. 'weight' => $product->weight ?? 0,
  1154. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1155. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1156. 'type' => $product->type,
  1157. 'additional' => $additional,
  1158. ]);
  1159. CartAddress::factory()->create([
  1160. 'cart_id' => $cart->id,
  1161. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1162. ]);
  1163. CartAddress::factory()->create([
  1164. 'cart_id' => $cart->id,
  1165. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1166. ]);
  1167. cart()->setCart($cart);
  1168. // Act and Assert.
  1169. postJson(route('shop.checkout.onepage.payment_methods.store'))
  1170. ->assertJsonValidationErrorFor('payment')
  1171. ->assertUnprocessable();
  1172. });
  1173. it('should store the payment method for guest user', function () {
  1174. // Arrange.
  1175. $product = (new ProductFaker([
  1176. 'attributes' => [
  1177. 5 => 'new',
  1178. 26 => 'guest_checkout',
  1179. ],
  1180. 'attribute_value' => [
  1181. 'new' => [
  1182. 'boolean_value' => true,
  1183. ],
  1184. 'guest_checkout' => [
  1185. 'boolean_value' => true,
  1186. ],
  1187. ],
  1188. ]))
  1189. ->getSimpleProductFactory()
  1190. ->create();
  1191. $cart = Cart::factory()->create();
  1192. $additional = [
  1193. 'product_id' => $product->id,
  1194. 'rating' => '0',
  1195. 'is_buy_now' => '0',
  1196. 'quantity' => '1',
  1197. ];
  1198. $cartItem = CartItem::factory()->create([
  1199. 'cart_id' => $cart->id,
  1200. 'product_id' => $product->id,
  1201. 'sku' => $product->sku,
  1202. 'quantity' => $additional['quantity'],
  1203. 'name' => $product->name,
  1204. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1205. 'price_incl_tax' => $convertedPrice,
  1206. 'base_price' => $price,
  1207. 'base_price_incl_tax' => $price,
  1208. 'total' => $total = $convertedPrice * $additional['quantity'],
  1209. 'total_incl_tax' => $total,
  1210. 'base_total' => $price * $additional['quantity'],
  1211. 'weight' => $product->weight ?? 0,
  1212. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1213. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1214. 'type' => $product->type,
  1215. 'additional' => $additional,
  1216. ]);
  1217. $cartBillingAddress = CartAddress::factory()->create([
  1218. 'cart_id' => $cart->id,
  1219. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1220. ]);
  1221. $cartShippingAddress = CartAddress::factory()->create([
  1222. 'cart_id' => $cart->id,
  1223. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1224. ]);
  1225. cart()->setCart($cart);
  1226. cart()->collectTotals();
  1227. // Act and Assert.
  1228. $response = postJson(route('shop.checkout.onepage.payment_methods.store'), [
  1229. 'payment' => [
  1230. 'method' => 'cashondelivery',
  1231. 'method_title' => 'Cash On Delivery',
  1232. 'description' => 'Cash On Delivery',
  1233. 'sort' => 1,
  1234. ],
  1235. ])
  1236. ->assertJsonPath('cart.id', $cart->id)
  1237. ->assertJsonPath('cart.is_guest', 1)
  1238. ->assertJsonPath('cart.items_count', 1)
  1239. ->assertJsonPath('cart.customer_id', null)
  1240. ->assertJsonPath('cart.items_count', 1)
  1241. ->assertJsonPath('cart.items_qty', 1)
  1242. ->assertJsonPath('cart.items.0.id', $cartItem->id)
  1243. ->assertJsonPath('cart.items.0.quantity', 1)
  1244. ->assertJsonPath('cart.items.0.type', $cartItem->type)
  1245. ->assertJsonPath('cart.items.0.name', $cartItem->name)
  1246. ->assertJsonPath('cart.items.0.price', $cartItem->price)
  1247. ->assertJsonPath('cart.items.0.formatted_price', core()->formatPrice($cartItem->price))
  1248. ->assertJsonPath('cart.items.0.formatted_total', core()->formatPrice($cartItem->total))
  1249. ->assertJsonPath('cart.items.0.options', array_values($cartItem->additional['attributes'] ?? []))
  1250. ->assertJsonPath('cart.items.0.base_image', $cartItem->getTypeInstance()->getBaseImage($cartItem))
  1251. ->assertJsonPath('cart.items.0.product_url_key', $cartItem->product->url_key)
  1252. ->assertJsonPath('cart.billing_address.id', $cartBillingAddress->id)
  1253. ->assertJsonPath('cart.billing_address.address_type', $cartBillingAddress->address_type)
  1254. ->assertJsonPath('cart.billing_address.parent_address_id', $cartBillingAddress->parent_address_id)
  1255. ->assertJsonPath('cart.billing_address.customer_id', $cartBillingAddress->customer_id)
  1256. ->assertJsonPath('cart.billing_address.cart_id', $cart->id)
  1257. ->assertJsonPath('cart.billing_address.order_id', null)
  1258. ->assertJsonPath('cart.billing_address.first_name', $cartBillingAddress->first_name)
  1259. ->assertJsonPath('cart.billing_address.last_name', $cartBillingAddress->last_name)
  1260. ->assertJsonPath('cart.billing_address.gender', $cartBillingAddress->gender)
  1261. ->assertJsonPath('cart.billing_address.company_name', $cartBillingAddress->company_name)
  1262. ->assertJsonPath('cart.billing_address.address', explode("\n", $cartBillingAddress->address))
  1263. ->assertJsonPath('cart.billing_address.city', $cartBillingAddress->city)
  1264. ->assertJsonPath('cart.billing_address.state', $cartBillingAddress->state)
  1265. ->assertJsonPath('cart.billing_address.country', $cartBillingAddress->country)
  1266. ->assertJsonPath('cart.billing_address.postcode', $cartBillingAddress->postcode)
  1267. ->assertJsonPath('cart.billing_address.email', $cartBillingAddress->email)
  1268. ->assertJsonPath('cart.billing_address.phone', $cartBillingAddress->phone)
  1269. ->assertJsonPath('cart.billing_address.vat_id', $cartBillingAddress->vat_id)
  1270. ->assertJsonPath('cart.shipping_address.id', $cartShippingAddress->id)
  1271. ->assertJsonPath('cart.shipping_address.address_type', $cartShippingAddress->address_type)
  1272. ->assertJsonPath('cart.shipping_address.parent_address_id', $cartShippingAddress->parent_address_id)
  1273. ->assertJsonPath('cart.shipping_address.customer_id', $cartShippingAddress->customer_id)
  1274. ->assertJsonPath('cart.shipping_address.cart_id', $cart->id)
  1275. ->assertJsonPath('cart.shipping_address.order_id', null)
  1276. ->assertJsonPath('cart.shipping_address.first_name', $cartShippingAddress->first_name)
  1277. ->assertJsonPath('cart.shipping_address.last_name', $cartShippingAddress->last_name)
  1278. ->assertJsonPath('cart.shipping_address.gender', $cartShippingAddress->gender)
  1279. ->assertJsonPath('cart.shipping_address.company_name', $cartShippingAddress->company_name)
  1280. ->assertJsonPath('cart.shipping_address.address', explode("\n", $cartShippingAddress->address))
  1281. ->assertJsonPath('cart.shipping_address.city', $cartShippingAddress->city)
  1282. ->assertJsonPath('cart.shipping_address.state', $cartShippingAddress->state)
  1283. ->assertJsonPath('cart.shipping_address.country', $cartShippingAddress->country)
  1284. ->assertJsonPath('cart.shipping_address.postcode', $cartShippingAddress->postcode)
  1285. ->assertJsonPath('cart.shipping_address.email', $cartShippingAddress->email)
  1286. ->assertJsonPath('cart.shipping_address.phone', $cartShippingAddress->phone)
  1287. ->assertJsonPath('cart.shipping_address.vat_id', $cartShippingAddress->vat_id)
  1288. ->assertJsonPath('cart.have_stockable_items', true)
  1289. ->assertJsonPath('cart.payment_method', 'cashondelivery')
  1290. ->assertJsonPath('cart.payment_method_title', 'Cash On Delivery')
  1291. ->assertOk();
  1292. $this->assertPrice($product->price, $response['cart']['grand_total']);
  1293. $this->assertPrice($cartItem->total, $response['cart']['items']['0']['total']);
  1294. $this->assertPrice($product->price, $response['cart']['sub_total']);
  1295. });
  1296. it('should store the payment method for customer', function () {
  1297. // Arrange.
  1298. $product = (new ProductFaker([
  1299. 'attributes' => [
  1300. 5 => 'new',
  1301. ],
  1302. 'attribute_value' => [
  1303. 'new' => [
  1304. 'boolean_value' => true,
  1305. ],
  1306. ],
  1307. ]))
  1308. ->getSimpleProductFactory()
  1309. ->create();
  1310. $customer = Customer::factory()->create();
  1311. $cart = Cart::factory()->create([
  1312. 'customer_id' => $customer->id,
  1313. 'customer_first_name' => $customer->first_name,
  1314. 'customer_last_name' => $customer->last_name,
  1315. 'customer_email' => $customer->email,
  1316. 'is_guest' => 0,
  1317. ]);
  1318. $additional = [
  1319. 'product_id' => $product->id,
  1320. 'rating' => '0',
  1321. 'is_buy_now' => '0',
  1322. 'quantity' => '1',
  1323. ];
  1324. $cartItem = CartItem::factory()->create([
  1325. 'cart_id' => $cart->id,
  1326. 'product_id' => $product->id,
  1327. 'sku' => $product->sku,
  1328. 'quantity' => $additional['quantity'],
  1329. 'name' => $product->name,
  1330. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1331. 'price_incl_tax' => $convertedPrice,
  1332. 'base_price' => $price,
  1333. 'base_price_incl_tax' => $price,
  1334. 'total' => $total = $convertedPrice * $additional['quantity'],
  1335. 'total_incl_tax' => $total,
  1336. 'base_total' => $price * $additional['quantity'],
  1337. 'weight' => $product->weight ?? 0,
  1338. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1339. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1340. 'type' => $product->type,
  1341. 'additional' => $additional,
  1342. ]);
  1343. $cartBillingAddress = CartAddress::factory()->create([
  1344. 'cart_id' => $cart->id,
  1345. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1346. ]);
  1347. $cartShippingAddress = CartAddress::factory()->create([
  1348. 'cart_id' => $cart->id,
  1349. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1350. ]);
  1351. cart()->setCart($cart);
  1352. // Act and Assert.
  1353. $this->loginAsCustomer($customer);
  1354. $response = postJson(route('shop.checkout.onepage.payment_methods.store'), [
  1355. 'payment' => [
  1356. 'method' => 'cashondelivery',
  1357. 'method_title' => 'Cash On Delivery',
  1358. 'description' => 'Cash On Delivery',
  1359. 'sort' => 1,
  1360. ],
  1361. ])
  1362. ->assertJsonPath('cart.id', $cart->id)
  1363. ->assertJsonPath('cart.is_guest', 0)
  1364. ->assertJsonPath('cart.items_count', 1)
  1365. ->assertJsonPath('cart.customer_id', $customer->id)
  1366. ->assertJsonPath('cart.items_count', 1)
  1367. ->assertJsonPath('cart.items_qty', 1)
  1368. ->assertJsonPath('cart.items.0.id', $cartItem->id)
  1369. ->assertJsonPath('cart.items.0.quantity', 1)
  1370. ->assertJsonPath('cart.items.0.type', $cartItem->type)
  1371. ->assertJsonPath('cart.items.0.name', $cartItem->name)
  1372. ->assertJsonPath('cart.items.0.price', $cartItem->price)
  1373. ->assertJsonPath('cart.items.0.formatted_price', core()->formatPrice($cartItem->price))
  1374. ->assertJsonPath('cart.items.0.formatted_total', core()->formatPrice($cartItem->total))
  1375. ->assertJsonPath('cart.items.0.options', array_values($cartItem->additional['attributes'] ?? []))
  1376. ->assertJsonPath('cart.items.0.base_image', $cartItem->getTypeInstance()->getBaseImage($cartItem))
  1377. ->assertJsonPath('cart.items.0.product_url_key', $cartItem->product->url_key)
  1378. ->assertJsonPath('cart.billing_address.id', $cartBillingAddress->id)
  1379. ->assertJsonPath('cart.billing_address.address_type', $cartBillingAddress->address_type)
  1380. ->assertJsonPath('cart.billing_address.parent_address_id', $cartBillingAddress->parent_address_id)
  1381. ->assertJsonPath('cart.billing_address.customer_id', $cartBillingAddress->customer_id)
  1382. ->assertJsonPath('cart.billing_address.cart_id', $cart->id)
  1383. ->assertJsonPath('cart.billing_address.order_id', null)
  1384. ->assertJsonPath('cart.billing_address.first_name', $cartBillingAddress->first_name)
  1385. ->assertJsonPath('cart.billing_address.last_name', $cartBillingAddress->last_name)
  1386. ->assertJsonPath('cart.billing_address.gender', $cartBillingAddress->gender)
  1387. ->assertJsonPath('cart.billing_address.company_name', $cartBillingAddress->company_name)
  1388. ->assertJsonPath('cart.billing_address.address', explode("\n", $cartBillingAddress->address))
  1389. ->assertJsonPath('cart.billing_address.city', $cartBillingAddress->city)
  1390. ->assertJsonPath('cart.billing_address.state', $cartBillingAddress->state)
  1391. ->assertJsonPath('cart.billing_address.country', $cartBillingAddress->country)
  1392. ->assertJsonPath('cart.billing_address.postcode', $cartBillingAddress->postcode)
  1393. ->assertJsonPath('cart.billing_address.email', $cartBillingAddress->email)
  1394. ->assertJsonPath('cart.billing_address.phone', $cartBillingAddress->phone)
  1395. ->assertJsonPath('cart.billing_address.vat_id', $cartBillingAddress->vat_id)
  1396. ->assertJsonPath('cart.shipping_address.id', $cartShippingAddress->id)
  1397. ->assertJsonPath('cart.shipping_address.address_type', $cartShippingAddress->address_type)
  1398. ->assertJsonPath('cart.shipping_address.parent_address_id', $cartShippingAddress->parent_address_id)
  1399. ->assertJsonPath('cart.shipping_address.customer_id', $cartShippingAddress->customer_id)
  1400. ->assertJsonPath('cart.shipping_address.cart_id', $cart->id)
  1401. ->assertJsonPath('cart.shipping_address.order_id', null)
  1402. ->assertJsonPath('cart.shipping_address.first_name', $cartShippingAddress->first_name)
  1403. ->assertJsonPath('cart.shipping_address.last_name', $cartShippingAddress->last_name)
  1404. ->assertJsonPath('cart.shipping_address.gender', $cartShippingAddress->gender)
  1405. ->assertJsonPath('cart.shipping_address.company_name', $cartShippingAddress->company_name)
  1406. ->assertJsonPath('cart.shipping_address.address', explode("\n", $cartShippingAddress->address))
  1407. ->assertJsonPath('cart.shipping_address.city', $cartShippingAddress->city)
  1408. ->assertJsonPath('cart.shipping_address.state', $cartShippingAddress->state)
  1409. ->assertJsonPath('cart.shipping_address.country', $cartShippingAddress->country)
  1410. ->assertJsonPath('cart.shipping_address.postcode', $cartShippingAddress->postcode)
  1411. ->assertJsonPath('cart.shipping_address.email', $cartShippingAddress->email)
  1412. ->assertJsonPath('cart.shipping_address.phone', $cartShippingAddress->phone)
  1413. ->assertJsonPath('cart.shipping_address.vat_id', $cartShippingAddress->vat_id)
  1414. ->assertJsonPath('cart.have_stockable_items', true)
  1415. ->assertJsonPath('cart.payment_method', 'cashondelivery')
  1416. ->assertJsonPath('cart.payment_method_title', 'Cash On Delivery')
  1417. ->assertOk();
  1418. $this->assertPrice($product->price, $response['cart']['grand_total']);
  1419. $this->assertPrice($cartItem->total, $response['cart']['items']['0']['total']);
  1420. $this->assertPrice($product->price, $response['cart']['sub_total']);
  1421. });
  1422. it('should place a simple product order for a guest user', function () {
  1423. // Arrange.
  1424. $product = (new ProductFaker([
  1425. 'attributes' => [
  1426. 5 => 'new',
  1427. 26 => 'guest_checkout',
  1428. ],
  1429. 'attribute_value' => [
  1430. 'new' => [
  1431. 'boolean_value' => true,
  1432. ],
  1433. 'guest_checkout' => [
  1434. 'boolean_value' => true,
  1435. ],
  1436. ],
  1437. ]))
  1438. ->getSimpleProductFactory()
  1439. ->create();
  1440. $cart = Cart::factory()->create(['shipping_method' => 'free_free']);
  1441. $additional = [
  1442. 'product_id' => $product->id,
  1443. 'rating' => '0',
  1444. 'is_buy_now' => '0',
  1445. 'quantity' => '1',
  1446. ];
  1447. $cartItem = CartItem::factory()->create([
  1448. 'cart_id' => $cart->id,
  1449. 'product_id' => $product->id,
  1450. 'sku' => $product->sku,
  1451. 'quantity' => $additional['quantity'],
  1452. 'name' => $product->name,
  1453. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1454. 'price_incl_tax' => $convertedPrice,
  1455. 'base_price' => $price,
  1456. 'base_price_incl_tax' => $price,
  1457. 'total' => $total = $convertedPrice * $additional['quantity'],
  1458. 'total_incl_tax' => $total,
  1459. 'base_total' => $price * $additional['quantity'],
  1460. 'weight' => $product->weight ?? 0,
  1461. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1462. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1463. 'type' => $product->type,
  1464. 'additional' => $additional,
  1465. ]);
  1466. $cartBillingAddress = CartAddress::factory()->create([
  1467. 'cart_id' => $cart->id,
  1468. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1469. ]);
  1470. $cartShippingAddress = CartAddress::factory()->create([
  1471. 'cart_id' => $cart->id,
  1472. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1473. ]);
  1474. $cartPayment = CartPayment::factory()->create([
  1475. 'cart_id' => $cart->id,
  1476. 'method' => $paymentMethod = 'cashondelivery',
  1477. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1478. ]);
  1479. $cartShippingRate = CartShippingRate::factory()->create([
  1480. 'carrier' => 'free',
  1481. 'carrier_title' => 'Free shipping',
  1482. 'method' => 'free_free',
  1483. 'method_title' => 'Free Shipping',
  1484. 'method_description' => 'Free Shipping',
  1485. 'cart_address_id' => $cartShippingAddress->id,
  1486. 'cart_id' => $cart->id,
  1487. ]);
  1488. cart()->setCart($cart);
  1489. cart()->collectTotals();
  1490. // Act and Assert.
  1491. postJson(route('shop.checkout.onepage.orders.store'))
  1492. ->assertOk()
  1493. ->assertJsonPath('data.redirect', true)
  1494. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  1495. $cart->refresh();
  1496. $cartItem->refresh();
  1497. $cartPayment->refresh();
  1498. $cartBillingAddress->refresh();
  1499. $cartShippingAddress->refresh();
  1500. $this->assertModelWise([
  1501. Cart::class => [
  1502. $this->prepareCart($cart),
  1503. ],
  1504. CartItem::class => [
  1505. $this->prepareCartItem($cartItem),
  1506. ],
  1507. CartPayment::class => [
  1508. $this->prepareCartPayment($cartPayment),
  1509. ],
  1510. CartAddress::class => [
  1511. $this->prepareAddress($cartBillingAddress),
  1512. $this->prepareAddress($cartShippingAddress),
  1513. ],
  1514. CartShippingRate::class => [
  1515. $this->prepareCartShippingRate($cartShippingRate),
  1516. ],
  1517. Order::class => [
  1518. $this->prepareOrderUsingCart($cart),
  1519. ],
  1520. OrderItem::class => [
  1521. $this->prepareOrderItemUsingCartItem($cartItem),
  1522. ],
  1523. OrderPayment::class => [
  1524. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  1525. ],
  1526. ProductOrderedInventory::class => [
  1527. [
  1528. 'qty' => $cartItem->quantity,
  1529. 'product_id' => $product->id,
  1530. ],
  1531. ],
  1532. ProductInventoryIndex::class => [
  1533. [
  1534. 'qty' => $product->inventory_source_qty(1) - $cartItem->quantity,
  1535. 'product_id' => $product->id,
  1536. ],
  1537. ],
  1538. ]);
  1539. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  1540. $cartShippingAddress->address_type = OrderAddress::ADDRESS_TYPE_SHIPPING;
  1541. $this->assertModelWise([
  1542. OrderAddress::class => [
  1543. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  1544. $this->prepareAddress($cartShippingAddress, OrderAddress::ADDRESS_TYPE_SHIPPING),
  1545. ],
  1546. ]);
  1547. });
  1548. it('should place a simple product order for a guest user and send mail to guest and admin', function () {
  1549. // Arrange.
  1550. Mail::fake();
  1551. $product = (new ProductFaker([
  1552. 'attributes' => [
  1553. 5 => 'new',
  1554. 26 => 'guest_checkout',
  1555. ],
  1556. 'attribute_value' => [
  1557. 'new' => [
  1558. 'boolean_value' => true,
  1559. ],
  1560. 'guest_checkout' => [
  1561. 'boolean_value' => true,
  1562. ],
  1563. ],
  1564. ]))
  1565. ->getSimpleProductFactory()
  1566. ->create();
  1567. $cart = Cart::factory()->create(['shipping_method' => 'free_free']);
  1568. $additional = [
  1569. 'product_id' => $product->id,
  1570. 'rating' => '0',
  1571. 'is_buy_now' => '0',
  1572. 'quantity' => '1',
  1573. ];
  1574. $cartItem = CartItem::factory()->create([
  1575. 'cart_id' => $cart->id,
  1576. 'product_id' => $product->id,
  1577. 'sku' => $product->sku,
  1578. 'quantity' => $additional['quantity'],
  1579. 'name' => $product->name,
  1580. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1581. 'price_incl_tax' => $convertedPrice,
  1582. 'base_price' => $price,
  1583. 'base_price_incl_tax' => $price,
  1584. 'total' => $total = $convertedPrice * $additional['quantity'],
  1585. 'total_incl_tax' => $total,
  1586. 'base_total' => $price * $additional['quantity'],
  1587. 'weight' => $product->weight ?? 0,
  1588. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1589. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1590. 'type' => $product->type,
  1591. 'additional' => $additional,
  1592. ]);
  1593. $cartBillingAddress = CartAddress::factory()->create([
  1594. 'cart_id' => $cart->id,
  1595. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1596. ]);
  1597. $cartShippingAddress = CartAddress::factory()->create([
  1598. 'cart_id' => $cart->id,
  1599. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1600. ]);
  1601. $cartPayment = CartPayment::factory()->create([
  1602. 'method' => $paymentMethod = 'cashondelivery',
  1603. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1604. 'cart_id' => $cart->id,
  1605. ]);
  1606. $cartShippingRate = CartShippingRate::factory()->create([
  1607. 'carrier' => 'free',
  1608. 'carrier_title' => 'Free shipping',
  1609. 'method' => 'free_free',
  1610. 'method_description' => 'Free Shipping',
  1611. 'method_title' => 'Free Shipping',
  1612. 'cart_address_id' => $cartShippingAddress->id,
  1613. 'cart_id' => $cart->id,
  1614. ]);
  1615. cart()->setCart($cart);
  1616. cart()->collectTotals();
  1617. // Act and Assert.
  1618. postJson(route('shop.checkout.onepage.orders.store'))
  1619. ->assertOk()
  1620. ->assertJsonPath('data.redirect', true)
  1621. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  1622. $cart->refresh();
  1623. $cartItem->refresh();
  1624. $cartPayment->refresh();
  1625. $cartBillingAddress->refresh();
  1626. $cartShippingAddress->refresh();
  1627. $this->assertModelWise([
  1628. Cart::class => [
  1629. $this->prepareCart($cart),
  1630. ],
  1631. CartItem::class => [
  1632. $this->prepareCartItem($cartItem),
  1633. ],
  1634. CartPayment::class => [
  1635. $this->prepareCartPayment($cartPayment),
  1636. ],
  1637. CartAddress::class => [
  1638. $this->prepareAddress($cartBillingAddress),
  1639. $this->prepareAddress($cartShippingAddress),
  1640. ],
  1641. CartShippingRate::class => [
  1642. $this->prepareCartShippingRate($cartShippingRate),
  1643. ],
  1644. Order::class => [
  1645. $this->prepareOrderUsingCart($cart),
  1646. ],
  1647. OrderItem::class => [
  1648. $this->prepareOrderItemUsingCartItem($cartItem),
  1649. ],
  1650. OrderPayment::class => [
  1651. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  1652. ],
  1653. ProductOrderedInventory::class => [
  1654. [
  1655. 'qty' => $cartItem->quantity,
  1656. 'product_id' => $product->id,
  1657. ],
  1658. ],
  1659. ProductInventoryIndex::class => [
  1660. [
  1661. 'qty' => $product->inventory_source_qty(1) - $cartItem->quantity,
  1662. 'product_id' => $product->id,
  1663. ],
  1664. ],
  1665. ]);
  1666. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  1667. $cartShippingAddress->address_type = OrderAddress::ADDRESS_TYPE_SHIPPING;
  1668. $this->assertModelWise([
  1669. OrderAddress::class => [
  1670. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  1671. $this->prepareAddress($cartShippingAddress, OrderAddress::ADDRESS_TYPE_SHIPPING),
  1672. ],
  1673. ]);
  1674. Mail::assertQueued(AdminOrderCreatedNotification::class);
  1675. Mail::assertQueued(ShopOrderCreatedNotification::class);
  1676. Mail::assertQueuedCount(2);
  1677. });
  1678. it('should place a simple product order for a customer', function () {
  1679. // Arrange.
  1680. $product = (new ProductFaker([
  1681. 'attributes' => [
  1682. 5 => 'new',
  1683. ],
  1684. 'attribute_value' => [
  1685. 'new' => [
  1686. 'boolean_value' => true,
  1687. ],
  1688. ],
  1689. ]))
  1690. ->getSimpleProductFactory()
  1691. ->create();
  1692. $customer = Customer::factory()->create();
  1693. $cart = Cart::factory()->create([
  1694. 'customer_id' => $customer->id,
  1695. 'customer_first_name' => $customer->first_name,
  1696. 'customer_last_name' => $customer->last_name,
  1697. 'customer_email' => $customer->email,
  1698. 'is_guest' => 0,
  1699. 'shipping_method' => 'free_free',
  1700. ]);
  1701. $additional = [
  1702. 'product_id' => $product->id,
  1703. 'rating' => '0',
  1704. 'is_buy_now' => '0',
  1705. 'quantity' => '1',
  1706. ];
  1707. $cartItem = CartItem::factory()->create([
  1708. 'cart_id' => $cart->id,
  1709. 'product_id' => $product->id,
  1710. 'sku' => $product->sku,
  1711. 'quantity' => $additional['quantity'],
  1712. 'name' => $product->name,
  1713. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1714. 'price_incl_tax' => $convertedPrice,
  1715. 'base_price' => $price,
  1716. 'base_price_incl_tax' => $price,
  1717. 'total' => $total = $convertedPrice * $additional['quantity'],
  1718. 'total_incl_tax' => $total,
  1719. 'base_total' => $price * $additional['quantity'],
  1720. 'weight' => $product->weight ?? 0,
  1721. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1722. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1723. 'type' => $product->type,
  1724. 'additional' => $additional,
  1725. ]);
  1726. $cartBillingAddress = CartAddress::factory()->create([
  1727. 'cart_id' => $cart->id,
  1728. 'customer_id' => $customer->id,
  1729. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1730. ]);
  1731. $cartShippingAddress = CartAddress::factory()->create([
  1732. 'cart_id' => $cart->id,
  1733. 'customer_id' => $customer->id,
  1734. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1735. ]);
  1736. $cartPayment = CartPayment::factory()->create([
  1737. 'cart_id' => $cart->id,
  1738. 'method' => $paymentMethod = 'cashondelivery',
  1739. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1740. ]);
  1741. $cartShippingRate = CartShippingRate::factory()->create([
  1742. 'carrier' => 'free',
  1743. 'carrier_title' => 'Free shipping',
  1744. 'method' => 'free_free',
  1745. 'method_title' => 'Free Shipping',
  1746. 'method_description' => 'Free Shipping',
  1747. 'cart_address_id' => $cartShippingAddress->id,
  1748. 'cart_id' => $cart->id,
  1749. ]);
  1750. cart()->setCart($cart);
  1751. cart()->collectTotals();
  1752. // Act and Assert.
  1753. $this->loginAsCustomer($customer);
  1754. postJson(route('shop.checkout.onepage.orders.store'))
  1755. ->assertOk()
  1756. ->assertJsonPath('data.redirect', true)
  1757. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  1758. $cart->refresh();
  1759. $cartItem->refresh();
  1760. $cartPayment->refresh();
  1761. $cartBillingAddress->refresh();
  1762. $cartShippingAddress->refresh();
  1763. $this->assertModelWise([
  1764. Cart::class => [
  1765. $this->prepareCart($cart),
  1766. ],
  1767. CartItem::class => [
  1768. $this->prepareCartItem($cartItem),
  1769. ],
  1770. CartPayment::class => [
  1771. $this->prepareCartPayment($cartPayment),
  1772. ],
  1773. CartAddress::class => [
  1774. $this->prepareAddress($cartBillingAddress),
  1775. $this->prepareAddress($cartShippingAddress),
  1776. ],
  1777. CartShippingRate::class => [
  1778. $this->prepareCartShippingRate($cartShippingRate),
  1779. ],
  1780. Order::class => [
  1781. $this->prepareOrderUsingCart($cart),
  1782. ],
  1783. OrderItem::class => [
  1784. $this->prepareOrderItemUsingCartItem($cartItem),
  1785. ],
  1786. OrderPayment::class => [
  1787. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  1788. ],
  1789. ProductOrderedInventory::class => [
  1790. [
  1791. 'qty' => $cartItem->quantity,
  1792. 'product_id' => $product->id,
  1793. ],
  1794. ],
  1795. ProductInventoryIndex::class => [
  1796. [
  1797. 'qty' => $product->inventory_source_qty(1) - $cartItem->quantity,
  1798. 'product_id' => $product->id,
  1799. ],
  1800. ],
  1801. ]);
  1802. $this->assertModelWise([
  1803. OrderAddress::class => [
  1804. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  1805. $this->prepareAddress($cartShippingAddress, OrderAddress::ADDRESS_TYPE_SHIPPING),
  1806. ],
  1807. ]);
  1808. });
  1809. it('should place a simple product order for a customer and send email to the customer', function () {
  1810. // Arrange.
  1811. Mail::fake();
  1812. $product = (new ProductFaker([
  1813. 'attributes' => [
  1814. 5 => 'new',
  1815. ],
  1816. 'attribute_value' => [
  1817. 'new' => [
  1818. 'boolean_value' => true,
  1819. ],
  1820. ],
  1821. ]))
  1822. ->getSimpleProductFactory()
  1823. ->create();
  1824. $customer = Customer::factory()->create();
  1825. $cart = Cart::factory()->create([
  1826. 'customer_id' => $customer->id,
  1827. 'customer_first_name' => $customer->first_name,
  1828. 'customer_last_name' => $customer->last_name,
  1829. 'customer_email' => $customer->email,
  1830. 'is_guest' => 0,
  1831. 'shipping_method' => 'free_free',
  1832. ]);
  1833. $additional = [
  1834. 'product_id' => $product->id,
  1835. 'rating' => '0',
  1836. 'is_buy_now' => '0',
  1837. 'quantity' => '1',
  1838. ];
  1839. $cartItem = CartItem::factory()->create([
  1840. 'cart_id' => $cart->id,
  1841. 'product_id' => $product->id,
  1842. 'sku' => $product->sku,
  1843. 'quantity' => $additional['quantity'],
  1844. 'name' => $product->name,
  1845. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1846. 'price_incl_tax' => $convertedPrice,
  1847. 'base_price' => $price,
  1848. 'base_price_incl_tax' => $price,
  1849. 'total' => $total = $convertedPrice * $additional['quantity'],
  1850. 'total_incl_tax' => $total,
  1851. 'base_total' => $price * $additional['quantity'],
  1852. 'weight' => $product->weight ?? 0,
  1853. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1854. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1855. 'type' => $product->type,
  1856. 'additional' => $additional,
  1857. ]);
  1858. $cartBillingAddress = CartAddress::factory()->create([
  1859. 'cart_id' => $cart->id,
  1860. 'customer_id' => $customer->id,
  1861. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1862. ]);
  1863. $cartShippingAddress = CartAddress::factory()->create([
  1864. 'cart_id' => $cart->id,
  1865. 'customer_id' => $customer->id,
  1866. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1867. ]);
  1868. $cartPayment = CartPayment::factory()->create([
  1869. 'cart_id' => $cart->id,
  1870. 'method' => $paymentMethod = 'cashondelivery',
  1871. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1872. ]);
  1873. $cartShippingRate = CartShippingRate::factory()->create([
  1874. 'carrier' => 'free',
  1875. 'carrier_title' => 'Free shipping',
  1876. 'method' => 'free_free',
  1877. 'method_title' => 'Free Shipping',
  1878. 'method_description' => 'Free Shipping',
  1879. 'cart_address_id' => $cartShippingAddress->id,
  1880. 'cart_id' => $cart->id,
  1881. ]);
  1882. cart()->setCart($cart);
  1883. cart()->collectTotals();
  1884. // Act and Assert.
  1885. $this->loginAsCustomer($customer);
  1886. postJson(route('shop.checkout.onepage.orders.store'))
  1887. ->assertOk()
  1888. ->assertJsonPath('data.redirect', true)
  1889. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  1890. $cart->refresh();
  1891. $cartItem->refresh();
  1892. $cartPayment->refresh();
  1893. $cartBillingAddress->refresh();
  1894. $cartShippingAddress->refresh();
  1895. $this->assertModelWise([
  1896. Cart::class => [
  1897. $this->prepareCart($cart),
  1898. ],
  1899. CartItem::class => [
  1900. $this->prepareCartItem($cartItem),
  1901. ],
  1902. CartPayment::class => [
  1903. $this->prepareCartPayment($cartPayment),
  1904. ],
  1905. CartAddress::class => [
  1906. $this->prepareAddress($cartBillingAddress),
  1907. $this->prepareAddress($cartShippingAddress),
  1908. ],
  1909. CartShippingRate::class => [
  1910. $this->prepareCartShippingRate($cartShippingRate),
  1911. ],
  1912. Order::class => [
  1913. $this->prepareOrderUsingCart($cart),
  1914. ],
  1915. OrderItem::class => [
  1916. $this->prepareOrderItemUsingCartItem($cartItem),
  1917. ],
  1918. OrderPayment::class => [
  1919. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  1920. ],
  1921. ProductOrderedInventory::class => [
  1922. [
  1923. 'qty' => $cartItem->quantity,
  1924. 'product_id' => $product->id,
  1925. ],
  1926. ],
  1927. ProductInventoryIndex::class => [
  1928. [
  1929. 'qty' => $product->inventory_source_qty(1) - $cartItem->quantity,
  1930. 'product_id' => $product->id,
  1931. ],
  1932. ],
  1933. ]);
  1934. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  1935. $cartShippingAddress->address_type = OrderAddress::ADDRESS_TYPE_SHIPPING;
  1936. $this->assertModelWise([
  1937. OrderAddress::class => [
  1938. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  1939. $this->prepareAddress($cartShippingAddress, OrderAddress::ADDRESS_TYPE_SHIPPING),
  1940. ],
  1941. ]);
  1942. Mail::assertQueued(AdminOrderCreatedNotification::class);
  1943. Mail::assertQueued(ShopOrderCreatedNotification::class);
  1944. Mail::assertQueuedCount(2);
  1945. });
  1946. it('should place a configurable product order for a guest user', function () {
  1947. // Arrange.
  1948. $product = (new ProductFaker([
  1949. 'attributes' => [
  1950. 5 => 'new',
  1951. 6 => 'featured',
  1952. 26 => 'guest_checkout',
  1953. ],
  1954. 'attribute_value' => [
  1955. 'new' => [
  1956. 'boolean_value' => true,
  1957. ],
  1958. 'featured' => [
  1959. 'boolean_value' => true,
  1960. ],
  1961. 'guest_checkout' => [
  1962. 'boolean_value' => true,
  1963. ],
  1964. ],
  1965. ]))
  1966. ->getConfigurableProductFactory()
  1967. ->create();
  1968. $childProduct = $product->variants()->first();
  1969. foreach ($product->super_attributes as $attribute) {
  1970. foreach ($attribute->options as $option) {
  1971. $superAttributes[$option->attribute_id] = $option->id;
  1972. }
  1973. }
  1974. $additional = [
  1975. 'selected_configurable_option' => $childProduct->id,
  1976. 'product_id' => $product->id,
  1977. 'is_buy_now' => '0',
  1978. 'rating' => '0',
  1979. 'quantity' => '1',
  1980. 'super_attribute' => $superAttributes ?? [],
  1981. ];
  1982. $cart = Cart::factory()->create(['shipping_method' => 'free_free']);
  1983. $cartItem = CartItem::factory()->create([
  1984. 'cart_id' => $cart->id,
  1985. 'product_id' => $childProduct->id,
  1986. 'sku' => $childProduct->sku,
  1987. 'quantity' => $additional['quantity'],
  1988. 'name' => $childProduct->name,
  1989. 'price' => $convertedPrice = core()->convertPrice($price = $childProduct->price),
  1990. 'base_price' => $price,
  1991. 'total' => $convertedPrice * $additional['quantity'],
  1992. 'base_total' => $price * $additional['quantity'],
  1993. 'weight' => $childProduct->weight ?? 0,
  1994. 'total_weight' => ($childProduct->weight ?? 0) * $additional['quantity'],
  1995. 'base_total_weight' => ($childProduct->weight ?? 0) * $additional['quantity'],
  1996. 'type' => $childProduct->type,
  1997. 'additional' => $additional,
  1998. ]);
  1999. $cartBillingAddress = CartAddress::factory()->create([
  2000. 'cart_id' => $cart->id,
  2001. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2002. ]);
  2003. $cartShippingAddress = CartAddress::factory()->create([
  2004. 'cart_id' => $cart->id,
  2005. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  2006. ]);
  2007. $cartShippingRate = CartShippingRate::factory()->create([
  2008. 'method_description' => 'Free Shipping',
  2009. 'carrier_title' => 'Free shipping',
  2010. 'method_title' => 'Free Shipping',
  2011. 'carrier' => 'free',
  2012. 'method' => 'free_free',
  2013. 'cart_address_id' => $cartShippingAddress->id,
  2014. 'cart_id' => $cart->id,
  2015. ]);
  2016. $cartPayment = CartPayment::factory()->create([
  2017. 'cart_id' => $cart->id,
  2018. 'method' => $paymentMethod = 'cashondelivery',
  2019. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  2020. ]);
  2021. cart()->setCart($cart);
  2022. cart()->collectTotals();
  2023. // Act and Assert.
  2024. postJson(route('shop.checkout.onepage.orders.store'))
  2025. ->assertOk()
  2026. ->assertJsonPath('data.redirect', true)
  2027. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  2028. $cart->refresh();
  2029. $cartItem->refresh();
  2030. $cartPayment->refresh();
  2031. $cartBillingAddress->refresh();
  2032. $cartShippingAddress->refresh();
  2033. $this->assertModelWise([
  2034. Cart::class => [
  2035. $this->prepareCart($cart),
  2036. ],
  2037. CartItem::class => [
  2038. $this->prepareCartItem($cartItem),
  2039. ],
  2040. CartPayment::class => [
  2041. $this->prepareCartPayment($cartPayment),
  2042. ],
  2043. CartAddress::class => [
  2044. $this->prepareAddress($cartBillingAddress),
  2045. $this->prepareAddress($cartShippingAddress),
  2046. ],
  2047. CartShippingRate::class => [
  2048. $this->prepareCartShippingRate($cartShippingRate),
  2049. ],
  2050. Order::class => [
  2051. $this->prepareOrderUsingCart($cart),
  2052. ],
  2053. OrderItem::class => [
  2054. $this->prepareOrderItemUsingCartItem($cartItem),
  2055. ],
  2056. OrderPayment::class => [
  2057. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  2058. ],
  2059. ProductOrderedInventory::class => [
  2060. [
  2061. 'qty' => $additional['quantity'],
  2062. 'product_id' => $childProduct->id,
  2063. ],
  2064. ],
  2065. ProductInventoryIndex::class => [
  2066. [
  2067. 'qty' => $childProduct->inventory_source_qty(1) - $additional['quantity'],
  2068. 'product_id' => $childProduct->id,
  2069. ],
  2070. ],
  2071. ]);
  2072. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  2073. $cartShippingAddress->address_type = OrderAddress::ADDRESS_TYPE_SHIPPING;
  2074. $this->assertModelWise([
  2075. OrderAddress::class => [
  2076. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  2077. $this->prepareAddress($cartShippingAddress, OrderAddress::ADDRESS_TYPE_SHIPPING),
  2078. ],
  2079. ]);
  2080. });
  2081. it('should place a configurable product order for a guest user and send email to the guest user', function () {
  2082. // Arrange.
  2083. Mail::fake();
  2084. $product = (new ProductFaker([
  2085. 'attributes' => [
  2086. 5 => 'new',
  2087. 6 => 'featured',
  2088. 26 => 'guest_checkout',
  2089. ],
  2090. 'attribute_value' => [
  2091. 'new' => [
  2092. 'boolean_value' => true,
  2093. ],
  2094. 'featured' => [
  2095. 'boolean_value' => true,
  2096. ],
  2097. 'guest_checkout' => [
  2098. 'boolean_value' => true,
  2099. ],
  2100. ],
  2101. ]))
  2102. ->getConfigurableProductFactory()
  2103. ->create();
  2104. $childProduct = $product->variants()->first();
  2105. foreach ($product->super_attributes as $attribute) {
  2106. foreach ($attribute->options as $option) {
  2107. $superAttributes[$option->attribute_id] = $option->id;
  2108. }
  2109. }
  2110. $additional = [
  2111. 'selected_configurable_option' => $childProduct->id,
  2112. 'product_id' => $product->id,
  2113. 'is_buy_now' => '0',
  2114. 'rating' => '0',
  2115. 'quantity' => '1',
  2116. 'super_attribute' => $superAttributes ?? [],
  2117. ];
  2118. $cart = Cart::factory()->create(['shipping_method' => 'free_free']);
  2119. $cartItem = CartItem::factory()->create([
  2120. 'cart_id' => $cart->id,
  2121. 'product_id' => $childProduct->id,
  2122. 'sku' => $childProduct->sku,
  2123. 'quantity' => $additional['quantity'],
  2124. 'name' => $childProduct->name,
  2125. 'price' => $convertedPrice = core()->convertPrice($price = $childProduct->price),
  2126. 'base_price' => $price,
  2127. 'total' => $convertedPrice * $additional['quantity'],
  2128. 'base_total' => $price * $additional['quantity'],
  2129. 'weight' => $childProduct->weight ?? 0,
  2130. 'total_weight' => ($childProduct->weight ?? 0) * $additional['quantity'],
  2131. 'base_total_weight' => ($childProduct->weight ?? 0) * $additional['quantity'],
  2132. 'type' => $childProduct->type,
  2133. 'additional' => $additional,
  2134. ]);
  2135. $cartBillingAddress = CartAddress::factory()->create([
  2136. 'cart_id' => $cart->id,
  2137. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2138. ]);
  2139. $cartShippingAddress = CartAddress::factory()->create([
  2140. 'cart_id' => $cart->id,
  2141. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  2142. ]);
  2143. $cartShippingRate = CartShippingRate::factory()->create([
  2144. 'method_description' => 'Free Shipping',
  2145. 'carrier_title' => 'Free shipping',
  2146. 'method_title' => 'Free Shipping',
  2147. 'carrier' => 'free',
  2148. 'method' => 'free_free',
  2149. 'cart_address_id' => $cartShippingAddress->id,
  2150. 'cart_id' => $cart->id,
  2151. ]);
  2152. $cartPayment = CartPayment::factory()->create([
  2153. 'cart_id' => $cart->id,
  2154. 'method' => $paymentMethod = 'cashondelivery',
  2155. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  2156. ]);
  2157. cart()->setCart($cart);
  2158. cart()->collectTotals();
  2159. // Act and Assert.
  2160. postJson(route('shop.checkout.onepage.orders.store'))
  2161. ->assertOk()
  2162. ->assertJsonPath('data.redirect', true)
  2163. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  2164. $cart->refresh();
  2165. $cartItem->refresh();
  2166. $cartPayment->refresh();
  2167. $cartBillingAddress->refresh();
  2168. $cartShippingAddress->refresh();
  2169. $this->assertModelWise([
  2170. Cart::class => [
  2171. $this->prepareCart($cart),
  2172. ],
  2173. CartItem::class => [
  2174. $this->prepareCartItem($cartItem),
  2175. ],
  2176. CartPayment::class => [
  2177. $this->prepareCartPayment($cartPayment),
  2178. ],
  2179. CartAddress::class => [
  2180. $this->prepareAddress($cartBillingAddress),
  2181. $this->prepareAddress($cartShippingAddress),
  2182. ],
  2183. CartShippingRate::class => [
  2184. $this->prepareCartShippingRate($cartShippingRate),
  2185. ],
  2186. Order::class => [
  2187. $this->prepareOrderUsingCart($cart),
  2188. ],
  2189. OrderItem::class => [
  2190. $this->prepareOrderItemUsingCartItem($cartItem),
  2191. ],
  2192. OrderPayment::class => [
  2193. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  2194. ],
  2195. ProductOrderedInventory::class => [
  2196. [
  2197. 'qty' => $additional['quantity'],
  2198. 'product_id' => $childProduct->id,
  2199. ],
  2200. ],
  2201. ProductInventoryIndex::class => [
  2202. [
  2203. 'qty' => $childProduct->inventory_source_qty(1) - $additional['quantity'],
  2204. 'product_id' => $childProduct->id,
  2205. ],
  2206. ],
  2207. ]);
  2208. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  2209. $cartShippingAddress->address_type = OrderAddress::ADDRESS_TYPE_SHIPPING;
  2210. $this->assertModelWise([
  2211. OrderAddress::class => [
  2212. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  2213. $this->prepareAddress($cartShippingAddress, OrderAddress::ADDRESS_TYPE_SHIPPING),
  2214. ],
  2215. ]);
  2216. Mail::assertQueued(AdminOrderCreatedNotification::class);
  2217. Mail::assertQueued(ShopOrderCreatedNotification::class);
  2218. Mail::assertQueuedCount(2);
  2219. });
  2220. it('should place a configurable product order for a customer', function () {
  2221. // Arrange.
  2222. $product = (new ProductFaker([
  2223. 'attributes' => [
  2224. 5 => 'new',
  2225. 6 => 'featured',
  2226. ],
  2227. 'attribute_value' => [
  2228. 'new' => [
  2229. 'boolean_value' => true,
  2230. ],
  2231. 'featured' => [
  2232. 'boolean_value' => true,
  2233. ],
  2234. ],
  2235. ]))
  2236. ->getConfigurableProductFactory()
  2237. ->create();
  2238. $childProduct = $product->variants()->first();
  2239. foreach ($product->super_attributes as $attribute) {
  2240. foreach ($attribute->options as $option) {
  2241. $superAttributes[$option->attribute_id] = $option->id;
  2242. }
  2243. }
  2244. $additional = [
  2245. 'product_id' => $product->id,
  2246. 'is_buy_now' => '0',
  2247. 'rating' => '0',
  2248. 'quantity' => '1',
  2249. 'super_attribute' => $superAttributes ?? [],
  2250. 'selected_configurable_option' => $childProduct->id,
  2251. ];
  2252. $customer = Customer::factory()->create();
  2253. $cart = Cart::factory()->create([
  2254. 'customer_id' => $customer->id,
  2255. 'customer_first_name' => $customer->first_name,
  2256. 'customer_last_name' => $customer->last_name,
  2257. 'customer_email' => $customer->email,
  2258. 'is_guest' => 0,
  2259. 'shipping_method' => 'free_free',
  2260. ]);
  2261. $cartItem = CartItem::factory()->create([
  2262. 'cart_id' => $cart->id,
  2263. 'product_id' => $childProduct->id,
  2264. 'sku' => $childProduct->sku,
  2265. 'quantity' => $additional['quantity'],
  2266. 'name' => $childProduct->name,
  2267. 'price' => $convertedPrice = core()->convertPrice($price = $childProduct->price),
  2268. 'base_price' => $price,
  2269. 'total' => $convertedPrice * $additional['quantity'],
  2270. 'base_total' => $price * $additional['quantity'],
  2271. 'weight' => $childProduct->weight ?? 0,
  2272. 'total_weight' => ($childProduct->weight ?? 0) * $additional['quantity'],
  2273. 'base_total_weight' => ($childProduct->weight ?? 0) * $additional['quantity'],
  2274. 'type' => $childProduct->type,
  2275. 'additional' => $additional,
  2276. ]);
  2277. $cartBillingAddress = CartAddress::factory()->create([
  2278. 'cart_id' => $cart->id,
  2279. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2280. 'customer_id' => $customer->id,
  2281. ]);
  2282. $cartShippingAddress = CartAddress::factory()->create([
  2283. 'cart_id' => $cart->id,
  2284. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  2285. 'customer_id' => $customer->id,
  2286. ]);
  2287. $cartShippingRate = CartShippingRate::factory()->create([
  2288. 'method_description' => 'Free Shipping',
  2289. 'carrier_title' => 'Free shipping',
  2290. 'method_title' => 'Free Shipping',
  2291. 'carrier' => 'free',
  2292. 'method' => 'free_free',
  2293. 'cart_address_id' => $cartShippingAddress->id,
  2294. 'cart_id' => $cart->id,
  2295. ]);
  2296. $cartPayment = CartPayment::factory()->create([
  2297. 'cart_id' => $cart->id,
  2298. 'method' => $paymentMethod = 'cashondelivery',
  2299. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  2300. ]);
  2301. cart()->setCart($cart);
  2302. cart()->collectTotals();
  2303. // Act and Assert.
  2304. $this->loginAsCustomer($customer);
  2305. postJson(route('shop.checkout.onepage.orders.store'))
  2306. ->assertOk()
  2307. ->assertJsonPath('data.redirect', true)
  2308. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  2309. $cart->refresh();
  2310. $cartItem->refresh();
  2311. $cartPayment->refresh();
  2312. $cartBillingAddress->refresh();
  2313. $cartShippingAddress->refresh();
  2314. $this->assertModelWise([
  2315. Cart::class => [
  2316. $this->prepareCart($cart),
  2317. ],
  2318. CartItem::class => [
  2319. $this->prepareCartItem($cartItem),
  2320. ],
  2321. CartPayment::class => [
  2322. $this->prepareCartPayment($cartPayment),
  2323. ],
  2324. CartAddress::class => [
  2325. $this->prepareAddress($cartBillingAddress),
  2326. $this->prepareAddress($cartShippingAddress),
  2327. ],
  2328. CartShippingRate::class => [
  2329. $this->prepareCartShippingRate($cartShippingRate),
  2330. ],
  2331. Order::class => [
  2332. $this->prepareOrderUsingCart($cart),
  2333. ],
  2334. OrderItem::class => [
  2335. $this->prepareOrderItemUsingCartItem($cartItem),
  2336. ],
  2337. OrderPayment::class => [
  2338. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  2339. ],
  2340. ProductOrderedInventory::class => [
  2341. [
  2342. 'qty' => $additional['quantity'],
  2343. 'product_id' => $childProduct->id,
  2344. ],
  2345. ],
  2346. ProductInventoryIndex::class => [
  2347. [
  2348. 'qty' => $childProduct->inventory_source_qty(1) - $additional['quantity'],
  2349. 'product_id' => $childProduct->id,
  2350. ],
  2351. ],
  2352. ]);
  2353. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  2354. $cartShippingAddress->address_type = OrderAddress::ADDRESS_TYPE_SHIPPING;
  2355. $this->assertModelWise([
  2356. OrderAddress::class => [
  2357. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  2358. $this->prepareAddress($cartShippingAddress, OrderAddress::ADDRESS_TYPE_SHIPPING),
  2359. ],
  2360. ]);
  2361. });
  2362. it('should place a configurable product order for a customer and send email to the user', function () {
  2363. // Arrange.
  2364. Mail::fake();
  2365. $product = (new ProductFaker([
  2366. 'attributes' => [
  2367. 5 => 'new',
  2368. 6 => 'featured',
  2369. ],
  2370. 'attribute_value' => [
  2371. 'new' => [
  2372. 'boolean_value' => true,
  2373. ],
  2374. 'featured' => [
  2375. 'boolean_value' => true,
  2376. ],
  2377. ],
  2378. ]))
  2379. ->getConfigurableProductFactory()
  2380. ->create();
  2381. $childProduct = $product->variants()->first();
  2382. foreach ($product->super_attributes as $attribute) {
  2383. foreach ($attribute->options as $option) {
  2384. $superAttributes[$option->attribute_id] = $option->id;
  2385. }
  2386. }
  2387. $additional = [
  2388. 'selected_configurable_option' => $childProduct->id,
  2389. 'product_id' => $product->id,
  2390. 'is_buy_now' => '0',
  2391. 'rating' => '0',
  2392. 'quantity' => '1',
  2393. 'super_attribute' => $superAttributes ?? [],
  2394. ];
  2395. $customer = Customer::factory()->create();
  2396. $cart = Cart::factory()->create([
  2397. 'customer_id' => $customer->id,
  2398. 'customer_first_name' => $customer->first_name,
  2399. 'customer_last_name' => $customer->last_name,
  2400. 'customer_email' => $customer->email,
  2401. 'is_guest' => 0,
  2402. 'shipping_method' => 'free_free',
  2403. ]);
  2404. $cartItem = CartItem::factory()->create([
  2405. 'cart_id' => $cart->id,
  2406. 'product_id' => $childProduct->id,
  2407. 'sku' => $childProduct->sku,
  2408. 'quantity' => $additional['quantity'],
  2409. 'name' => $childProduct->name,
  2410. 'price' => $convertedPrice = core()->convertPrice($price = $childProduct->price),
  2411. 'base_price' => $price,
  2412. 'total' => $convertedPrice * $additional['quantity'],
  2413. 'base_total' => $price * $additional['quantity'],
  2414. 'weight' => $childProduct->weight ?? 0,
  2415. 'total_weight' => ($childProduct->weight ?? 0) * $additional['quantity'],
  2416. 'base_total_weight' => ($childProduct->weight ?? 0) * $additional['quantity'],
  2417. 'type' => $childProduct->type,
  2418. 'additional' => $additional,
  2419. ]);
  2420. $cartBillingAddress = CartAddress::factory()->create([
  2421. 'cart_id' => $cart->id,
  2422. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2423. 'customer_id' => $customer->id,
  2424. ]);
  2425. $cartShippingAddress = CartAddress::factory()->create([
  2426. 'cart_id' => $cart->id,
  2427. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  2428. 'customer_id' => $customer->id,
  2429. ]);
  2430. $cartShippingRate = CartShippingRate::factory()->create([
  2431. 'method_description' => 'Free Shipping',
  2432. 'carrier_title' => 'Free shipping',
  2433. 'method_title' => 'Free Shipping',
  2434. 'carrier' => 'free',
  2435. 'method' => 'free_free',
  2436. 'cart_address_id' => $cartShippingAddress->id,
  2437. 'cart_id' => $cart->id,
  2438. ]);
  2439. $cartPayment = CartPayment::factory()->create([
  2440. 'cart_id' => $cart->id,
  2441. 'method' => $paymentMethod = 'cashondelivery',
  2442. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  2443. ]);
  2444. cart()->setCart($cart);
  2445. cart()->collectTotals();
  2446. // Act and Assert.
  2447. $this->loginAsCustomer($customer);
  2448. postJson(route('shop.checkout.onepage.orders.store'))
  2449. ->assertOk()
  2450. ->assertJsonPath('data.redirect', true)
  2451. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  2452. $cart->refresh();
  2453. $cartItem->refresh();
  2454. $cartPayment->refresh();
  2455. $cartBillingAddress->refresh();
  2456. $cartShippingAddress->refresh();
  2457. $this->assertModelWise([
  2458. Cart::class => [
  2459. $this->prepareCart($cart),
  2460. ],
  2461. CartItem::class => [
  2462. $this->prepareCartItem($cartItem),
  2463. ],
  2464. CartPayment::class => [
  2465. $this->prepareCartPayment($cartPayment),
  2466. ],
  2467. CartAddress::class => [
  2468. $this->prepareAddress($cartBillingAddress),
  2469. $this->prepareAddress($cartShippingAddress),
  2470. ],
  2471. CartShippingRate::class => [
  2472. $this->prepareCartShippingRate($cartShippingRate),
  2473. ],
  2474. Order::class => [
  2475. $this->prepareOrderUsingCart($cart),
  2476. ],
  2477. OrderItem::class => [
  2478. $this->prepareOrderItemUsingCartItem($cartItem),
  2479. ],
  2480. OrderPayment::class => [
  2481. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  2482. ],
  2483. ProductOrderedInventory::class => [
  2484. [
  2485. 'qty' => $additional['quantity'],
  2486. 'product_id' => $childProduct->id,
  2487. ],
  2488. ],
  2489. ProductInventoryIndex::class => [
  2490. [
  2491. 'qty' => $childProduct->inventory_source_qty(1) - $additional['quantity'],
  2492. 'product_id' => $childProduct->id,
  2493. ],
  2494. ],
  2495. ]);
  2496. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  2497. $cartShippingAddress->address_type = OrderAddress::ADDRESS_TYPE_SHIPPING;
  2498. $this->assertModelWise([
  2499. OrderAddress::class => [
  2500. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  2501. $this->prepareAddress($cartShippingAddress, OrderAddress::ADDRESS_TYPE_SHIPPING),
  2502. ],
  2503. ]);
  2504. Mail::assertQueued(AdminOrderCreatedNotification::class);
  2505. Mail::assertQueued(ShopOrderCreatedNotification::class);
  2506. Mail::assertQueuedCount(2);
  2507. });
  2508. it('should place a virtual product order for a guest user', function () {
  2509. // Arrange.
  2510. $product = (new ProductFaker([
  2511. 'attributes' => [
  2512. 5 => 'new',
  2513. 6 => 'featured',
  2514. 26 => 'guest_checkout',
  2515. ],
  2516. 'attribute_value' => [
  2517. 'new' => [
  2518. 'boolean_value' => true,
  2519. ],
  2520. 'featured' => [
  2521. 'boolean_value' => true,
  2522. ],
  2523. 'guest_checkout' => [
  2524. 'boolean_value' => true,
  2525. ],
  2526. ],
  2527. ]))
  2528. ->getVirtualProductFactory()
  2529. ->create();
  2530. $cart = Cart::factory()->create();
  2531. $additional = [
  2532. 'product_id' => $product->id,
  2533. 'rating' => '0',
  2534. 'is_buy_now' => '0',
  2535. 'quantity' => '1',
  2536. ];
  2537. $cartItem = CartItem::factory()->create([
  2538. 'cart_id' => $cart->id,
  2539. 'product_id' => $product->id,
  2540. 'sku' => $product->sku,
  2541. 'quantity' => $additional['quantity'],
  2542. 'name' => $product->name,
  2543. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  2544. 'price_incl_tax' => $convertedPrice,
  2545. 'base_price' => $price,
  2546. 'base_price_incl_tax' => $price,
  2547. 'total' => $total = $convertedPrice * $additional['quantity'],
  2548. 'total_incl_tax' => $total,
  2549. 'base_total' => $price * $additional['quantity'],
  2550. 'weight' => $product->weight ?? 0,
  2551. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  2552. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  2553. 'type' => $product->type,
  2554. 'additional' => $additional,
  2555. ]);
  2556. $cartBillingAddress = CartAddress::factory()->create([
  2557. 'cart_id' => $cart->id,
  2558. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2559. ]);
  2560. $cartPayment = CartPayment::factory()->create([
  2561. 'cart_id' => $cart->id,
  2562. 'method' => $paymentMethod = 'cashondelivery',
  2563. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  2564. ]);
  2565. cart()->setCart($cart);
  2566. cart()->collectTotals();
  2567. // Act and Assert.
  2568. postJson(route('shop.checkout.onepage.orders.store'))
  2569. ->assertOk()
  2570. ->assertJsonPath('data.redirect', true)
  2571. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  2572. $cart->refresh();
  2573. $cartItem->refresh();
  2574. $cartPayment->refresh();
  2575. $cartBillingAddress->refresh();
  2576. $this->assertModelWise([
  2577. Cart::class => [
  2578. $this->prepareCart($cart),
  2579. ],
  2580. CartItem::class => [
  2581. $this->prepareCartItem($cartItem),
  2582. ],
  2583. CartPayment::class => [
  2584. $this->prepareCartPayment($cartPayment),
  2585. ],
  2586. CartAddress::class => [
  2587. $this->prepareAddress($cartBillingAddress),
  2588. ],
  2589. Order::class => [
  2590. $this->prepareOrderUsingCart($cart),
  2591. ],
  2592. OrderItem::class => [
  2593. $this->prepareOrderItemUsingCartItem($cartItem),
  2594. ],
  2595. OrderPayment::class => [
  2596. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  2597. ],
  2598. ProductOrderedInventory::class => [
  2599. [
  2600. 'qty' => $cartItem->quantity,
  2601. 'product_id' => $product->id,
  2602. ],
  2603. ],
  2604. ProductInventoryIndex::class => [
  2605. [
  2606. 'qty' => $product->inventory_source_qty(1) - $cartItem->quantity,
  2607. 'product_id' => $product->id,
  2608. ],
  2609. ],
  2610. ]);
  2611. $this->assertModelWise([
  2612. OrderAddress::class => [
  2613. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  2614. ],
  2615. ]);
  2616. });
  2617. it('should place a virtual product order for a guest user and send email to the guest user', function () {
  2618. // Arrange.
  2619. Mail::fake();
  2620. $product = (new ProductFaker([
  2621. 'attributes' => [
  2622. 5 => 'new',
  2623. 6 => 'featured',
  2624. 26 => 'guest_checkout',
  2625. ],
  2626. 'attribute_value' => [
  2627. 'new' => [
  2628. 'boolean_value' => true,
  2629. ],
  2630. 'featured' => [
  2631. 'boolean_value' => true,
  2632. ],
  2633. 'guest_checkout' => [
  2634. 'boolean_value' => true,
  2635. ],
  2636. ],
  2637. ]))
  2638. ->getVirtualProductFactory()
  2639. ->create();
  2640. $cart = Cart::factory()->create();
  2641. $additional = [
  2642. 'product_id' => $product->id,
  2643. 'rating' => '0',
  2644. 'is_buy_now' => '0',
  2645. 'quantity' => '1',
  2646. ];
  2647. $cartItem = CartItem::factory()->create([
  2648. 'cart_id' => $cart->id,
  2649. 'product_id' => $product->id,
  2650. 'sku' => $product->sku,
  2651. 'quantity' => $additional['quantity'],
  2652. 'name' => $product->name,
  2653. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  2654. 'price_incl_tax' => $convertedPrice,
  2655. 'base_price' => $price,
  2656. 'base_price_incl_tax' => $price,
  2657. 'total' => $total = $convertedPrice * $additional['quantity'],
  2658. 'total_incl_tax' => $total,
  2659. 'base_total' => $price * $additional['quantity'],
  2660. 'weight' => $product->weight ?? 0,
  2661. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  2662. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  2663. 'type' => $product->type,
  2664. 'additional' => $additional,
  2665. ]);
  2666. $cartBillingAddress = CartAddress::factory()->create([
  2667. 'cart_id' => $cart->id,
  2668. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2669. ]);
  2670. $cartPayment = CartPayment::factory()->create([
  2671. 'cart_id' => $cart->id,
  2672. 'method' => $paymentMethod = 'cashondelivery',
  2673. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  2674. ]);
  2675. cart()->setCart($cart);
  2676. cart()->collectTotals();
  2677. // Act and Assert.
  2678. postJson(route('shop.checkout.onepage.orders.store'))
  2679. ->assertOk()
  2680. ->assertJsonPath('data.redirect', true)
  2681. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  2682. $cart->refresh();
  2683. $cartItem->refresh();
  2684. $cartPayment->refresh();
  2685. $cartBillingAddress->refresh();
  2686. $this->assertModelWise([
  2687. Cart::class => [
  2688. $this->prepareCart($cart),
  2689. ],
  2690. CartItem::class => [
  2691. $this->prepareCartItem($cartItem),
  2692. ],
  2693. CartPayment::class => [
  2694. $this->prepareCartPayment($cartPayment),
  2695. ],
  2696. CartAddress::class => [
  2697. $this->prepareAddress($cartBillingAddress),
  2698. ],
  2699. Order::class => [
  2700. $this->prepareOrderUsingCart($cart),
  2701. ],
  2702. OrderItem::class => [
  2703. $this->prepareOrderItemUsingCartItem($cartItem),
  2704. ],
  2705. OrderPayment::class => [
  2706. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  2707. ],
  2708. ProductOrderedInventory::class => [
  2709. [
  2710. 'qty' => $cartItem->quantity,
  2711. 'product_id' => $product->id,
  2712. ],
  2713. ],
  2714. ProductInventoryIndex::class => [
  2715. [
  2716. 'qty' => $product->inventory_source_qty(1) - $cartItem->quantity,
  2717. 'product_id' => $product->id,
  2718. ],
  2719. ],
  2720. ]);
  2721. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  2722. $this->assertModelWise([
  2723. OrderAddress::class => [
  2724. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  2725. ],
  2726. ]);
  2727. Mail::assertQueued(AdminOrderCreatedNotification::class);
  2728. Mail::assertQueued(ShopOrderCreatedNotification::class);
  2729. Mail::assertQueuedCount(2);
  2730. });
  2731. it('should place a virtual product order for a customer', function () {
  2732. // Arrange.
  2733. $product = (new ProductFaker([
  2734. 'attributes' => [
  2735. 5 => 'new',
  2736. 6 => 'featured',
  2737. ],
  2738. 'attribute_value' => [
  2739. 'new' => [
  2740. 'boolean_value' => true,
  2741. ],
  2742. 'featured' => [
  2743. 'boolean_value' => true,
  2744. ],
  2745. ],
  2746. ]))
  2747. ->getVirtualProductFactory()
  2748. ->create();
  2749. $customer = Customer::factory()->create();
  2750. $cart = Cart::factory()->create([
  2751. 'customer_id' => $customer->id,
  2752. 'customer_first_name' => $customer->first_name,
  2753. 'customer_last_name' => $customer->last_name,
  2754. 'customer_email' => $customer->email,
  2755. 'is_guest' => 0,
  2756. ]);
  2757. $additional = [
  2758. 'product_id' => $product->id,
  2759. 'rating' => '0',
  2760. 'is_buy_now' => '0',
  2761. 'quantity' => '1',
  2762. ];
  2763. $cartItem = CartItem::factory()->create([
  2764. 'cart_id' => $cart->id,
  2765. 'product_id' => $product->id,
  2766. 'sku' => $product->sku,
  2767. 'quantity' => $additional['quantity'],
  2768. 'name' => $product->name,
  2769. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  2770. 'price_incl_tax' => $convertedPrice,
  2771. 'base_price' => $price,
  2772. 'base_price_incl_tax' => $price,
  2773. 'total' => $total = $convertedPrice * $additional['quantity'],
  2774. 'total_incl_tax' => $total,
  2775. 'base_total' => $price * $additional['quantity'],
  2776. 'weight' => $product->weight ?? 0,
  2777. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  2778. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  2779. 'type' => $product->type,
  2780. 'additional' => $additional,
  2781. ]);
  2782. $cartBillingAddress = CartAddress::factory()->create([
  2783. 'cart_id' => $cart->id,
  2784. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2785. 'customer_id' => $cart->customer_id,
  2786. ]);
  2787. $cartPayment = CartPayment::factory()->create([
  2788. 'cart_id' => $cart->id,
  2789. 'method' => $paymentMethod = 'cashondelivery',
  2790. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  2791. ]);
  2792. cart()->setCart($cart);
  2793. cart()->collectTotals();
  2794. // Act and Assert.
  2795. $this->loginAsCustomer($customer);
  2796. postJson(route('shop.checkout.onepage.orders.store'))
  2797. ->assertOk()
  2798. ->assertJsonPath('data.redirect', true)
  2799. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  2800. $cart->refresh();
  2801. $cartItem->refresh();
  2802. $cartPayment->refresh();
  2803. $cartBillingAddress->refresh();
  2804. $this->assertModelWise([
  2805. Cart::class => [
  2806. $this->prepareCart($cart),
  2807. ],
  2808. CartItem::class => [
  2809. $this->prepareCartItem($cartItem),
  2810. ],
  2811. CartPayment::class => [
  2812. $this->prepareCartPayment($cartPayment),
  2813. ],
  2814. CartAddress::class => [
  2815. $this->prepareAddress($cartBillingAddress),
  2816. ],
  2817. Order::class => [
  2818. $this->prepareOrderUsingCart($cart),
  2819. ],
  2820. OrderItem::class => [
  2821. $this->prepareOrderItemUsingCartItem($cartItem),
  2822. ],
  2823. OrderPayment::class => [
  2824. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  2825. ],
  2826. ProductOrderedInventory::class => [
  2827. [
  2828. 'qty' => $cartItem->quantity,
  2829. 'product_id' => $product->id,
  2830. ],
  2831. ],
  2832. ProductInventoryIndex::class => [
  2833. [
  2834. 'qty' => $product->inventory_source_qty(1) - $cartItem->quantity,
  2835. 'product_id' => $product->id,
  2836. ],
  2837. ],
  2838. ]);
  2839. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  2840. $this->assertModelWise([
  2841. OrderAddress::class => [
  2842. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  2843. ],
  2844. ]);
  2845. });
  2846. it('should place a virtual product order for a customer and send email to the user', function () {
  2847. // Arrange.
  2848. Mail::fake();
  2849. $product = (new ProductFaker([
  2850. 'attributes' => [
  2851. 5 => 'new',
  2852. 6 => 'featured',
  2853. ],
  2854. 'attribute_value' => [
  2855. 'new' => [
  2856. 'boolean_value' => true,
  2857. ],
  2858. 'featured' => [
  2859. 'boolean_value' => true,
  2860. ],
  2861. ],
  2862. ]))
  2863. ->getVirtualProductFactory()
  2864. ->create();
  2865. $customer = Customer::factory()->create();
  2866. $cart = Cart::factory()->create([
  2867. 'customer_id' => $customer->id,
  2868. 'customer_first_name' => $customer->first_name,
  2869. 'customer_last_name' => $customer->last_name,
  2870. 'customer_email' => $customer->email,
  2871. 'is_guest' => 0,
  2872. ]);
  2873. $additional = [
  2874. 'product_id' => $product->id,
  2875. 'rating' => '0',
  2876. 'is_buy_now' => '0',
  2877. 'quantity' => '1',
  2878. ];
  2879. $cartItem = CartItem::factory()->create([
  2880. 'cart_id' => $cart->id,
  2881. 'product_id' => $product->id,
  2882. 'sku' => $product->sku,
  2883. 'quantity' => $additional['quantity'],
  2884. 'name' => $product->name,
  2885. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  2886. 'price_incl_tax' => $convertedPrice,
  2887. 'base_price' => $price,
  2888. 'base_price_incl_tax' => $price,
  2889. 'total' => $total = $convertedPrice * $additional['quantity'],
  2890. 'total_incl_tax' => $total,
  2891. 'base_total' => $price * $additional['quantity'],
  2892. 'weight' => $product->weight ?? 0,
  2893. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  2894. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  2895. 'type' => $product->type,
  2896. 'additional' => $additional,
  2897. ]);
  2898. $cartBillingAddress = CartAddress::factory()->create([
  2899. 'cart_id' => $cart->id,
  2900. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2901. 'customer_id' => $cart->customer_id,
  2902. ]);
  2903. $cartPayment = CartPayment::factory()->create([
  2904. 'cart_id' => $cart->id,
  2905. 'method' => $paymentMethod = 'cashondelivery',
  2906. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  2907. ]);
  2908. cart()->setCart($cart);
  2909. cart()->collectTotals();
  2910. // Act and Assert.
  2911. $this->loginAsCustomer($customer);
  2912. postJson(route('shop.checkout.onepage.orders.store'))
  2913. ->assertOk()
  2914. ->assertJsonPath('data.redirect', true)
  2915. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  2916. $cart->refresh();
  2917. $cartItem->refresh();
  2918. $cartPayment->refresh();
  2919. $cartBillingAddress->refresh();
  2920. $this->assertModelWise([
  2921. Cart::class => [
  2922. $this->prepareCart($cart),
  2923. ],
  2924. CartItem::class => [
  2925. $this->prepareCartItem($cartItem),
  2926. ],
  2927. CartPayment::class => [
  2928. $this->prepareCartPayment($cartPayment),
  2929. ],
  2930. CartAddress::class => [
  2931. $this->prepareAddress($cartBillingAddress),
  2932. ],
  2933. Order::class => [
  2934. $this->prepareOrderUsingCart($cart),
  2935. ],
  2936. OrderItem::class => [
  2937. $this->prepareOrderItemUsingCartItem($cartItem),
  2938. ],
  2939. OrderPayment::class => [
  2940. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  2941. ],
  2942. ProductOrderedInventory::class => [
  2943. [
  2944. 'qty' => $cartItem->quantity,
  2945. 'product_id' => $product->id,
  2946. ],
  2947. ],
  2948. ProductInventoryIndex::class => [
  2949. [
  2950. 'qty' => $product->inventory_source_qty(1) - $cartItem->quantity,
  2951. 'product_id' => $product->id,
  2952. ],
  2953. ],
  2954. ]);
  2955. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  2956. $this->assertModelWise([
  2957. OrderAddress::class => [
  2958. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  2959. ],
  2960. ]);
  2961. Mail::assertQueued(AdminOrderCreatedNotification::class);
  2962. Mail::assertQueued(ShopOrderCreatedNotification::class);
  2963. Mail::assertQueuedCount(2);
  2964. });
  2965. it('should place a downloadable product order for a customer', function () {
  2966. // Arrange.
  2967. $product = (new ProductFaker([
  2968. 'attributes' => [
  2969. 5 => 'new',
  2970. 6 => 'featured',
  2971. ],
  2972. 'attribute_value' => [
  2973. 'new' => [
  2974. 'boolean_value' => true,
  2975. ],
  2976. 'featured' => [
  2977. 'boolean_value' => true,
  2978. ],
  2979. ],
  2980. ]))
  2981. ->getDownloadableProductFactory()
  2982. ->create();
  2983. $customer = Customer::factory()->create();
  2984. $cart = Cart::factory()->create([
  2985. 'customer_id' => $customer->id,
  2986. 'customer_first_name' => $customer->first_name,
  2987. 'customer_last_name' => $customer->last_name,
  2988. 'customer_email' => $customer->email,
  2989. 'is_guest' => 0,
  2990. ]);
  2991. $additional = [
  2992. 'product_id' => $product->id,
  2993. 'is_buy_now' => '0',
  2994. 'rating' => '0',
  2995. 'quantity' => '1',
  2996. 'links' => [
  2997. '1',
  2998. ],
  2999. ];
  3000. $cartItem = CartItem::factory()->create([
  3001. 'cart_id' => $cart->id,
  3002. 'product_id' => $product->id,
  3003. 'sku' => $product->sku,
  3004. 'quantity' => $additional['quantity'],
  3005. 'name' => $product->name,
  3006. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  3007. 'price_incl_tax' => $convertedPrice,
  3008. 'base_price' => $price,
  3009. 'base_price_incl_tax' => $price,
  3010. 'total' => $total = $convertedPrice * $additional['quantity'],
  3011. 'total_incl_tax' => $total,
  3012. 'base_total' => $price * $additional['quantity'],
  3013. 'weight' => $product->weight ?? 0,
  3014. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3015. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3016. 'type' => $product->type,
  3017. 'additional' => $additional,
  3018. ]);
  3019. $cartBillingAddress = CartAddress::factory()->create([
  3020. 'cart_id' => $cart->id,
  3021. 'customer_id' => $customer->id,
  3022. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  3023. ]);
  3024. $cartPayment = CartPayment::factory()->create([
  3025. 'cart_id' => $cart->id,
  3026. 'method' => $paymentMethod = 'cashondelivery',
  3027. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  3028. ]);
  3029. cart()->setCart($cart);
  3030. cart()->collectTotals();
  3031. // Act and Assert.
  3032. $this->loginAsCustomer($customer);
  3033. postJson(route('shop.checkout.onepage.orders.store'))
  3034. ->assertOk()
  3035. ->assertJsonPath('data.redirect', true)
  3036. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  3037. $cart->refresh();
  3038. $cartItem->refresh();
  3039. $cartPayment->refresh();
  3040. $cartBillingAddress->refresh();
  3041. $this->assertModelWise([
  3042. Cart::class => [
  3043. $this->prepareCart($cart),
  3044. ],
  3045. CartItem::class => [
  3046. $this->prepareCartItem($cartItem),
  3047. ],
  3048. CartPayment::class => [
  3049. $this->prepareCartPayment($cartPayment),
  3050. ],
  3051. CartAddress::class => [
  3052. $this->prepareAddress($cartBillingAddress),
  3053. ],
  3054. Order::class => [
  3055. $this->prepareOrderUsingCart($cart),
  3056. ],
  3057. OrderItem::class => [
  3058. $this->prepareOrderItemUsingCartItem($cartItem),
  3059. ],
  3060. OrderPayment::class => [
  3061. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  3062. ],
  3063. ProductInventoryIndex::class => [
  3064. [
  3065. 'qty' => 0,
  3066. 'product_id' => $product->id,
  3067. ],
  3068. ],
  3069. ]);
  3070. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  3071. $this->assertModelWise([
  3072. OrderAddress::class => [
  3073. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  3074. ],
  3075. ]);
  3076. });
  3077. it('should place a downloadable product order for a customer and send email to the user', function () {
  3078. // Arrange.
  3079. Mail::fake();
  3080. $product = (new ProductFaker([
  3081. 'attributes' => [
  3082. 5 => 'new',
  3083. 6 => 'featured',
  3084. ],
  3085. 'attribute_value' => [
  3086. 'new' => [
  3087. 'boolean_value' => true,
  3088. ],
  3089. 'featured' => [
  3090. 'boolean_value' => true,
  3091. ],
  3092. ],
  3093. ]))
  3094. ->getDownloadableProductFactory()
  3095. ->create();
  3096. $customer = Customer::factory()->create();
  3097. $cart = Cart::factory()->create([
  3098. 'customer_id' => $customer->id,
  3099. 'customer_first_name' => $customer->first_name,
  3100. 'customer_last_name' => $customer->last_name,
  3101. 'customer_email' => $customer->email,
  3102. 'is_guest' => 0,
  3103. ]);
  3104. $additional = [
  3105. 'product_id' => $product->id,
  3106. 'is_buy_now' => '0',
  3107. 'rating' => '0',
  3108. 'quantity' => '1',
  3109. 'links' => [
  3110. '1',
  3111. ],
  3112. ];
  3113. $cartItem = CartItem::factory()->create([
  3114. 'cart_id' => $cart->id,
  3115. 'product_id' => $product->id,
  3116. 'sku' => $product->sku,
  3117. 'quantity' => $additional['quantity'],
  3118. 'name' => $product->name,
  3119. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  3120. 'price_incl_tax' => $convertedPrice,
  3121. 'base_price' => $price,
  3122. 'base_price_incl_tax' => $price,
  3123. 'total' => $total = $convertedPrice * $additional['quantity'],
  3124. 'total_incl_tax' => $total,
  3125. 'base_total' => $price * $additional['quantity'],
  3126. 'weight' => $product->weight ?? 0,
  3127. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3128. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3129. 'type' => $product->type,
  3130. 'additional' => $additional,
  3131. ]);
  3132. $cartBillingAddress = CartAddress::factory()->create([
  3133. 'cart_id' => $cart->id,
  3134. 'customer_id' => $customer->id,
  3135. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  3136. ]);
  3137. $cartPayment = CartPayment::factory()->create([
  3138. 'cart_id' => $cart->id,
  3139. 'method' => $paymentMethod = 'cashondelivery',
  3140. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  3141. ]);
  3142. cart()->setCart($cart);
  3143. cart()->collectTotals();
  3144. // Act and Assert.
  3145. $this->loginAsCustomer($customer);
  3146. postJson(route('shop.checkout.onepage.orders.store'))
  3147. ->assertOk()
  3148. ->assertJsonPath('data.redirect', true)
  3149. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  3150. $cart->refresh();
  3151. $cartItem->refresh();
  3152. $cartPayment->refresh();
  3153. $cartBillingAddress->refresh();
  3154. $this->assertModelWise([
  3155. Cart::class => [
  3156. $this->prepareCart($cart),
  3157. ],
  3158. CartItem::class => [
  3159. $this->prepareCartItem($cartItem),
  3160. ],
  3161. CartPayment::class => [
  3162. $this->prepareCartPayment($cartPayment),
  3163. ],
  3164. CartAddress::class => [
  3165. $this->prepareAddress($cartBillingAddress),
  3166. ],
  3167. Order::class => [
  3168. $this->prepareOrderUsingCart($cart),
  3169. ],
  3170. OrderItem::class => [
  3171. $this->prepareOrderItemUsingCartItem($cartItem),
  3172. ],
  3173. OrderPayment::class => [
  3174. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  3175. ],
  3176. ProductInventoryIndex::class => [
  3177. [
  3178. 'qty' => 0,
  3179. 'product_id' => $product->id,
  3180. ],
  3181. ],
  3182. ]);
  3183. $cartBillingAddress->address_type = OrderAddress::ADDRESS_TYPE_BILLING;
  3184. $this->assertModelWise([
  3185. OrderAddress::class => [
  3186. $this->prepareAddress($cartBillingAddress, OrderAddress::ADDRESS_TYPE_BILLING),
  3187. ],
  3188. ]);
  3189. Mail::assertQueued(AdminOrderCreatedNotification::class);
  3190. Mail::assertQueued(ShopOrderCreatedNotification::class);
  3191. Mail::assertQueuedCount(2);
  3192. });
  3193. it('should not return the cash on delivery payment method if product is downloadable', function () {
  3194. // Arrange.
  3195. $product = (new ProductFaker([
  3196. 'attributes' => [
  3197. 5 => 'new',
  3198. 6 => 'featured',
  3199. ],
  3200. 'attribute_value' => [
  3201. 'new' => [
  3202. 'boolean_value' => true,
  3203. ],
  3204. 'featured' => [
  3205. 'boolean_value' => true,
  3206. ],
  3207. ],
  3208. ]))
  3209. ->getDownloadableProductFactory()
  3210. ->create();
  3211. $customer = Customer::factory()->create();
  3212. $cart = Cart::factory()->create([
  3213. 'customer_id' => $customer->id,
  3214. 'customer_first_name' => $customer->first_name,
  3215. 'customer_last_name' => $customer->last_name,
  3216. 'customer_email' => $customer->email,
  3217. 'is_guest' => 0,
  3218. ]);
  3219. $additional = [
  3220. 'product_id' => $product->id,
  3221. 'is_buy_now' => '0',
  3222. 'rating' => '0',
  3223. 'quantity' => '1',
  3224. 'links' => [
  3225. '1',
  3226. ],
  3227. ];
  3228. CartItem::factory()->create([
  3229. 'cart_id' => $cart->id,
  3230. 'product_id' => $product->id,
  3231. 'sku' => $product->sku,
  3232. 'quantity' => $additional['quantity'],
  3233. 'name' => $product->name,
  3234. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  3235. 'price_incl_tax' => $convertedPrice,
  3236. 'base_price' => $price,
  3237. 'base_price_incl_tax' => $price,
  3238. 'total' => $total = $convertedPrice * $additional['quantity'],
  3239. 'total_incl_tax' => $total,
  3240. 'base_total' => $price * $additional['quantity'],
  3241. 'weight' => $product->weight ?? 0,
  3242. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3243. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3244. 'type' => $product->type,
  3245. 'additional' => $additional,
  3246. ]);
  3247. CartAddress::factory()->create([
  3248. 'cart_id' => $cart->id,
  3249. 'customer_id' => $customer->id,
  3250. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  3251. ]);
  3252. $customerAddress = CustomerAddress::factory()->create()->toArray();
  3253. CartPayment::factory()->create([
  3254. 'cart_id' => $cart->id,
  3255. 'method' => $paymentMethod = 'cashondelivery',
  3256. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  3257. ]);
  3258. $customerAddress = CustomerAddress::factory()->create()->toArray();
  3259. cart()->setCart($cart);
  3260. cart()->collectTotals();
  3261. // Act and Assert.
  3262. $this->loginAsCustomer($customer);
  3263. postJson(route('shop.checkout.onepage.addresses.store'), [
  3264. 'billing' => [
  3265. ...$customerAddress,
  3266. 'address' => [fake()->address()],
  3267. 'use_for_shipping' => false,
  3268. ],
  3269. 'shipping' => [
  3270. ...$customerAddress,
  3271. 'address' => [fake()->address()],
  3272. ],
  3273. ])
  3274. ->assertOk()
  3275. ->assertJsonPath('redirect', false)
  3276. ->assertJsonCount(3, 'data.payment_methods')
  3277. ->assertDontSeeText('cashondelivery');
  3278. });
  3279. it('should not return the shipping methods if product is downloadable', function () {
  3280. // Arrange.
  3281. $product = (new ProductFaker([
  3282. 'attributes' => [
  3283. 5 => 'new',
  3284. 6 => 'featured',
  3285. ],
  3286. 'attribute_value' => [
  3287. 'new' => [
  3288. 'boolean_value' => true,
  3289. ],
  3290. 'featured' => [
  3291. 'boolean_value' => true,
  3292. ],
  3293. ],
  3294. ]))
  3295. ->getDownloadableProductFactory()
  3296. ->create();
  3297. $customer = Customer::factory()->create();
  3298. $cart = Cart::factory()->create([
  3299. 'customer_id' => $customer->id,
  3300. 'customer_first_name' => $customer->first_name,
  3301. 'customer_last_name' => $customer->last_name,
  3302. 'customer_email' => $customer->email,
  3303. 'is_guest' => 0,
  3304. ]);
  3305. $additional = [
  3306. 'product_id' => $product->id,
  3307. 'is_buy_now' => '0',
  3308. 'rating' => '0',
  3309. 'quantity' => '1',
  3310. 'links' => [
  3311. '1',
  3312. ],
  3313. ];
  3314. CartItem::factory()->create([
  3315. 'cart_id' => $cart->id,
  3316. 'product_id' => $product->id,
  3317. 'sku' => $product->sku,
  3318. 'quantity' => $additional['quantity'],
  3319. 'name' => $product->name,
  3320. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  3321. 'price_incl_tax' => $convertedPrice,
  3322. 'base_price' => $price,
  3323. 'base_price_incl_tax' => $price,
  3324. 'total' => $total = $convertedPrice * $additional['quantity'],
  3325. 'total_incl_tax' => $total,
  3326. 'base_total' => $price * $additional['quantity'],
  3327. 'weight' => $product->weight ?? 0,
  3328. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3329. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3330. 'type' => $product->type,
  3331. 'additional' => $additional,
  3332. ]);
  3333. CartAddress::factory()->create([
  3334. 'cart_id' => $cart->id,
  3335. 'customer_id' => $customer->id,
  3336. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  3337. ]);
  3338. $customerAddress = CustomerAddress::factory()->create()->toArray();
  3339. CartPayment::factory()->create([
  3340. 'cart_id' => $cart->id,
  3341. 'method' => $paymentMethod = 'cashondelivery',
  3342. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  3343. ]);
  3344. $customerAddress = CustomerAddress::factory()->create()->toArray();
  3345. cart()->setCart($cart);
  3346. cart()->collectTotals();
  3347. // Act and Assert.
  3348. $this->loginAsCustomer($customer);
  3349. postJson(route('shop.checkout.onepage.addresses.store'), [
  3350. 'billing' => [
  3351. ...$customerAddress,
  3352. 'address' => [fake()->address()],
  3353. 'use_for_shipping' => false,
  3354. ],
  3355. 'shipping' => [
  3356. ...$customerAddress,
  3357. 'address' => [fake()->address()],
  3358. ],
  3359. ])
  3360. ->assertOk()
  3361. ->assertJsonPath('redirect', false)
  3362. ->assertJsonMissingPath('data.shippingMethods');
  3363. });
  3364. it('should not return the cash on delivery payment method if product is virtual', function () {
  3365. // Arrange.
  3366. $product = (new ProductFaker([
  3367. 'attributes' => [
  3368. 5 => 'new',
  3369. 6 => 'featured',
  3370. ],
  3371. 'attribute_value' => [
  3372. 'new' => [
  3373. 'boolean_value' => true,
  3374. ],
  3375. 'featured' => [
  3376. 'boolean_value' => true,
  3377. ],
  3378. ],
  3379. ]))
  3380. ->getVirtualProductFactory()
  3381. ->create();
  3382. $customer = Customer::factory()->create();
  3383. $cart = Cart::factory()->create([
  3384. 'customer_id' => $customer->id,
  3385. 'customer_first_name' => $customer->first_name,
  3386. 'customer_last_name' => $customer->last_name,
  3387. 'customer_email' => $customer->email,
  3388. 'is_guest' => 0,
  3389. ]);
  3390. $additional = [
  3391. 'product_id' => $product->id,
  3392. 'is_buy_now' => '0',
  3393. 'rating' => '0',
  3394. 'quantity' => '1',
  3395. 'links' => [
  3396. '1',
  3397. ],
  3398. ];
  3399. CartItem::factory()->create([
  3400. 'cart_id' => $cart->id,
  3401. 'product_id' => $product->id,
  3402. 'sku' => $product->sku,
  3403. 'quantity' => $additional['quantity'],
  3404. 'name' => $product->name,
  3405. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  3406. 'price_incl_tax' => $convertedPrice,
  3407. 'base_price' => $price,
  3408. 'base_price_incl_tax' => $price,
  3409. 'total' => $total = $convertedPrice * $additional['quantity'],
  3410. 'total_incl_tax' => $total,
  3411. 'base_total' => $price * $additional['quantity'],
  3412. 'weight' => $product->weight ?? 0,
  3413. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3414. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3415. 'type' => $product->type,
  3416. 'additional' => $additional,
  3417. ]);
  3418. CartAddress::factory()->create([
  3419. 'cart_id' => $cart->id,
  3420. 'customer_id' => $customer->id,
  3421. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  3422. ]);
  3423. $customerAddress = CustomerAddress::factory()->create()->toArray();
  3424. CartPayment::factory()->create([
  3425. 'cart_id' => $cart->id,
  3426. 'method' => $paymentMethod = 'cashondelivery',
  3427. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  3428. ]);
  3429. $customerAddress = CustomerAddress::factory()->create()->toArray();
  3430. cart()->setCart($cart);
  3431. cart()->collectTotals();
  3432. // Act and Assert.
  3433. $this->loginAsCustomer($customer);
  3434. postJson(route('shop.checkout.onepage.addresses.store'), [
  3435. 'billing' => [
  3436. ...$customerAddress,
  3437. 'address' => [fake()->address()],
  3438. 'use_for_shipping' => 0,
  3439. ],
  3440. 'shipping' => [
  3441. ...$customerAddress,
  3442. 'address' => [fake()->address()],
  3443. ],
  3444. ])
  3445. ->assertOk()
  3446. ->assertJsonPath('redirect', false)
  3447. ->assertJsonCount(3, 'data.payment_methods')
  3448. ->assertDontSeeText('cashondelivery');
  3449. });
  3450. it('should not return the shipping methods if product is virtual', function () {
  3451. // Arrange.
  3452. $product = (new ProductFaker([
  3453. 'attributes' => [
  3454. 5 => 'new',
  3455. 6 => 'featured',
  3456. ],
  3457. 'attribute_value' => [
  3458. 'new' => [
  3459. 'boolean_value' => true,
  3460. ],
  3461. 'featured' => [
  3462. 'boolean_value' => true,
  3463. ],
  3464. ],
  3465. ]))
  3466. ->getVirtualProductFactory()
  3467. ->create();
  3468. $customer = Customer::factory()->create();
  3469. $cart = Cart::factory()->create([
  3470. 'customer_id' => $customer->id,
  3471. 'customer_first_name' => $customer->first_name,
  3472. 'customer_last_name' => $customer->last_name,
  3473. 'customer_email' => $customer->email,
  3474. 'is_guest' => 0,
  3475. ]);
  3476. $additional = [
  3477. 'product_id' => $product->id,
  3478. 'is_buy_now' => '0',
  3479. 'rating' => '0',
  3480. 'quantity' => '1',
  3481. 'links' => [
  3482. '1',
  3483. ],
  3484. ];
  3485. CartItem::factory()->create([
  3486. 'cart_id' => $cart->id,
  3487. 'product_id' => $product->id,
  3488. 'sku' => $product->sku,
  3489. 'quantity' => $additional['quantity'],
  3490. 'name' => $product->name,
  3491. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  3492. 'price_incl_tax' => $convertedPrice,
  3493. 'base_price' => $price,
  3494. 'base_price_incl_tax' => $price,
  3495. 'total' => $total = $convertedPrice * $additional['quantity'],
  3496. 'total_incl_tax' => $total,
  3497. 'base_total' => $price * $additional['quantity'],
  3498. 'weight' => $product->weight ?? 0,
  3499. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3500. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  3501. 'type' => $product->type,
  3502. 'additional' => $additional,
  3503. ]);
  3504. CartAddress::factory()->create([
  3505. 'cart_id' => $cart->id,
  3506. 'customer_id' => $customer->id,
  3507. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  3508. ]);
  3509. $customerAddress = CustomerAddress::factory()->create()->toArray();
  3510. CartPayment::factory()->create([
  3511. 'cart_id' => $cart->id,
  3512. 'method' => $paymentMethod = 'cashondelivery',
  3513. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  3514. ]);
  3515. $customerAddress = CustomerAddress::factory()->create()->toArray();
  3516. cart()->setCart($cart);
  3517. cart()->collectTotals();
  3518. // Act and Assert.
  3519. $this->loginAsCustomer($customer);
  3520. postJson(route('shop.checkout.onepage.addresses.store'), [
  3521. 'billing' => [
  3522. ...$customerAddress,
  3523. 'address' => [fake()->address()],
  3524. 'use_for_shipping' => fake()->boolean(),
  3525. ],
  3526. 'shipping' => [
  3527. ...$customerAddress,
  3528. 'address' => [fake()->address()],
  3529. ],
  3530. ])
  3531. ->assertOk()
  3532. ->assertJsonPath('redirect', false)
  3533. ->assertJsonMissingPath('data.shippingMethods');
  3534. });
  3535. it('should place order with two products with simple and configurable product type', function () {
  3536. // Arrange.
  3537. $simpleProduct = (new ProductFaker([
  3538. 'attributes' => [
  3539. 5 => 'new',
  3540. 6 => 'featured',
  3541. ],
  3542. 'attribute_value' => [
  3543. 'new' => [
  3544. 'boolean_value' => true,
  3545. ],
  3546. 'featured' => [
  3547. 'boolean_value' => true,
  3548. ],
  3549. ],
  3550. ]))
  3551. ->getSimpleProductFactory()
  3552. ->create();
  3553. $configurableProduct = (new ProductFaker([
  3554. 'attributes' => [
  3555. 5 => 'new',
  3556. 6 => 'featured',
  3557. ],
  3558. 'attribute_value' => [
  3559. 'new' => [
  3560. 'boolean_value' => true,
  3561. ],
  3562. 'featured' => [
  3563. 'boolean_value' => true,
  3564. ],
  3565. ],
  3566. ]))
  3567. ->getConfigurableProductFactory()
  3568. ->create();
  3569. foreach ($configurableProduct->super_attributes as $attribute) {
  3570. foreach ($attribute->options as $option) {
  3571. $super_attributes[$option->attribute_id] = $option->id;
  3572. }
  3573. }
  3574. $customer = Customer::factory()->create();
  3575. $cart = Cart::factory()->create([
  3576. 'customer_id' => $customer->id,
  3577. 'customer_first_name' => $customer->first_name,
  3578. 'customer_last_name' => $customer->last_name,
  3579. 'customer_email' => $customer->email,
  3580. 'is_guest' => 0,
  3581. 'shipping_method' => 'free_free',
  3582. ]);
  3583. $childProduct = $configurableProduct->variants()->first();
  3584. $simpleProductAdditional = [
  3585. 'product_id' => $simpleProduct->id,
  3586. 'rating' => '0',
  3587. 'is_buy_now' => '0',
  3588. 'quantity' => '1',
  3589. ];
  3590. $configurableProductAdditional = [
  3591. 'selected_configurable_option' => $childProduct->id,
  3592. 'product_id' => $configurableProduct->id,
  3593. 'is_buy_now' => '0',
  3594. 'rating' => '0',
  3595. 'quantity' => '1',
  3596. 'super_attribute' => $super_attributes ?? [],
  3597. ];
  3598. $configurableProductCartItem = CartItem::factory()->create([
  3599. 'cart_id' => $cart->id,
  3600. 'product_id' => $childProduct->id,
  3601. 'sku' => $childProduct->sku,
  3602. 'quantity' => $configurableProductAdditional['quantity'],
  3603. 'name' => $childProduct->name,
  3604. 'price' => $convertedPrice = core()->convertPrice($price = $childProduct->price),
  3605. 'price_incl_tax' => $convertedPrice,
  3606. 'base_price' => $price,
  3607. 'base_price_incl_tax' => $price,
  3608. 'total' => $convertedPrice * $configurableProductAdditional['quantity'],
  3609. 'total_incl_tax' => $convertedPrice * $configurableProductAdditional['quantity'],
  3610. 'base_total' => $price * $configurableProductAdditional['quantity'],
  3611. 'weight' => $childProduct->weight ?? 0,
  3612. 'total_weight' => ($childProduct->weight ?? 0) * $configurableProductAdditional['quantity'],
  3613. 'base_total_weight' => ($childProduct->weight ?? 0) * $configurableProductAdditional['quantity'],
  3614. 'type' => $childProduct->type,
  3615. 'additional' => $configurableProductAdditional,
  3616. ]);
  3617. $simpleProductCartItem = CartItem::factory()->create([
  3618. 'cart_id' => $cart->id,
  3619. 'product_id' => $simpleProduct->id,
  3620. 'sku' => $simpleProduct->sku,
  3621. 'quantity' => $simpleProductAdditional['quantity'],
  3622. 'name' => $simpleProduct->name,
  3623. 'price' => $convertedPrice = core()->convertPrice($price = $simpleProduct->price),
  3624. 'price_incl_tax' => $convertedPrice,
  3625. 'base_price' => $price,
  3626. 'base_price_incl_tax' => $price,
  3627. 'total' => $convertedPrice * $simpleProductAdditional['quantity'],
  3628. 'total_incl_tax' => $convertedPrice * $simpleProductAdditional['quantity'],
  3629. 'base_total' => $price * $simpleProductAdditional['quantity'],
  3630. 'weight' => $simpleProduct->weight ?? 0,
  3631. 'total_weight' => ($simpleProduct->weight ?? 0) * $simpleProductAdditional['quantity'],
  3632. 'base_total_weight' => ($simpleProduct->weight ?? 0) * $simpleProductAdditional['quantity'],
  3633. 'type' => $simpleProduct->type,
  3634. 'additional' => $simpleProductAdditional,
  3635. ]);
  3636. $cartBillingAddress = CartAddress::factory()->create([
  3637. 'cart_id' => $cart->id,
  3638. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  3639. ]);
  3640. $cartShippingAddress = CartAddress::factory()->create([
  3641. 'cart_id' => $cart->id,
  3642. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  3643. ]);
  3644. $cartShippingRate = CartShippingRate::factory()->create([
  3645. 'method_description' => 'Free Shipping',
  3646. 'carrier_title' => 'Free shipping',
  3647. 'method_title' => 'Free Shipping',
  3648. 'carrier' => 'free',
  3649. 'method' => 'free_free',
  3650. 'cart_address_id' => $cartShippingAddress->id,
  3651. 'cart_id' => $cart->id,
  3652. ]);
  3653. $cartPayment = CartPayment::factory()->create([
  3654. 'cart_id' => $cart->id,
  3655. 'method' => $paymentMethod = 'cashondelivery',
  3656. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  3657. ]);
  3658. cart()->setCart($cart);
  3659. cart()->collectTotals();
  3660. // Act and Assert.
  3661. $this->loginAsCustomer($customer);
  3662. postJson(route('shop.checkout.onepage.orders.store'))
  3663. ->assertOk()
  3664. ->assertJsonPath('data.redirect', true)
  3665. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  3666. $cart->refresh();
  3667. $configurableProductCartItem->refresh();
  3668. $simpleProductCartItem->refresh();
  3669. $cartPayment->refresh();
  3670. $cartBillingAddress->refresh();
  3671. $cartShippingAddress->refresh();
  3672. $this->assertModelWise([
  3673. Cart::class => [
  3674. $this->prepareCart($cart),
  3675. ],
  3676. CartItem::class => [
  3677. $this->prepareCartItem($configurableProductCartItem),
  3678. $this->prepareCartItem($simpleProductCartItem),
  3679. ],
  3680. CartPayment::class => [
  3681. $this->prepareCartPayment($cartPayment),
  3682. ],
  3683. CartAddress::class => [
  3684. $this->prepareAddress($cartBillingAddress),
  3685. $this->prepareAddress($cartShippingAddress),
  3686. ],
  3687. CartShippingRate::class => [
  3688. $this->prepareCartShippingRate($cartShippingRate),
  3689. ],
  3690. Order::class => [
  3691. $this->prepareOrderUsingCart($cart),
  3692. ],
  3693. OrderItem::class => [
  3694. $this->prepareOrderItemUsingCartItem($configurableProductCartItem),
  3695. $this->prepareOrderItemUsingCartItem($simpleProductCartItem),
  3696. ],
  3697. OrderPayment::class => [
  3698. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  3699. ],
  3700. ProductOrderedInventory::class => [
  3701. [
  3702. 'qty' => $simpleProductCartItem->quantity,
  3703. 'product_id' => $simpleProduct->id,
  3704. ],
  3705. ],
  3706. ProductInventoryIndex::class => [
  3707. [
  3708. 'qty' => $simpleProduct->inventory_source_qty(1) - $simpleProduct->quantity,
  3709. 'product_id' => $simpleProduct->id,
  3710. ],
  3711. ],
  3712. ProductOrderedInventory::class => [
  3713. [
  3714. 'product_id' => $childProduct->id,
  3715. 'qty' => $configurableProductCartItem->quantity,
  3716. ],
  3717. ],
  3718. ProductOrderedInventory::class => [
  3719. [
  3720. 'product_id' => $simpleProduct->id,
  3721. 'qty' => $simpleProductCartItem->quantity,
  3722. ],
  3723. ],
  3724. ProductInventoryIndex::class => [
  3725. [
  3726. 'qty' => $childProduct->inventory_source_qty(1) - $configurableProductCartItem->quantity,
  3727. 'product_id' => $childProduct->id,
  3728. ],
  3729. ],
  3730. ProductInventoryIndex::class => [
  3731. [
  3732. 'qty' => $simpleProduct->inventory_source_qty(1) - $configurableProductCartItem->quantity,
  3733. 'product_id' => $simpleProduct->id,
  3734. ],
  3735. ],
  3736. ]);
  3737. });
  3738. it('should place order with two products with simple and grouped product type', function () {
  3739. // Arrange.
  3740. $simpleProduct = (new ProductFaker([
  3741. 'attributes' => [
  3742. 5 => 'new',
  3743. 6 => 'featured',
  3744. ],
  3745. 'attribute_value' => [
  3746. 'new' => [
  3747. 'boolean_value' => true,
  3748. ],
  3749. 'featured' => [
  3750. 'boolean_value' => true,
  3751. ],
  3752. ],
  3753. ]))
  3754. ->getSimpleProductFactory()
  3755. ->create();
  3756. $groupedProduct = (new ProductFaker([
  3757. 'attributes' => [
  3758. 5 => 'new',
  3759. 6 => 'featured',
  3760. ],
  3761. 'attribute_value' => [
  3762. 'new' => [
  3763. 'boolean_value' => true,
  3764. ],
  3765. 'featured' => [
  3766. 'boolean_value' => true,
  3767. ],
  3768. ],
  3769. ]))
  3770. ->getGroupedProductFactory()
  3771. ->create();
  3772. $customer = Customer::factory()->create();
  3773. $data = [
  3774. 'prices' => [],
  3775. 'qty' => [],
  3776. 'grand_total' => [],
  3777. ];
  3778. $bundleProducts = $groupedProduct->grouped_products(['associated_product'])->get();
  3779. foreach ($bundleProducts as $bundleProduct) {
  3780. $data['prices'][] = $price = $bundleProduct->associated_product->price;
  3781. $data['qty'][] = $qty = $bundleProduct->qty;
  3782. $data['grand_total'][] = $price * $qty;
  3783. }
  3784. $data['prices'][] = $simpleProduct->price;
  3785. $data['qty'][] = 1;
  3786. $data['grand_total'][] = (float) $simpleProduct->price;
  3787. $cart = Cart::factory()->create([
  3788. 'channel_id' => core()->getCurrentChannel()->id,
  3789. 'global_currency_code' => $baseCurrencyCode = core()->getBaseCurrencyCode(),
  3790. 'base_currency_code' => $baseCurrencyCode,
  3791. 'channel_currency_code' => core()->getChannelBaseCurrencyCode(),
  3792. 'cart_currency_code' => core()->getCurrentCurrencyCode(),
  3793. 'items_count' => 5,
  3794. 'items_qty' => array_sum($data['qty']),
  3795. 'grand_total' => $price = array_sum($data['grand_total']),
  3796. 'base_grand_total' => $price,
  3797. 'sub_total' => $price,
  3798. 'shipping_method' => 'free_free',
  3799. 'customer_id' => $customer->id,
  3800. 'is_active' => 1,
  3801. 'customer_email' => $customer->email,
  3802. 'customer_first_name' => $customer->first_name,
  3803. 'customer_last_name' => $customer->last_name,
  3804. ]);
  3805. $bundleProductCartItems = [];
  3806. foreach ($bundleProducts as $bundleProduct) {
  3807. $bundleProductCartItems[] = CartItem::factory()->create([
  3808. 'quantity' => $bundleProduct->qty,
  3809. 'product_id' => $bundleProduct->associated_product->id,
  3810. 'sku' => $bundleProduct->associated_product->sku,
  3811. 'name' => $bundleProduct->associated_product->name,
  3812. 'type' => $bundleProduct->associated_product->type,
  3813. 'weight' => 1,
  3814. 'total_weight' => 1,
  3815. 'base_total_weight' => 1,
  3816. 'cart_id' => $cart->id,
  3817. ]);
  3818. }
  3819. $simpleProductCartItem = CartItem::factory()->create([
  3820. 'quantity' => 1,
  3821. 'product_id' => $simpleProduct->id,
  3822. 'sku' => $simpleProduct->sku,
  3823. 'name' => $simpleProduct->name,
  3824. 'type' => $simpleProduct->type,
  3825. 'weight' => 1,
  3826. 'total_weight' => 1,
  3827. 'base_total_weight' => 1,
  3828. 'cart_id' => $cart->id,
  3829. ]);
  3830. $cartBillingAddress = CartAddress::factory()->create([
  3831. 'cart_id' => $cart->id,
  3832. 'customer_id' => $customer->id,
  3833. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  3834. ]);
  3835. $cartShippingAddress = CartAddress::factory()->create([
  3836. 'cart_id' => $cart->id,
  3837. 'customer_id' => $customer->id,
  3838. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  3839. ]);
  3840. $cartPayment = CartPayment::factory()->create([
  3841. 'cart_id' => $cart->id,
  3842. 'method' => $paymentMethod = 'cashondelivery',
  3843. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  3844. ]);
  3845. $cartShippingRate = CartShippingRate::factory()->create([
  3846. 'method_description' => 'Free Shipping',
  3847. 'carrier_title' => 'Free shipping',
  3848. 'method_title' => 'Free Shipping',
  3849. 'carrier' => 'free',
  3850. 'method' => 'free_free',
  3851. 'cart_address_id' => $cartShippingAddress->id,
  3852. 'cart_id' => $cart->id,
  3853. ]);
  3854. cart()->setCart($cart);
  3855. cart()->collectTotals();
  3856. // Act and Assert.
  3857. $this->loginAsCustomer($customer);
  3858. postJson(route('shop.checkout.onepage.orders.store'))
  3859. ->assertOk()
  3860. ->assertJsonPath('data.redirect', true)
  3861. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  3862. $cart->refresh();
  3863. $simpleProductCartItem->refresh();
  3864. $cartBillingAddress->refresh();
  3865. $cartShippingAddress->refresh();
  3866. $this->assertModelWise([
  3867. Cart::class => [
  3868. $this->prepareCart($cart),
  3869. ],
  3870. CartItem::class => [
  3871. $this->prepareCartItem($simpleProductCartItem),
  3872. ],
  3873. CartPayment::class => [
  3874. $this->prepareCartPayment($cartPayment),
  3875. ],
  3876. CartAddress::class => [
  3877. $this->prepareAddress($cartBillingAddress),
  3878. $this->prepareAddress($cartShippingAddress),
  3879. ],
  3880. CartShippingRate::class => [
  3881. $this->prepareCartShippingRate($cartShippingRate),
  3882. ],
  3883. Order::class => [
  3884. $this->prepareOrderUsingCart($cart),
  3885. ],
  3886. OrderItem::class => [
  3887. $this->prepareOrderItemUsingCartItem($simpleProductCartItem),
  3888. ],
  3889. OrderPayment::class => [
  3890. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  3891. ],
  3892. ProductOrderedInventory::class => [
  3893. [
  3894. 'product_id' => $simpleProduct->id,
  3895. 'qty' => 1,
  3896. ],
  3897. ],
  3898. ProductInventoryIndex::class => [
  3899. [
  3900. 'qty' => $simpleProduct->inventory_source_qty(1) - 1,
  3901. 'product_id' => $simpleProduct->id,
  3902. ],
  3903. ],
  3904. ]);
  3905. foreach ($bundleProductCartItems as $bundleProductCartItem) {
  3906. $bundleProductCartItem->refresh();
  3907. $this->assertModelWise([
  3908. CartItem::class => [
  3909. $this->prepareCartItem($bundleProductCartItem),
  3910. ],
  3911. OrderItem::class => [
  3912. $this->prepareOrderItemUsingCartItem($bundleProductCartItem),
  3913. ],
  3914. ]);
  3915. }
  3916. foreach ($bundleProducts as $bundleProduct) {
  3917. $this->assertModelWise([
  3918. ProductOrderedInventory::class => [
  3919. [
  3920. 'product_id' => $bundleProduct->associated_product->id,
  3921. 'qty' => $bundleProduct->qty,
  3922. ],
  3923. ],
  3924. ProductInventoryIndex::class => [
  3925. [
  3926. 'qty' => $bundleProduct->associated_product->inventory_source_qty(1) - $bundleProduct->qty,
  3927. 'product_id' => $bundleProduct->associated_product->id,
  3928. ],
  3929. ],
  3930. ]);
  3931. }
  3932. });
  3933. it('should place order with two products with simple and downloadable product type', function () {
  3934. // Arrange.
  3935. $simpleProduct = (new ProductFaker([
  3936. 'attributes' => [
  3937. 5 => 'new',
  3938. 6 => 'featured',
  3939. ],
  3940. 'attribute_value' => [
  3941. 'new' => [
  3942. 'boolean_value' => true,
  3943. ],
  3944. 'featured' => [
  3945. 'boolean_value' => true,
  3946. ],
  3947. ],
  3948. ]))
  3949. ->getSimpleProductFactory()
  3950. ->create();
  3951. $downloadableProduct = (new ProductFaker([
  3952. 'attributes' => [
  3953. 5 => 'new',
  3954. 6 => 'featured',
  3955. ],
  3956. 'attribute_value' => [
  3957. 'new' => [
  3958. 'boolean_value' => true,
  3959. ],
  3960. 'featured' => [
  3961. 'boolean_value' => true,
  3962. ],
  3963. ],
  3964. ]))
  3965. ->getDownloadableProductFactory()
  3966. ->create();
  3967. $customer = Customer::factory()->create();
  3968. $cart = Cart::factory()->create([
  3969. 'customer_id' => $customer->id,
  3970. 'customer_first_name' => $customer->first_name,
  3971. 'customer_last_name' => $customer->last_name,
  3972. 'customer_email' => $customer->email,
  3973. 'is_guest' => 0,
  3974. 'shipping_method' => 'free_free',
  3975. ]);
  3976. $downloadAdditional = [
  3977. 'product_id' => $downloadableProduct->id,
  3978. 'is_buy_now' => '0',
  3979. 'rating' => '0',
  3980. 'quantity' => '1',
  3981. 'links' => [
  3982. '1',
  3983. ],
  3984. ];
  3985. $simpleAdditional = [
  3986. 'product_id' => $simpleProduct->id,
  3987. 'rating' => '0',
  3988. 'is_buy_now' => '0',
  3989. 'quantity' => '1',
  3990. ];
  3991. $simpleProductCartItem = CartItem::factory()->create([
  3992. 'cart_id' => $cart->id,
  3993. 'product_id' => $simpleProduct->id,
  3994. 'sku' => $simpleProduct->sku,
  3995. 'quantity' => $simpleAdditional['quantity'],
  3996. 'name' => $simpleProduct->name,
  3997. 'price' => $convertedPrice = core()->convertPrice($price = $simpleProduct->price),
  3998. 'base_price' => $price,
  3999. 'total' => $convertedPrice * $simpleAdditional['quantity'],
  4000. 'base_total' => $price * $simpleAdditional['quantity'],
  4001. 'weight' => $simpleProduct->weight ?? 0,
  4002. 'total_weight' => ($simpleProduct->weight ?? 0) * $simpleAdditional['quantity'],
  4003. 'base_total_weight' => ($simpleProduct->weight ?? 0) * $simpleAdditional['quantity'],
  4004. 'type' => $simpleProduct->type,
  4005. 'additional' => $simpleAdditional,
  4006. ]);
  4007. $downloadableProductCartItem = CartItem::factory()->create([
  4008. 'cart_id' => $cart->id,
  4009. 'product_id' => $downloadableProduct->id,
  4010. 'sku' => $downloadableProduct->sku,
  4011. 'quantity' => $downloadAdditional['quantity'],
  4012. 'name' => $downloadableProduct->name,
  4013. 'price' => $convertedPrice = core()->convertPrice($price = $downloadableProduct->price),
  4014. 'base_price' => $price,
  4015. 'total' => $convertedPrice * $downloadAdditional['quantity'],
  4016. 'base_total' => $price * $downloadAdditional['quantity'],
  4017. 'weight' => $downloadableProduct->weight ?? 0,
  4018. 'total_weight' => ($downloadableProduct->weight ?? 0) * $downloadAdditional['quantity'],
  4019. 'base_total_weight' => ($downloadableProduct->weight ?? 0) * $downloadAdditional['quantity'],
  4020. 'type' => $downloadableProduct->type,
  4021. 'additional' => $downloadAdditional,
  4022. ]);
  4023. $cartBillingAddress = CartAddress::factory()->create([
  4024. 'cart_id' => $cart->id,
  4025. 'customer_id' => $customer->id,
  4026. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  4027. ]);
  4028. $cartShippingAddress = CartAddress::factory()->create([
  4029. 'cart_id' => $cart->id,
  4030. 'customer_id' => $customer->id,
  4031. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  4032. ]);
  4033. $cartShippingRate = CartShippingRate::factory()->create([
  4034. 'carrier' => 'free',
  4035. 'carrier_title' => 'Free shipping',
  4036. 'method' => 'free_free',
  4037. 'method_title' => 'Free Shipping',
  4038. 'method_description' => 'Free Shipping',
  4039. 'cart_address_id' => $cartShippingAddress->id,
  4040. 'cart_id' => $cart->id,
  4041. ]);
  4042. $cartPayment = CartPayment::factory()->create([
  4043. 'cart_id' => $cart->id,
  4044. 'method' => $paymentMethod = 'cashondelivery',
  4045. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  4046. ]);
  4047. cart()->setCart($cart);
  4048. cart()->collectTotals();
  4049. // Act and Assert.
  4050. $this->loginAsCustomer($customer);
  4051. postJson(route('shop.checkout.onepage.orders.store'))
  4052. ->assertOk()
  4053. ->assertJsonPath('data.redirect', true)
  4054. ->assertJsonPath('data.redirect_url', route('shop.checkout.onepage.success'));
  4055. $cart->refresh();
  4056. $simpleProductCartItem->refresh();
  4057. $downloadableProductCartItem->refresh();
  4058. $cartPayment->refresh();
  4059. $cartBillingAddress->refresh();
  4060. $cartShippingAddress->refresh();
  4061. $this->assertModelWise([
  4062. Cart::class => [
  4063. $this->prepareCart($cart),
  4064. ],
  4065. CartItem::class => [
  4066. $this->prepareCartItem($simpleProductCartItem),
  4067. $this->prepareCartItem($downloadableProductCartItem),
  4068. ],
  4069. CartPayment::class => [
  4070. $this->prepareCartPayment($cartPayment),
  4071. ],
  4072. CartAddress::class => [
  4073. $this->prepareAddress($cartBillingAddress),
  4074. $this->prepareAddress($cartShippingAddress),
  4075. ],
  4076. CartShippingRate::class => [
  4077. $this->prepareCartShippingRate($cartShippingRate),
  4078. ],
  4079. Order::class => [
  4080. $this->prepareOrderUsingCart($cart),
  4081. ],
  4082. OrderItem::class => [
  4083. $this->prepareOrderItemUsingCartItem($simpleProductCartItem),
  4084. $this->prepareOrderItemUsingCartItem($downloadableProductCartItem),
  4085. ],
  4086. OrderPayment::class => [
  4087. $this->prepareOrderPaymentUsingCartPayment($cart->payment),
  4088. ],
  4089. ProductOrderedInventory::class => [
  4090. [
  4091. 'qty' => $simpleProductCartItem->quantity,
  4092. 'product_id' => $simpleProduct->id,
  4093. ],
  4094. ],
  4095. ProductInventoryIndex::class => [
  4096. [
  4097. 'qty' => $simpleProduct->inventory_source_qty(1) - $simpleProductCartItem->quantity,
  4098. 'product_id' => $simpleProduct->id,
  4099. ],
  4100. ],
  4101. ProductOrderedInventory::class => [
  4102. [
  4103. 'product_id' => $simpleProduct->id,
  4104. 'qty' => 1,
  4105. ],
  4106. ],
  4107. ProductInventoryIndex::class => [
  4108. [
  4109. 'qty' => $simpleProduct->inventory_source_qty(1) - 1,
  4110. 'product_id' => $simpleProduct->id,
  4111. ],
  4112. ],
  4113. ]);
  4114. });