G1sms/shell/sms_PAY.sh

107 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
################################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.1
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
################################################################################
source ./shell/init.sh
source ./shell/functions.sh
log "__SUB:sms_PAY.sh: START ($1=PHONE, $2=PHONEDEST, $3=AMOUNT)"
phone="$1"
PHONEDEST="$2"
# Initialise PHONE, PIN, PUBKEY, UNIT
sms_INIT_ACCOUNT "$phone" "NOSMS"
pin=$PIN
uidna=$UIDNA
if [[ $UNKNOWN == "unknown" ]]; then
sms_ERROR "$phone" "Porte-monnaie inconnu. Envoyez N (suivi de votre Pseudo membre) pour le créer."
exit
else
log "PIN: $pin"
fi
###########################################################
# CALCULATE if wallet have enough for VIR+PERCENT
AMOUNT="$3"
accounting=($(make_accounting))
log "__SUB:sms_PAY.sh: $(declare -p accounting)"
testmin=${accounting[0]}
VIR=${accounting[1]}
PERCENT=${accounting[2]}
if [[ $testmin -eq 0 ]]; then
sms_ERROR "$phone" "Solde de votre Porte-monnaie G1sms insuffisant. Minimum: $((VIR+PERCENT+LIMIT)) G1! Rechargez avec https://Cesium.app"
exit
fi
# Add COUNTRY code to PHONEDEST + PHONEDEST INIT
PHONEDEST="$COUNTRY${PHONEDEST:1:10}"
HASHLINKDEST=$(echo -n $PHONEDEST | sha256sum | cut -d ' ' -f 1)
# CHECK if PHONEDEST have an account in IPFS G1sms+ SWARM
if [[ -f "./wallets_swarm/PHONE/$HASHLINKDEST/_pub" ]]
then
PUBKEYDEST=$(cat "./wallets_swarm/PHONE/$HASHLINKDEST/_pub")
UNITDEST=$(cat "./wallets_swarm/PHONE/$HASHLINKDEST/_unit")
log "__SUB:sms_PAY.sh: PUBKEYDEST found in swarn... $PUBKEYDEST"
else
# NO, then create NEW Wallet
log "__SUB:sms_PAY.sh: Creating Account for... $PHONEDEST"
sms_INIT_ACCOUNT "$PHONEDEST"
# Refreshed new values
PUBKEYDEST="$PUBKEY"
UNITDEST="$UNIT"
fi
# Payement
log "./silkaj/silkaj transaction --auth-scrypt -salt="$uidna" -password="$pin" --amount="$VIR" --output="$PUBKEYDEST""
PAY=$(./shell/timeout.sh -t 30 ./silkaj/silkaj transaction --auth-scrypt -salt="$uidna" -password="$pin" --amount="$VIR" --output="$PUBKEYDEST" --comment="[G1sms+] PAY $AMOUNT $UNIT" -y)
if [[ "$(echo $PAY | cut -d '|' -f 1)" == "KO" || "$PAY" == "" ]]; then
sms_ERROR "$phone" "PIN : $PAY ? Voyez vous votre code secret: $pin?
NON, Reinitaliser Compte. Envoyer: RAZ
OUI, Bourage Blockchain. Envoyer: S";
log "__SUB:sms_PAY.sh: Problème de payement avec silkaj : $PAY"
exit
else
# OK: Sync g1cents TODO check move_g1cents function and generalize on all silkaj transactions
cents=$(echo $(bc -l <<< "scale=0; $VIR * 100") | cut -d '.' -f 1)
move_g1cents "$phone" "$PUBKEYDEST" "$cents"
fi
sleep 2
# + G1SMS Commission
log "./silkaj/silkaj transaction --auth-scrypt -salt="$uidna" -password="$pin" --amount="$PERCENT" --output="$NODE_G1PUBKEY""
COM=$(./shell/timeout.sh -t 30 ./silkaj/silkaj transaction --auth-scrypt -salt="$uidna" -password="$pin" --amount="$PERCENT" --output="$NODE_G1PUBKEY" --comment="[G1sms+] P Commission" -y)
if [[ "$(echo $COM | cut -d '|' -f 1)" == "KO" || "$COM" == "" ]]; then
sms_ERROR "$phone" "Problème de bourrage blockchain : $COM";
log "__SUB:sms_PAY.sh: Problème de payement avec silkaj : $COM"
exit
else
# OK: Sync g1cents
cents=$(echo $(bc -l <<< "scale=0; $PERCENT * 100") | cut -d '.' -f 1)
move_g1cents "$phone" "$NODE_G1PUBKEY" "$cents"
fi
# LOG ACCOUNT HISTORY EVENTS
log "__SUB:sms_PAY.sh: $1 => $2 = $3 $UNIT / SILKAJ == TxPay $PAY + TxCom $COM /"
log_history $phone "PAY, $VIR, $PHONEDEST, $PUBKEYDEST"
# Send response SMS
mess_src="[G1sms+]
Envoi de $VIR G1 vers $PHONEDEST effectué!
+ Commission: $PERCENT G1"
sms_SEND "$phone" "$mess_src"
# Send dest SMS
mess_dest="[G1sms+] Bonjour.
Un(e) ami(e) ($phone) vient de vous envoyer $VIR G1 sur votre portefeuille G1sms!
Envoyez D pour (D)étail.
A pour (A)ide..."
sms_SEND "$PHONEDEST" "$mess_dest"
log "__SUB:sms_PAY.sh: END ~~~~~~~~~~~~~~~~~~~~~~~~~~ "
exit