added on local at 2026-07-01 13:47:43
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # AffSoft - Stripe Connect OAuth pair. | |
| 4 | # | |
| 5 | # Consolidates former stripe_connect_callback.cgi into this file via | |
| 6 | # SCRIPT_NAME dispatch: | |
| 7 | # /stripe_connect.cgi -> initiator (create acct + link, 302 to Stripe) | |
| 8 | # /stripe_connect_callback.cgi -> return URL (re-fetch acct, mark onboarded) | |
| 9 | # | |
| 10 | # stripe_webhook.cgi is intentionally NOT merged: it depends on | |
| 11 | # MODS::AffSoft::SignedDownload (missing at the time of this pass; | |
| 12 | # perl -c currently fails). Once that module lands, webhook can be | |
| 13 | # added as another entry here. | |
| 14 | #====================================================================== | |
| 15 | use strict; | |
| 16 | use warnings; | |
| 17 | ||
| 18 | use lib '/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com'; | |
| 19 | use CGI; | |
| 20 | use MODS::DBConnect; | |
| 21 | use MODS::Login; | |
| 22 | use MODS::AffSoft::Config; | |
| 23 | use MODS::AffSoft::Stripe; | |
| 24 | ||
| 25 | $| = 1; | |
| 26 | ||
| 27 | my $q = CGI->new; | |
| 28 | my $form = $q->Vars; | |
| 29 | my $auth = MODS::Login->new; | |
| 30 | my $db = MODS::DBConnect->new; | |
| 31 | my $cfg = MODS::AffSoft::Config->new; | |
| 32 | my $DB = $cfg->settings('database_name'); | |
| 33 | ||
| 34 | my $entry = ($ENV{SCRIPT_NAME} || '') =~ m{/([^/]+)\.cgi$} ? $1 : 'stripe_connect'; | |
| 35 | ||
| 36 | my $userinfo = $auth->login_verify(); | |
| 37 | unless ($userinfo) { | |
| 38 | print "Status: 302 Found\nLocation: /login.cgi\n\n"; | |
| 39 | exit; | |
| 40 | } | |
| 41 | require MODS::AffSoft::Permissions; | |
| 42 | MODS::AffSoft::Permissions->new->require_owner($userinfo); | |
| 43 | my $uid = $userinfo->{user_id}; | |
| 44 | $uid =~ s/[^0-9]//g; | |
| 45 | ||
| 46 | my $sid = $form->{storefront_id} || $form->{id} || 0; | |
| 47 | $sid =~ s/[^0-9]//g; | |
| 48 | ||
| 49 | if ($entry eq 'stripe_connect_callback') { | |
| 50 | _stc_callback(); | |
| 51 | } else { | |
| 52 | _stc_initiate(); | |
| 53 | } | |
| 54 | exit; | |
| 55 | ||
| 56 | #====================================================================== | |
| 57 | # /stripe_connect.cgi entry: create Stripe Express account if needed, | |
| 58 | # then create an account link and 302 the user to Stripe onboarding. | |
| 59 | #====================================================================== | |
| 60 | sub _stc_initiate { | |
| 61 | my $stripe = MODS::AffSoft::Stripe->new; | |
| 62 | unless ($stripe->is_configured) { | |
| 63 | _stc_bail('Stripe is not configured on this server yet. Add keys to /private/stripe.conf.'); | |
| 64 | } | |
| 65 | ||
| 66 | my $dbh = $db->db_connect(); | |
| 67 | my $store = $db->db_readwrite($dbh, qq~ | |
| 68 | SELECT id, stripe_account_id | |
| 69 | FROM `${DB}`.storefronts | |
| 70 | WHERE id='$sid' AND user_id='$uid' | |
| 71 | LIMIT 1 | |
| 72 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 73 | unless ($store && $store->{id}) { | |
| 74 | $db->db_disconnect($dbh); | |
| 75 | _stc_bail('Storefront not found.'); | |
| 76 | } | |
| 77 | ||
| 78 | my $account_id = $store->{stripe_account_id} || ''; | |
| 79 | ||
| 80 | # Create the connected account on first run. | |
| 81 | if (!$account_id) { | |
| 82 | my $email = $userinfo->{email} || ''; | |
| 83 | my $acct = $stripe->create_express_account( | |
| 84 | email => $email, | |
| 85 | metadata => { | |
| 86 | webstls_user_id => $uid, | |
| 87 | webstls_storefront_id => $sid, | |
| 88 | }, | |
| 89 | ); | |
| 90 | if ($acct->{error} || !$acct->{id}) { | |
| 91 | $db->db_disconnect($dbh); | |
| 92 | _stc_bail('Stripe account create failed: ' . ($acct->{error} || 'unknown')); | |
| 93 | } | |
| 94 | $account_id = $acct->{id}; | |
| 95 | $account_id =~ s/'/''/g; | |
| 96 | $db->db_readwrite($dbh, qq~ | |
| 97 | UPDATE `${DB}`.storefronts | |
| 98 | SET stripe_account_id='$account_id' | |
| 99 | WHERE id='$sid' AND user_id='$uid' | |
| 100 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 101 | } | |
| 102 | ||
| 103 | $db->db_disconnect($dbh); | |
| 104 | ||
| 105 | # Build absolute return URLs. Plesk fronts everything with HTTPS so | |
| 106 | # we hard-prefix https:// rather than reading $ENV{HTTPS}. | |
| 107 | my $host = $ENV{HTTP_HOST} || 'webstls.com'; | |
| 108 | my $base = "https://$host"; | |
| 109 | my $link = $stripe->create_account_link( | |
| 110 | account => $account_id, | |
| 111 | refresh_url => "$base/stripe_connect.cgi?storefront_id=$sid", | |
| 112 | return_url => "$base/stripe_connect_callback.cgi?storefront_id=$sid", | |
| 113 | ); | |
| 114 | if ($link->{error} || !$link->{url}) { | |
| 115 | _stc_bail('Stripe account link failed: ' . ($link->{error} || 'unknown')); | |
| 116 | } | |
| 117 | ||
| 118 | print "Status: 302 Found\nLocation: " . $link->{url} . "\n\n"; | |
| 119 | } | |
| 120 | ||
| 121 | #====================================================================== | |
| 122 | # /stripe_connect_callback.cgi entry: Stripe return URL. Refetch the | |
| 123 | # account state; if charges_enabled + details_submitted, stamp | |
| 124 | # stripe_onboarded_at. | |
| 125 | #====================================================================== | |
| 126 | sub _stc_callback { | |
| 127 | my $dbh = $db->db_connect(); | |
| 128 | my $store = $db->db_readwrite($dbh, qq~ | |
| 129 | SELECT id, stripe_account_id | |
| 130 | FROM `${DB}`.storefronts | |
| 131 | WHERE id='$sid' AND user_id='$uid' | |
| 132 | LIMIT 1 | |
| 133 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 134 | ||
| 135 | my $msg = ''; | |
| 136 | if ($store && $store->{stripe_account_id}) { | |
| 137 | my $stripe = MODS::AffSoft::Stripe->new; | |
| 138 | if ($stripe->is_configured) { | |
| 139 | my $acct = $stripe->retrieve_account($store->{stripe_account_id}); | |
| 140 | if (!$acct->{error} && $acct->{charges_enabled} && $acct->{details_submitted}) { | |
| 141 | $db->db_readwrite($dbh, qq~ | |
| 142 | UPDATE `${DB}`.storefronts | |
| 143 | SET stripe_onboarded_at=NOW() | |
| 144 | WHERE id='$sid' AND user_id='$uid' | |
| 145 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 146 | $msg = 'stripe_connected=1'; | |
| 147 | } else { | |
| 148 | $msg = 'stripe_pending=1'; | |
| 149 | } | |
| 150 | } | |
| 151 | } | |
| 152 | $db->db_disconnect($dbh); | |
| 153 | ||
| 154 | print "Status: 302 Found\nLocation: /storefront.cgi?$msg\n\n"; | |
| 155 | } | |
| 156 | ||
| 157 | sub _stc_bail { | |
| 158 | my ($msg) = @_; | |
| 159 | print "Status: 302 Found\nLocation: /storefront.cgi?stripe_error=" . _stc_urlenc($msg) . "\n\n"; | |
| 160 | exit; | |
| 161 | } | |
| 162 | sub _stc_urlenc { | |
| 163 | my $s = shift // ''; | |
| 164 | $s =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge; | |
| 165 | return $s; | |
| 166 | } |