NikolaGavric94 / laravel-square

Square integration with Laravel/Lumen >=5.5
MIT License
31 stars 23 forks source link

Setting up an order for gift cards in conjunction with credit card payment #37

Closed gogeens closed 4 years ago

gogeens commented 4 years ago

Hello Nikola, Hope all is well. As the title suggests, we are having a slightly difficult time creating an order that would handle multiple transactions (combination of gift cards and credit card). At the moment, we calculate the entire amount the customer needs to pay and simply charge them that amount from a cc (eg: $transaction = Square::charge($options)). From our understanding, to split the payment into multiple transactions we need to set up an order, as evident from https://developer.squareup.com/docs/payment-form/gift-cards/part-1 ("Additionally, this guide makes the following assumptions: Your backend supports a check out experience, maintains a checkout payment balance, and can provide an Order or reference ID."). We have tried the following https://github.com/NikolaGavric94/laravel-square/wiki/Order%20Examples, but have been unsuccessful at saving the order properly, as we keep getting "Array to string conversion" error. The only time we were successful at getting the order instance was when we substituted $order with a string "order" in the setOrder method. Anyway, maybe we are misunderstanding something in the first place, as we don't have "products" for the order per say, we simply have the final amount that we need to charge the customer in separate transactions. So what i'm getting at is can/should we use your package with gift cards? Hopefully you can help us out. Thank you. Our square order that fails to convert array to string:

$products = array([
            'name' => some int,
            'variation_name' => some string,
            'note' => some string,
            'price' => some int - total amount we want to charge,
            'quantity' => some int - 1 for us,
            'reference_id' => some int - holds our own transaction id
        ]);

        $order = array(
            'payment_service_identifier' => null,
            'payment_service_type' => 'square',
            'products' => $products
        );

        $location_id = some string - our location id;

        $squareOrder = Square::setOrder($order, $location_id, 'CAD')->save();
NikolaGavric94 commented 4 years ago

Hmm, that is unusual and all the examples are covered with tests. Could you maybe provide a stacktrace of the error u get? Thanks

gogeens commented 4 years ago

Hi Nikola, here is the issue:

Screen Shot 2020-02-24 at 10 11 33 AM

which happens right here:

Screen Shot 2020-02-24 at 10 16 28 AM

and here is the code for the order: Screen Shot 2020-02-24 at 10 14 22 AM We also tried pasting mock products from your example and it doesn't work. Let me know if you want to see anything else

NikolaGavric94 commented 4 years ago

Okay, I will look into this today and get back to you by the end of the day

NikolaGavric94 commented 4 years ago

I wasn't able to replicate the issue, used a fresh project and installed the latest version of the library, are you using latest version too?

Also, if you are still experiencing the problem, could you post the stacktrace from the server logs, not the web view?

gogeens commented 4 years ago

Hey Nikola, Yeah, we are using the latest version of the package. The stacktrace you asked for:

[2020-02-24 10:13:53] production.ERROR: Array to string conversion {"exception":"[object] (ErrorException(code: 0): Array to string conversion at /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Support/Str.php:360)
[stacktrace]
#0 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Support/Str.php(360): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Array to string...', '/Users/george/D...', 360, Array)
#1 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/QueryException.php(56): Illuminate\\Support\\Str::replaceArray('?', Array, 'insert into `bo...')
#2 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/QueryException.php(39): Illuminate\\Database\\QueryException->formatMessage('insert into `bo...', Array, Object(PDOException))
#3 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Connection.php(666): Illuminate\\Database\\QueryException->__construct('insert into `bo...', Array, Object(PDOException))
#4 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Connection.php(625): Illuminate\\Database\\Connection->runQueryCallback('insert into `bo...', Array, Object(Closure))
#5 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Connection.php(460): Illuminate\\Database\\Connection->run('insert into `bo...', Array, Object(Closure))
#6 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Connection.php(412): Illuminate\\Database\\Connection->statement('insert into `bo...', Array)
#7 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php(32): Illuminate\\Database\\Connection->insert('insert into `bo...', Array)
#8 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2658): Illuminate\\Database\\Query\\Processors\\Processor->processInsertGetId(Object(Illuminate\\Database\\Query\\Builder), 'insert into `bo...', Array, 'id')
#9 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1350): Illuminate\\Database\\Query\\Builder->insertGetId(Array, 'id')
#10 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(839): Illuminate\\Database\\Eloquent\\Builder->__call('insertGetId', Array)
#11 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(804): Illuminate\\Database\\Eloquent\\Model->insertAndSetId(Object(Illuminate\\Database\\Eloquent\\Builder), Array)
#12 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(667): Illuminate\\Database\\Eloquent\\Model->performInsert(Object(Illuminate\\Database\\Eloquent\\Builder))
#13 /Users/george/Documents/projects/project/vendor/nikolag/laravel-square/src/builders/OrderBuilder.php(52): Illuminate\\Database\\Eloquent\\Model->save()
#14 /Users/george/Documents/projects/project/vendor/nikolag/laravel-square/src/SquareService.php(124): Nikolag\\Square\\Builders\\OrderBuilder->buildOrderFromOrderCopy(Object(App\\Bookings), Object(stdClass))
#15 /Users/george/Documents/projects/project/vendor/nikolag/laravel-square/src/SquareService.php(192): Nikolag\\Square\\SquareService->_saveOrder()
#16 /Users/george/Documents/projects/project/app/Http/Controllers/CreateBookingController.php(261): Nikolag\\Square\\SquareService->save()
#17 [internal function]: App\\Http\\Controllers\\CreateBookingController->storeSelectTime(Object(Illuminate\\Http\\Request))
#18 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#19 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('storeSelectTime', Array)
#20 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\CreateBookingController), 'storeSelectTime')
#21 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\\Routing\\Route->runController()
#22 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(680): Illuminate\\Routing\\Route->run()
#23 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#24 /Users/george/Documents/projects/project/app/Http/Middleware/BookingSessionTimeout.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#25 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): App\\Http\\Middleware\\BookingSessionTimeout->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#26 /Users/george/Documents/projects/project/app/Http/Middleware/CheckBookingSession.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#27 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): App\\Http\\Middleware\\CheckBookingSession->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'game_id')
#28 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#29 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#30 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(76): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#31 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#32 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#33 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#34 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(56): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#35 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#36 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#37 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#38 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(66): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#39 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#40 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#41 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#42 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#43 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#44 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#45 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#46 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#47 /Users/george/Documents/projects/project/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(65): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#48 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#49 /Users/george/Documents/projects/project/app/Http/Middleware/LoadData.php(32): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#50 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): App\\Http\\Middleware\\LoadData->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#51 /Users/george/Documents/projects/project/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#52 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#53 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#54 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#55 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#56 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#57 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#58 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#59 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#60 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#61 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#62 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#63 /Users/george/Documents/projects/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#64 /Users/george/Documents/projects/project/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#65 /Users/george/Documents/projects/project/server.php(21): require_once('/Users/george/D...')
#66 {main}
"} 
NikolaGavric94 commented 4 years ago

Good news @gogeens is that I've identified the bug and I will post couple of solutions but there will be one library update to address mass asignment, but because the bug is actually not in the library itself but in the way the Order system has been setup on your side, which is caused due to some guidelines I've missed to put into the examples. So I will update examples and README.md to reflect this issue you just encountered. Solutions incoming in the next comment...

gogeens commented 4 years ago

Thank you so much Nikola, we sincerely appreciate it!

NikolaGavric94 commented 4 years ago
<?php
...
class Order extends Model {
    use HasProducts;

    ...
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'orders';

   /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'products',
        'payment_service_id',
        'payment_service_type'
    ];
}

This is needed so the library can actually know which table is responsible for holding your orders and $fillable so we can mass assign products, payment_service_id and payment_service_type

I will update the library but before that I want to do some more tests and run them before, I will notify you as soon as I push out a fix for this so you can just upgrade it and continue with your work. Sorry for any inconvenience.

NikolaGavric94 commented 4 years ago

@gogeens This should be fixed in the v2.4.2, please update your model with $table property to target table for your orders and also follow Orders examples, if you are still having issues after this feel free to reopen the ticket.

Sorry for inconvenience

gogeens commented 4 years ago

Excellent, thank you so much! We will give it a go shortly and get back to you if there are any persisting issues.