2021-08-27 01:38:24 -06:00
/ *
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/.
* /
function checkAccountStatus ( callback ) {
2021-10-19 17:31:18 -06:00
if ( inStorage ( "accountnumber" ) ) {
2021-08-27 01:38:24 -06:00
apirequest ( SETTINGS . apis . authorstartverify , {
2021-10-19 17:31:18 -06:00
accountnumber : getStorage ( "accountnumber" ) ,
2021-08-27 01:38:24 -06:00
accountkey : ( inStorage ( "accountkey" ) ? getStorage ( "accountkey" ) : "" )
} , function ( resp ) {
if ( resp . status == "OK" ) {
if ( resp . authok && resp . accountok ) {
callback ( true ) ;
} else if ( ! resp . authok && resp . accountok ) {
// verify phone or email
// openAccountVerify(resp.verifymsg);
callback ( "noauth" , resp . verifymsg ) ;
} else if ( ! resp . authok && ! resp . accountok ) {
callback ( false ) ;
} else {
callback ( "badstate" ) ;
}
} else {
2021-09-06 20:17:36 -06:00
router . back ( ) ;
2021-09-08 21:17:29 -06:00
// Server is saying something's wrong, let's clear the account number in case
// the user wants to try a different one.
2021-10-19 17:31:18 -06:00
removeFromStorage ( "accountnumber" ) ;
2021-08-27 01:38:24 -06:00
app . dialog . alert ( resp . msg , "Error" ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Couldn't check account status" , resp . msg ) ;
2021-08-27 01:38:24 -06:00
}
2022-01-26 14:10:09 -07:00
} , function ( xhr , status , error ) {
2021-09-06 20:17:36 -06:00
router . back ( ) ;
2021-08-27 01:38:24 -06:00
app . dialog . alert ( "Something went wrong. Try again later." , "Error" ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Couldn't check account status" , "Server/network problem: " + xhr . status + ": " + xhr . statusText ) ;
2021-08-27 01:38:24 -06:00
} ) ;
} else {
callback ( false ) ;
}
}
2021-09-22 00:34:54 -06:00
function checkIfAccountGoodWithPaymentMethod ( successcb , errorcb ) {
2021-10-19 17:31:18 -06:00
if ( ! inStorage ( "accountnumber" ) || ! inStorage ( "accountkey" ) ) {
2021-09-22 00:34:54 -06:00
successcb ( false ) ;
return ;
}
apirequest ( SETTINGS . apis . getaccountinfo , {
2021-10-19 17:31:18 -06:00
accountnumber : getStorage ( "accountnumber" ) ,
2021-09-22 00:34:54 -06:00
accountkey : getStorage ( "accountkey" )
} , function ( success ) {
if ( success . status == "OK" ) {
if ( success . payments _setup === false ) {
successcb ( false ) ;
} else {
successcb ( true ) ;
}
} else {
errorcb ( success . msg ) ;
}
} , function ( error ) {
errorcb ( "Couldn't get your account info. Try again later." ) ;
} , "GET" ) ;
}
2021-08-27 01:38:24 -06:00
function openAccountVerify ( verifymsg ) {
app . dialog . prompt ( verifymsg , "Verify Your Account" , function ( val ) {
verifyCode ( val ) ;
} , function ( cancel ) {
// shrug
} , "" ) ;
}
/ * *
* Confirm auth / login code with server and store login key if successful .
* @ param { type } code
* @ returns { undefined }
* /
function verifyCode ( code ) {
app . dialog . preloader ( "Verifying..." ) ;
apirequest ( SETTINGS . apis . verifyauthcode , {
code : code ,
2021-10-19 17:31:18 -06:00
accountnumber : getStorage ( "accountnumber" )
2021-08-27 01:38:24 -06:00
} , function ( resp ) {
app . dialog . close ( ) ;
if ( resp . status == "OK" ) {
setStorage ( "accountkey" , resp . authkey ) ;
app . dialog . alert ( "This device has been successfully linked to your Helena Express account." , "Account verified!" ) ;
2022-01-26 14:10:09 -07:00
sendActionReport ( "Account" , "Device linked OK" ) ;
2021-08-27 01:38:24 -06:00
displayAccountInfo ( ) ;
} else if ( resp . status == "ERROR" ) {
app . dialog . alert ( resp . msg , "Error" ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Device linking" , resp . msg ) ;
2021-08-27 01:38:24 -06:00
}
2022-01-26 14:10:09 -07:00
} , function ( xhr , status , error ) {
2021-08-27 01:38:24 -06:00
app . dialog . close ( ) ;
app . dialog . alert ( "There's a server or network problem. Check your Internet connection or try again later." , "Error" ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Couldn't verify account code" , "Server/network problem: " + xhr . status + ": " + xhr . statusText ) ;
2021-08-27 01:38:24 -06:00
} ) ;
}
function displayAccountInfo ( ) {
$ ( "#loyaltyBalanceBox" ) . addClass ( "display-none" ) ;
$ ( "#loyaltyErrorMessage" ) . html ( "" ) ;
2021-10-19 17:31:18 -06:00
if ( inStorage ( "accountkey" ) && inStorage ( "accountnumber" ) ) {
2021-08-27 01:38:24 -06:00
} else {
$ ( "#loyaltyErrorMessage" ) . text ( "Error: No account connected." ) ;
return ;
}
apirequest ( SETTINGS . apis . getaccountinfo , {
2021-10-19 17:31:18 -06:00
accountnumber : getStorage ( "accountnumber" ) ,
2021-08-27 01:38:24 -06:00
accountkey : getStorage ( "accountkey" )
} , function ( success ) {
$ ( "#hasaccountbox" ) . css ( "display" , "" ) ;
$ ( "#loadingaccountbox" ) . css ( "display" , "none" ) ;
if ( success . status == "OK" ) {
$ ( "#loyaltyCreditBalanceHeading" ) . text ( success . credits + " points" ) ;
2021-10-02 19:57:41 -06:00
$ ( "#loyaltyBalanceBox" ) . removeClass ( "display-none" )
2021-10-19 17:31:18 -06:00
$ ( "#accountnumberspan" ) . text ( success . accountnumber ) ;
2021-08-27 01:38:24 -06:00
if ( success . payments _setup === false ) {
$ ( "#addPaymentMethodBox" ) . css ( "display" , "" ) ;
2021-10-14 23:09:00 -06:00
} else {
$ ( "#addPaymentMethodBox" ) . css ( "display" , "none" ) ;
2021-08-27 01:38:24 -06:00
}
$ ( "#accountupdateform input#name" ) . val ( success . name ) ;
2021-10-19 17:31:18 -06:00
$ ( "#accountupdateform input#phone" ) . val ( success . phone ) ;
2021-08-27 01:38:24 -06:00
$ ( "#accountupdateform input#email" ) . val ( success . email ) ;
$ ( "#accountupdateform input#streetaddress" ) . val ( success . streetaddress ) ;
$ ( "#accountupdateform input#zipcode" ) . val ( success . zipcode ) ;
} else {
$ ( "#loyaltyBalanceBox" ) . addClass ( "display-none" ) ;
$ ( "#loyaltyErrorMessage" ) . text ( "Error: " + success . msg ) ;
}
2022-01-26 14:10:09 -07:00
} , function ( xhr , status , error ) {
2021-08-27 01:38:24 -06:00
$ ( "#loyaltyErrorMessage" ) . text ( "Error: Couldn't get your account info. Try again later." ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Couldn't display account info" , "Server/network problem: " + xhr . status + ": " + xhr . statusText ) ;
2021-08-27 01:38:24 -06:00
} , "GET" ) ;
}
2021-10-09 15:43:18 -06:00
$ ( "body" ) . on ( "click" , "#setupAccountBtn" , function ( ) {
2021-08-27 01:38:24 -06:00
if ( $ ( "#accountsetupform input#phonenumber" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your phone number." , "Error" ) ;
return ;
}
if ( $ ( "#accountsetupform input#name" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your name." , "Error" ) ;
return ;
}
if ( $ ( "#accountsetupform input#email" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your email address." , "Error" ) ;
return ;
}
if ( $ ( "#accountsetupform input#streetaddress" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your street address." , "Error" ) ;
return ;
}
if ( $ ( "#accountsetupform input#zipcode" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your ZIP Code." , "Error" ) ;
return ;
}
app . dialog . preloader ( "Creating Account..." ) ;
apirequest ( SETTINGS . apis . accountregister , {
phone : $ ( "#accountsetupform input#phonenumber" ) . val ( ) ,
name : $ ( "#accountsetupform input#name" ) . val ( ) ,
email : $ ( "#accountsetupform input#email" ) . val ( ) ,
address : $ ( "#accountsetupform input#streetaddress" ) . val ( ) ,
zipcode : $ ( "#accountsetupform input#zipcode" ) . val ( )
} , function ( resp ) {
app . dialog . close ( ) ;
if ( resp . status == "ERROR" ) {
app . dialog . alert ( resp . msg , "Error" ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Couldn't register account" , resp . msg ) ;
2021-08-27 01:38:24 -06:00
return ;
} else {
2021-10-19 17:31:18 -06:00
setStorage ( "accountnumber" , resp . accountnumber ) ;
2022-01-26 14:10:09 -07:00
sendActionReport ( "Account" , "Account created" ) ;
2021-08-27 01:38:24 -06:00
router . refreshPage ( ) ;
}
2022-01-26 14:10:09 -07:00
} , function ( xhr , status , error ) {
2021-08-27 01:38:24 -06:00
app . dialog . close ( ) ;
app . dialog . alert ( "There's a server or network problem. Check your Internet connection or try again later." , "Error" ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Couldn't register account" , "Server/network problem: " + xhr . status + ": " + xhr . statusText ) ;
2021-08-27 01:38:24 -06:00
} ) ;
} ) ;
2021-10-09 15:43:18 -06:00
$ ( "body" ) . on ( "click" , "#updateAccountBtn" , function ( ) {
2021-08-27 01:38:24 -06:00
if ( $ ( "#accountupdateform input#name" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your name." , "Error" ) ;
return ;
}
if ( $ ( "#accountupdateform input#email" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your email address." , "Error" ) ;
return ;
}
if ( $ ( "#accountupdateform input#streetaddress" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your street address." , "Error" ) ;
return ;
}
if ( $ ( "#accountupdateform input#zipcode" ) . val ( ) == "" ) {
app . dialog . alert ( "Add your ZIP Code." , "Error" ) ;
return ;
}
app . dialog . preloader ( "Updating Account..." ) ;
apirequest ( SETTINGS . apis . accountregister , {
2021-10-19 17:31:18 -06:00
accountnumber : getStorage ( "accountnumber" ) ,
2021-08-27 01:38:24 -06:00
accountkey : getStorage ( "accountkey" ) ,
name : $ ( "#accountupdateform input#name" ) . val ( ) ,
2021-10-19 17:31:18 -06:00
phone : $ ( "#accountupdateform input#phone" ) . val ( ) ,
2021-08-27 01:38:24 -06:00
email : $ ( "#accountupdateform input#email" ) . val ( ) ,
address : $ ( "#accountupdateform input#streetaddress" ) . val ( ) ,
zipcode : $ ( "#accountupdateform input#zipcode" ) . val ( )
} , function ( resp ) {
app . dialog . close ( ) ;
if ( resp . status == "ERROR" ) {
app . dialog . alert ( resp . msg , "Error" ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Couldn't update account" , resp . msg ) ;
2021-08-27 01:38:24 -06:00
return ;
} else {
app . popup . close ( "#accountUpdatePopup" , true ) ;
2021-10-19 17:31:18 -06:00
setStorage ( "accountnumber" , resp . accountnumber ) ;
2022-01-26 14:10:09 -07:00
sendActionReport ( "Account" , "Account updated" ) ;
2021-08-27 01:38:24 -06:00
router . refreshPage ( ) ;
app . dialog . alert ( "Account details updated." , "Account Updated" ) ;
}
2022-01-26 14:10:09 -07:00
} , function ( xhr , status , error ) {
2021-08-27 01:38:24 -06:00
app . dialog . close ( ) ;
app . dialog . alert ( "There's a server or network problem. Check your Internet connection or try again later." , "Error" ) ;
2022-01-26 14:10:09 -07:00
sendErrorReport ( "Account" , "Couldn't update account" , "Server/network problem: " + xhr . status + ": " + xhr . statusText ) ;
2021-08-27 01:38:24 -06:00
} ) ;
} ) ;
2021-10-09 15:43:18 -06:00
$ ( "body" ) . on ( "click" , "#connectExistingAccountBtn" , function ( ) {
2021-10-19 20:07:44 -06:00
app . dialog . prompt ( "Enter your account number:" , "Connect Your Account" , function ( val ) {
var accountnumber = val . replace ( /\D/g , '' ) ;
2021-10-19 17:31:18 -06:00
setStorage ( "accountnumber" , accountnumber ) ;
2021-08-27 01:38:24 -06:00
router . refreshPage ( ) ;
} , function ( cancel ) {
// shrug
} , "" ) ;
} ) ;
2021-10-02 14:34:53 -06:00
$ ( "body" ) . on ( "popup:open" , "#accountUpdatePopup" , function ( ) {
2021-08-27 01:38:24 -06:00
app . input . checkEmptyState ( "#accountupdateform input#name" ) ;
2021-10-19 17:31:18 -06:00
app . input . checkEmptyState ( "#accountupdateform input#phone" ) ;
2021-08-27 01:38:24 -06:00
app . input . checkEmptyState ( "#accountupdateform input#email" ) ;
app . input . checkEmptyState ( "#accountupdateform input#streetaddress" ) ;
app . input . checkEmptyState ( "#accountupdateform input#zipcode" ) ;
} ) ;
function initAccountPage ( ) {
checkAccountStatus ( function ( result , msg ) {
switch ( result ) {
case true :
displayAccountInfo ( ) ;
break ;
case false :
$ ( "#setupaccountbox" ) . css ( "display" , "" ) ;
$ ( "#loadingaccountbox" ) . css ( "display" , "none" ) ;
break ;
case "noauth" :
openAccountVerify ( msg ) ;
break ;
case "badstate" :
app . dialog . alert ( "Your account is in an unstable state. This shouldn't happen. Please contact us at (406) 389-8988 and we'll fix it." , "Error" ) ;
break ;
default :
app . dialog . alert ( "Something went very wrong. Close the app completely (swipe it away from the app switcher) and re-open it. If you continue to get this message, contact us for help: (406) 389-8988" , "Error" ) ;
break ;
}
} ) ;
2021-09-10 00:03:48 -06:00
}
2021-09-22 00:34:54 -06:00
function openCheckoutWindowToSaveCard ( onaccountpage ) {
if ( typeof onaccountpage == "undefined" ) {
onaccountpage = true ;
}
2021-09-10 00:03:48 -06:00
openBrowser ( SETTINGS . apis . redirecttopaymentsetup
2021-10-19 17:31:18 -06:00
+ "?accountnumber=" + getStorage ( "accountnumber" )
2021-09-10 00:03:48 -06:00
+ "&accountkey=" + getStorage ( "accountkey" ) ,
2021-09-17 12:30:46 -06:00
"location=yes,hidenavigationbuttons=yes,hideurlbar=yes,zoom=no,hardwareback=no,fullscreen=no,presentationstyle=pagesheet,toolbarposition=top,lefttoright=yes,toolbarcolor=#D0F2FC" ,
2021-09-10 00:49:55 -06:00
function ( ) {
// on exit browser
initAccountPage ( ) ;
} ,
function ( params ) {
// on get message from browser
// only message we should get is "kill me"
2021-09-23 10:49:07 -06:00
if ( params . data . msg == "kill me" ) {
2021-09-10 00:49:55 -06:00
closeBrowser ( ) ;
}
2021-10-14 23:09:00 -06:00
initAccountPage ( ) ;
2021-09-10 00:49:55 -06:00
}
) ;
2021-10-14 23:09:00 -06:00
// refresh a bit while user adds card
setTimeout ( initAccountPage , 1000 * 10 ) ;
setTimeout ( initAccountPage , 1000 * 20 ) ;
setTimeout ( initAccountPage , 1000 * 30 ) ;
setTimeout ( initAccountPage , 1000 * 40 ) ;
setTimeout ( initAccountPage , 1000 * 50 ) ;
setTimeout ( initAccountPage , 1000 * 60 ) ;
2022-01-26 14:10:09 -07:00
setTimeout ( initAccountPage , 1000 * 90 ) ;
setTimeout ( initAccountPage , 1000 * 120 ) ;
2022-02-01 19:52:22 -07:00
}
function loadReceiptHTMLIntoPopup ( receiptid ) {
app . dialog . preloader ( "Loading..." ) ;
apirequest ( SETTINGS . apis . getreceipt , {
accountnumber : getStorage ( "accountnumber" ) ,
accountkey : getStorage ( "accountkey" ) ,
receiptid : receiptid
} , function ( success ) {
app . dialog . close ( ) ;
if ( success . status == "OK" ) {
$ ( '#receiptvieweriframe' ) . attr ( "src" , "data:text/html," + encodeURIComponent ( success . receipt . html ) ) ;
app . popup . open ( "#receiptViewerPopup" ) ;
} else {
app . dialog . alert ( success . msg , "Error" ) ;
sendErrorReport ( "Receipts" , "Loading receipt" , success . msg ) ;
}
} , function ( xhr , status , error ) {
app . dialog . close ( ) ;
app . dialog . alert ( "There's a server or network problem. Check your Internet connection or try again later." , "Error" ) ;
sendErrorReport ( "Receipts" , "Couldn't load receipt" , "Server/network problem: " + xhr . status + ": " + xhr . statusText ) ;
} , "POST" ) ;
2021-08-27 01:38:24 -06:00
}