You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.5 KiB
PHP

<?php
/*
* 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/.
*/
\Stripe\Stripe::setApiKey($SETTINGS["stripe"]["seckey"]);
$accountid = $database->get("accounts", ["[>]authkeys" => ["accountid"]], "accounts.accountid", ["key" => $VARS["key"]]);
$token = $VARS['token'];
$amount = $VARS["amount"] * 100.00;
try {
$charge = \Stripe\Charge::create([
'amount' => $amount,
'currency' => 'usd',
'description' => 'Helping Helena',
'statement_descriptor' => "HelpingHelena",
'source' => $token
]);
$stripefees = 30 + ($amount * 0.029);
$totalfees = $amount * $SETTINGS["stripe"]["fee"];
$amountadded = ($amount - max($stripefees, $totalfees)) / 100.0;
$database->update("accounts", ["balance[+]" => $amountadded], ["accountid" => $accountid]);
$database->insert("log", [
"datetime" => date("Y-m-d H:i:s"),
"accountid" => $accountid,
"entry" => "Added funds with card: $" . ($amount / 100.0) . " charged, $$amountadded added to balance."
]);
exitWithJson([
"status" => "OK",
"charged_amount" => $amount / 100.0,
"final_amount" => $amountadded
]);
} catch (\Stripe\Error\Card $e) {
$body = $e->getJsonBody();
$err = $body['error'];
sendJsonResp("We couldn't process your card because it was declined. Your card issuer or bank sent us this message: " . $err["message"] . " That's all we know.", "ERROR");
} catch (\Stripe\Error\RateLimit $e) {
sendJsonResp("We couldn't process your card because things are happening too fast. Please try again in a minute. (Error code: STRIPE_RATELIMIT)", "ERROR");
} catch (\Stripe\Error\InvalidRequest $e) {
sendJsonResp("We couldn't process your card because of a technical issue. Please try again later. (Error code: STRIPE_INVREQ)", "ERROR");
} catch (\Stripe\Error\Authentication $e) {
sendJsonResp("We can't connect to the card processor. Please try again later. (Error code: STRIPE_AUTH)", "ERROR");
} catch (\Stripe\Error\ApiConnection $e) {
sendJsonResp("We can't connect to the card processor. Please try again later. (Error code: STRIPE_NOAPI)", "ERROR");
} catch (\Stripe\Error\Base $e) {
sendJsonResp("An unknown payment error occurred. Please try again later.", "ERROR");
} catch (Exception $e) {
sendJsonResp("An unknown error occurred. Please try again later.", "ERROR");
}