Browse Source

Merge pull request 'Merge API into main' (#26) from api into main

Reviewed-on: https://git.socialistra.org/socialist-rifle-association/website/pulls/26
pull/28/head
parent
commit
43f03f9e78
4 changed files with 112 additions and 7 deletions
  1. +95
    -0
      api/state/chapters/index.php
  2. +4
    -4
      donate/capture.php
  3. +1
    -1
      donate/index.php
  4. +12
    -2
      res/php/payment/charge.php

+ 95
- 0
api/state/chapters/index.php View File

@ -0,0 +1,95 @@
<?php
// Copyright (C) 2021 Aayla
//
// This file is part of website.
//
// website is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// website is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with website. If not, see <http://www.gnu.org/licenses/>.
// Array of state abbreviations
$states = [
"Alabama" => "AL",
"Alaska" => "AK",
"Arizona" => "AZ",
"Arkansas" => "AR",
"California" => "CA",
"Colorado" => "CO",
"Connecticut" => "CT",
"Delaware" => "DE",
"Florida" => "FL",
"Georgia" => "GA",
"Hawaii" => "HI",
"Idaho" => "ID",
"Illinois" => "IL",
"Indiana" => "IN",
"Iowa" => "IA",
"Kansas" => "KS",
"Kentucky" => "KY",
"Louisiana" => "LA",
"Maine" => "ME",
"Maryland" => "MD",
"Massachusetts" => "MA",
"Michigan" => "MI",
"Minnesota" => "MN",
"Mississippi" => "MS",
"Missouri" => "MO",
"Montana" => "MT",
"Nebraska" => "NE",
"Nevada" => "NV",
"New Hampshire" => "NH",
"New Jersey" => "NJ",
"New Mexico" => "NM",
"New York" => "NY",
"North Carolina" => "NC",
"North Dakota" => "ND",
"Ohio" => "OH",
"Oklahoma" => "OK",
"Oregon" => "OR",
"Pennsylvania" => "PA",
"Rhode Island" => "RI",
"South Carolina" => "SC",
"South Dakota" => "SD",
"Tennessee" => "TN",
"Texas" => "TX",
"Utah" => "UT",
"Vermont" => "VT",
"Virginia" => "VA",
"Washington" => "WA",
"West Virginia" => "WV",
"Wisconsin" => "WI",
"Wyoming" => "WY",
"District of Columbia" => "DC",
"Guam" => "GU",
"Marhsall Islands" => "MH",
"Puerto Rico" => "PR",
"Virgin Islands" => "VI"
];
// Ensure required parameters are present
if (!isset($_GET["state"])) {
http_response_code(404);
die();
}
if (!array_key_exists($_GET["state"], $states)) {
http_response_code(404);
die();
}
$chapters = json_decode(file_get_contents("../../../chapters/chapters.json"), true);
$state = $states[$_GET["state"]];
header('Content-Type: application/json; charset=utf-8');
echo json_encode($chapters[$state]);
?>

+ 4
- 4
donate/capture.php View File

@ -78,17 +78,17 @@ if (!in_array($recipient, $recipientsList)) {
* adding metadata field with chapter and potentially memo per stripe api
* https://stripe.com/docs/api/metadata
*/
$metadata = array("chapter" => $recipient)
$metadata = array("chapter" => $recipient);
if(isset($_POST["memo"]) {
$metadata = array_push($metadata, "memo" => htmlspecialchars($_POST["memo"]));
if (isset($_POST["memo"])) {
$metadata = array_push($metadata, htmlspecialchars($_POST["memo"]));
}
// Load charge() function from charge.php
require "../res/php/payment/charge.php";
// Attempt charge
$response = charge($amount, $_POST['stripeToken'], 'Donation - ' . $recipient, $email);
$response = charge($amount, $_POST['stripeToken'], 'Donation - ' . $recipient, $metadata, $email);
// Redirect to donation page with response
header("Location: /donate?error=". $response); // Redirect browser for success
exit();

+ 1
- 1
donate/index.php View File

@ -403,7 +403,7 @@ if (isset($error)) {
<?php include("../res/php/bottom.php") ?>
<script type="text/javascript">
// Create a Stripe client.
var stripe = Stripe('pk_live_BZ1zqXFctBlrARB6AsTxI6k6');
var stripe = Stripe('<?php echo(getenv("STRIPE_PUBLIC_KEY")) ?>');
// Create an instance of Elements.
var elements = stripe.elements();


+ 12
- 2
res/php/payment/charge.php View File

@ -31,15 +31,25 @@ use Mailgun\Mailgun;
* $metadata: array of metadata that presently includes chapter name and if given, memo.
*/
function charge($amount, $token, $description, $metadata, $email) {
$stripe_private_key = getenv('STRIPE_PRIVATE_KEY');
// Ensure environment variable is defined
if ($stripe_private_key == false) {
die("Stripe Private Key is not set!");
}
// echo $stripe_private_key;
// Create Stripe Client
$stripe = new StripeClient(getenv('STRIPE_SECRET_KEY'));
$stripe = new StripeClient(strval($stripe_private_key));
// echo "Created Stripe Client";
// Create the charge
$response = $stripe->charges->create([
"amount" => $amount,
"currency" => "usd",
"source" => $token,
"description" => $description,
"metadata" => $metadata,
// "metadata" => $metadata, // This breaks things. I don't know why.
"receipt_email" => $email
]);
return $response->status;

Loading…
Cancel
Save