|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Client Portal |
| 4 | + * Auto-pay configuration for PTC/finance contacts |
| 5 | + */ |
| 6 | + |
| 7 | +require_once "inc_portal.php"; |
| 8 | + |
| 9 | +if ($session_contact_primary == 0 && !$session_contact_is_billing_contact) { |
| 10 | + header("Location: portal_post.php?logout"); |
| 11 | + exit(); |
| 12 | +} |
| 13 | + |
| 14 | +// Initialize stripe |
| 15 | +require_once '../vendor/stripe-php-10.5.0/init.php'; |
| 16 | + |
| 17 | +// Get Stripe vars |
| 18 | +$stripe_vars = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_stripe_enable, config_stripe_publishable, config_stripe_secret FROM settings WHERE company_id = 1")); |
| 19 | +$config_stripe_enable = intval($stripe_vars['config_stripe_enable']); |
| 20 | +$config_stripe_publishable = nullable_htmlentities($stripe_vars['config_stripe_publishable']); |
| 21 | +$config_stripe_secret = nullable_htmlentities($stripe_vars['config_stripe_secret']); |
| 22 | + |
| 23 | +// Get client's StripeID from database |
| 24 | +$stripe_client_details = mysqli_fetch_array(mysqli_query($mysqli, "SELECT * FROM client_stripe WHERE client_id = $session_client_id LIMIT 1")); |
| 25 | +if ($stripe_client_details) { |
| 26 | + $stripe_id = sanitizeInput($stripe_client_details['stripe_id']); |
| 27 | + $stripe_pm = sanitizeInput($stripe_client_details['stripe_pm']); |
| 28 | +} |
| 29 | + |
| 30 | +// Stripe not enabled in settings |
| 31 | +if (!$config_stripe_enable || !$config_stripe_publishable || !$config_stripe_secret) { |
| 32 | + echo "Stripe payment error - Stripe is not enabled, please talk to your helpdesk for further information."; |
| 33 | + include_once 'portal_footer.php'; |
| 34 | + exit(); |
| 35 | +} |
| 36 | + |
| 37 | +?> |
| 38 | + |
| 39 | + <h3>AutoPay</h3> |
| 40 | + <div class="row"> |
| 41 | + |
| 42 | + <div class="col-md-10"> |
| 43 | + |
| 44 | + <!-- Setup pt1: Stripe ID not found / auto-payment not configured --> |
| 45 | + <?php if (!$stripe_client_details || empty($stripe_id)) { ?> |
| 46 | + |
| 47 | + <b>Save card details</b><br> |
| 48 | + In order to set up automatic payments, you must create a customer record in Stripe.<br> |
| 49 | + First, you must authorize Stripe to store your card details for the purpose of automatic payment. |
| 50 | + <br><br> |
| 51 | + |
| 52 | + <div class="col-5"> |
| 53 | + <form action="portal_post.php" method="POST"> |
| 54 | + |
| 55 | + <div class="form-group"> |
| 56 | + <div class="custom-control custom-checkbox"> |
| 57 | + <input class="custom-control-input" type="checkbox" id="consent" name="consent" value="1" required> |
| 58 | + <label for="consent" class="custom-control-label"> |
| 59 | + I grant consent for automatic payments |
| 60 | + </label> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + |
| 64 | + <div class="form-group"> |
| 65 | + <button type="submit" class="form-control btn-success" name="create_stripe_customer">Create Stripe Customer Record</button> |
| 66 | + </div> |
| 67 | + </form> |
| 68 | + </div> |
| 69 | + |
| 70 | + <?php } |
| 71 | + |
| 72 | + // Setup pt2: Stripe ID found / payment may be configured --> |
| 73 | + elseif (empty($stripe_pm)) { ?> |
| 74 | + |
| 75 | + <b>Save card details</b><br> |
| 76 | + Please add the payment details you would like to save.<br> |
| 77 | + By adding payment details here, you grant consent for future automatic payments of invoices.<br><br> |
| 78 | + |
| 79 | + <script src="https://js.stripe.com/v3/"></script> |
| 80 | + <script src="../js/autopay_setup_stripe.js"></script> |
| 81 | + <input type="hidden" id="stripe_publishable_key" value="<?php echo $config_stripe_publishable ?>"> |
| 82 | + <div id="checkout"> |
| 83 | + <!-- Checkout will insert the payment form here --> |
| 84 | + </div> |
| 85 | + |
| 86 | + <?php } |
| 87 | + |
| 88 | + // Manage the saved card |
| 89 | + else { ?> |
| 90 | + |
| 91 | + <b>Manage saved card details</b> |
| 92 | + |
| 93 | + <?php |
| 94 | + |
| 95 | + try { |
| 96 | + // Initialize |
| 97 | + $stripe = new \Stripe\StripeClient($config_stripe_secret); |
| 98 | + |
| 99 | + // Get payment method info (last 4 digits etc) |
| 100 | + $payment_method = $stripe->customers->retrievePaymentMethod( |
| 101 | + $stripe_id, |
| 102 | + $stripe_pm, |
| 103 | + [] |
| 104 | + ); |
| 105 | + |
| 106 | + } catch (Exception $e) { |
| 107 | + $error = $e->getMessage(); |
| 108 | + error_log("Stripe payment error - encountered exception when fetching payment method info for $stripe_pm: $error"); |
| 109 | + logApp("Stripe", "error", "Exception when fetching payment method info for $stripe_pm: $error"); |
| 110 | + } |
| 111 | + |
| 112 | + $card_name = nullable_htmlentities($payment_method->billing_details->name); |
| 113 | + $card_brand = nullable_htmlentities($payment_method->card->display_brand); |
| 114 | + $card_last4 = nullable_htmlentities($payment_method->card->last4); |
| 115 | + $card_expires = nullable_htmlentities($payment_method->card->exp_month) . "/" . nullable_htmlentities($payment_method->card->exp_year); |
| 116 | + |
| 117 | + ?> |
| 118 | + |
| 119 | + <ul><li><?php echo "$card_name - $card_brand card ending in $card_last4, expires $card_expires"; ?></li></ul> |
| 120 | + |
| 121 | + <hr> |
| 122 | + <b>Actions</b><br> |
| 123 | + - <a href="portal_post.php?stripe_remove_card&pm=<?php echo $stripe_pm; ?>">Remove saved card</a> |
| 124 | + |
| 125 | + <?php } ?> |
| 126 | + |
| 127 | + |
| 128 | + </div> |
| 129 | + |
| 130 | + </div> |
| 131 | + |
| 132 | + |
| 133 | +<?php |
| 134 | +require_once "portal_footer.php"; |
0 commit comments