|
1 | | -// |
2 | | -// LabelCore |
3 | | -// Label StoreMax |
| 1 | +// WooGym |
4 | 2 | // |
5 | 3 | // Created by Anthony Gordon. |
6 | | -// 2023, WooSignal Ltd. All rights reserved. |
| 4 | +// 2024, WooSignal Ltd. All rights reserved. |
7 | 5 | // |
8 | 6 |
|
9 | 7 | // Unless required by applicable law or agreed to in writing, software |
10 | 8 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | -// |
13 | 10 |
|
14 | 11 | import 'package:flutter/material.dart'; |
15 | | -import 'package:flutter_app/app/models/cart_line_item.dart'; |
16 | | -import 'package:flutter_app/bootstrap/data/order_wc.dart'; |
17 | | -import 'package:flutter_app/bootstrap/helpers.dart'; |
18 | | -import 'package:flutter_app/resources/pages/checkout_confirmation_page.dart'; |
19 | | -import 'package:flutter_app/resources/widgets/checkout_paypal.dart'; |
20 | | -import 'package:nylo_framework/nylo_framework.dart'; |
| 12 | +import 'package:flutter_paypal_payment/flutter_paypal_payment.dart'; |
21 | 13 | import 'package:woosignal/models/payload/order_wc.dart'; |
22 | 14 | import 'package:woosignal/models/response/order.dart'; |
23 | 15 | import 'package:woosignal/models/response/tax_rate.dart'; |
| 16 | +import 'package:woosignal/models/response/woosignal_app.dart'; |
| 17 | +import '/bootstrap/data/order_wc.dart'; |
| 18 | +import '/resources/pages/checkout_status_page.dart'; |
| 19 | +import '/app/models/cart_line_item.dart'; |
| 20 | +import '/app/models/checkout_session.dart'; |
| 21 | +import '/resources/pages/checkout_confirmation_page.dart'; |
| 22 | +import '/bootstrap/app_helper.dart'; |
| 23 | +import '/bootstrap/helpers.dart'; |
| 24 | +import 'package:nylo_framework/nylo_framework.dart'; |
| 25 | + |
| 26 | +payPalPay(context, {required CheckoutConfirmationPageState state, TaxRate? taxRate}) async { |
24 | 27 |
|
25 | | -payPalPay(context, |
26 | | - {required CheckoutConfirmationPageState state, TaxRate? taxRate}) async { |
27 | 28 | await checkout(taxRate, (total, billingDetails, cart) async { |
| 29 | + WooSignalApp? wooSignalApp = AppHelper.instance.appConfig; |
| 30 | + |
28 | 31 | List<CartLineItem> cartLineItems = await cart.getCart(); |
29 | | - String description = await cart.cartShortDesc(); |
| 32 | + String cartTotal = await cart.getTotal(); |
| 33 | + String? currencyCode = wooSignalApp?.currencyMeta?.code; |
30 | 34 |
|
31 | | - await Navigator.push( |
32 | | - context, |
33 | | - MaterialPageRoute( |
34 | | - builder: (_) => PayPalCheckout( |
35 | | - description: description, |
36 | | - amount: total, |
37 | | - cartLineItems: cartLineItems))).then((value) async { |
38 | | - if (value is! Map<String, dynamic>) { |
39 | | - showToastNotification( |
40 | | - context, |
41 | | - title: trans("Payment Cancelled"), |
42 | | - description: trans("The payment has been cancelled"), |
43 | | - ); |
44 | | - state.reloadState(showLoader: false); |
45 | | - return; |
46 | | - } |
| 35 | + String shippingTotal = CheckoutSession.getInstance.shippingType?.getTotal() ?? "0"; |
47 | 36 |
|
48 | | - state.reloadState(showLoader: true); |
49 | | - if (value.containsKey("status") && value["status"] == "success") { |
50 | | - OrderWC orderWC = await buildOrderWC(taxRate: taxRate, markPaid: true); |
51 | | - Order? order = await (appWooSignal((api) => api.createOrder(orderWC))); |
| 37 | + String description = "(${cartLineItems.length}) items from ${getEnv('APP_NAME')}".tr(arguments: {"appName": getEnv('APP_NAME')}); |
52 | 38 |
|
53 | | - if (order == null) { |
| 39 | + Navigator.of(context).push(MaterialPageRoute( |
| 40 | + builder: (BuildContext context) => PaypalCheckoutView( |
| 41 | + sandboxMode: getEnv('PAYPAL_LIVE_MODE') != true, |
| 42 | + clientId: getEnv('PAYPAL_CLIENT_ID'), |
| 43 | + secretKey: getEnv('PAYPAL_SECRET_KEY'), |
| 44 | + note: "Contact us for any questions on your order.".tr(), |
| 45 | + transactions: [ |
| 46 | + { |
| 47 | + "amount": { |
| 48 | + "total": total, |
| 49 | + "currency": currencyCode?.toUpperCase(), |
| 50 | + "details": { |
| 51 | + "subtotal": cartTotal, |
| 52 | + "shipping": shippingTotal, |
| 53 | + "shipping_discount": 0 |
| 54 | + } |
| 55 | + }, |
| 56 | + "description": description, |
| 57 | + "item_list": { |
| 58 | + "items": cartLineItems.map((item) => { |
| 59 | + "name": item.name, |
| 60 | + "quantity": item.quantity, |
| 61 | + "price": item.total, |
| 62 | + "currency": currencyCode?.toUpperCase() |
| 63 | + }).toList(), |
| 64 | + |
| 65 | + "shipping_address": { |
| 66 | + "recipient_name": "${billingDetails?.shippingAddress?.nameFull()}", |
| 67 | + "line1": billingDetails?.shippingAddress?.addressLine, |
| 68 | + "line2": "", |
| 69 | + "city": billingDetails?.shippingAddress?.city, |
| 70 | + "country_code": billingDetails?.shippingAddress?.customerCountry?.countryCode, |
| 71 | + "postal_code": billingDetails?.shippingAddress?.postalCode, |
| 72 | + "phone": billingDetails?.shippingAddress?.phoneNumber, |
| 73 | + "state": billingDetails?.shippingAddress?.customerCountry?.state?.name |
| 74 | + }, |
| 75 | + } |
| 76 | + } |
| 77 | + ], |
| 78 | + onSuccess: (Map params) async { |
| 79 | + OrderWC orderWC = await buildOrderWC(taxRate: taxRate); |
| 80 | + Order? order = await (appWooSignal((api) => api.createOrder(orderWC))); |
| 81 | + |
| 82 | + if (order == null) { |
| 83 | + showToastNotification( |
| 84 | + context, |
| 85 | + title: trans("Error"), |
| 86 | + description: trans("Something went wrong, please contact our store"), |
| 87 | + ); |
| 88 | + state.reloadState(showLoader: false); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + routeTo(CheckoutStatusPage.path, data: order); |
| 93 | + }, |
| 94 | + onError: (error) { |
| 95 | + NyLogger.error(error.toString()); |
54 | 96 | showToastNotification( |
55 | 97 | context, |
56 | 98 | title: trans("Error"), |
57 | 99 | description: |
58 | | - trans("Something went wrong, please contact our store"), |
| 100 | + trans("Something went wrong, please contact our store"), |
| 101 | + ); |
| 102 | + updateState(CheckoutConfirmationPage.path, data: {"reloadState": false}); |
| 103 | + }, |
| 104 | + onCancel: () { |
| 105 | + showToastNotification( |
| 106 | + context, |
| 107 | + title: trans("Payment Cancelled"), |
| 108 | + description: trans("The payment has been cancelled"), |
59 | 109 | ); |
60 | | - return; |
61 | | - } |
62 | | - Navigator.pushNamed(context, "/checkout-status", arguments: order); |
63 | | - return; |
64 | | - } else { |
65 | | - showToastNotification( |
66 | | - context, |
67 | | - title: trans("Payment Cancelled"), |
68 | | - description: trans("The payment has been cancelled"), |
69 | | - ); |
70 | | - } |
71 | | - }); |
72 | | - state.reloadState(showLoader: false); |
| 110 | + updateState(CheckoutConfirmationPage.path, data: {"reloadState": false}); |
| 111 | + }, |
| 112 | + ),),); |
73 | 113 | }); |
74 | 114 | } |
0 commit comments