Diff -- /var/www/vhosts/3dshawn.com/shop.3dshawn.com/stripe_connect.cgi
Diff

/var/www/vhosts/3dshawn.com/shop.3dshawn.com/stripe_connect.cgi

added on local at 2026-07-01 22:09:46

Added
+158
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 6e7f0f237fd0
Restore this content →
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.
1#!/usr/bin/perl
2#======================================================================
3# ShopCart - Stripe Connect: seller-side onboarding.
4#
5# Consolidates the OAuth pair via SCRIPT_NAME dispatch:
6# stripe_connect.cgi -- initiator (primary)
7# stripe_connect_callback.cgi -- Stripe return_url after onboarding
8#
9# Both entries require the seller to be logged in as the storefront's
10# owner (require_owner). The callback re-fetches the account from
11# Stripe to see if charges_enabled/details_submitted flipped true and
12# stamps stripe_onboarded_at accordingly.
13#======================================================================
14use strict;
15use warnings;
16
17use lib '/var/www/vhosts/3dshawn.com/shop.3dshawn.com';
18use CGI;
19use MODS::DBConnect;
20use MODS::Login;
21use MODS::ShopCart::Config;
22use MODS::ShopCart::Stripe;
23
24$| = 1;
25
26my $q = CGI->new;
27my $form = $q->Vars;
28my $auth = MODS::Login->new;
29my $db = MODS::DBConnect->new;
30my $cfg = MODS::ShopCart::Config->new;
31my $DB = $cfg->settings('database_name');
32
33my $userinfo = $auth->login_verify();
34unless ($userinfo) {
35 print "Status: 302 Found\nLocation: /login.cgi\n\n";
36 exit;
37}
38require MODS::ShopCart::Permissions;
39MODS::ShopCart::Permissions->new->require_owner($userinfo);
40my $uid = $userinfo->{user_id};
41$uid =~ s/[^0-9]//g;
42
43my $sid = $form->{storefront_id} || $form->{id} || 0;
44$sid =~ s/[^0-9]//g;
45
46my $entry = ($ENV{SCRIPT_NAME} || '') =~ m{/([^/]+)\.cgi$} ? $1 : 'stripe_connect';
47
48if ($entry eq 'stripe_connect_callback') { _stc_callback(); exit; }
49_stc_initiate();
50exit;
51
52#======================================================================
53# stripe_connect.cgi -- create Express account + account link -> 302
54#======================================================================
55sub _stc_initiate {
56 my $stripe = MODS::ShopCart::Stripe->new;
57 unless ($stripe->is_configured) {
58 _stc_bail('Stripe is not configured on this server yet. Add keys to /private/stripe.conf.');
59 }
60
61 my $dbh = $db->db_connect();
62 my $store = $db->db_readwrite($dbh, qq~
63 SELECT id, stripe_account_id
64 FROM `${DB}`.storefronts
65 WHERE id='$sid' AND user_id='$uid'
66 LIMIT 1
67 ~, $ENV{SCRIPT_NAME}, __LINE__);
68 unless ($store && $store->{id}) {
69 $db->db_disconnect($dbh);
70 _stc_bail('Storefront not found.');
71 }
72
73 my $account_id = $store->{stripe_account_id} || '';
74
75 if (!$account_id) {
76 my $email = $userinfo->{email} || '';
77 my $acct = $stripe->create_express_account(
78 email => $email,
79 metadata => {
80 shopcart_user_id => $uid,
81 shopcart_storefront_id => $sid,
82 },
83 );
84 if ($acct->{error} || !$acct->{id}) {
85 $db->db_disconnect($dbh);
86 _stc_bail('Stripe account create failed: ' . ($acct->{error} || 'unknown'));
87 }
88 $account_id = $acct->{id};
89 $account_id =~ s/'/''/g;
90 $db->db_readwrite($dbh, qq~
91 UPDATE `${DB}`.storefronts
92 SET stripe_account_id='$account_id'
93 WHERE id='$sid' AND user_id='$uid'
94 ~, $ENV{SCRIPT_NAME}, __LINE__);
95 }
96
97 $db->db_disconnect($dbh);
98
99 my $host = $ENV{HTTP_HOST} || 'shop.3dshawn.com';
100 my $base = "https://$host";
101 my $link = $stripe->create_account_link(
102 account => $account_id,
103 refresh_url => "$base/stripe_connect.cgi?storefront_id=$sid",
104 return_url => "$base/stripe_connect_callback.cgi?storefront_id=$sid",
105 );
106 if ($link->{error} || !$link->{url}) {
107 _stc_bail('Stripe account link failed: ' . ($link->{error} || 'unknown'));
108 }
109
110 print "Status: 302 Found\nLocation: " . $link->{url} . "\n\n";
111 return;
112}
113
114#======================================================================
115# stripe_connect_callback.cgi -- Stripe return_url after onboarding
116#======================================================================
117sub _stc_callback {
118 my $dbh = $db->db_connect();
119 my $store = $db->db_readwrite($dbh, qq~
120 SELECT id, stripe_account_id
121 FROM `${DB}`.storefronts
122 WHERE id='$sid' AND user_id='$uid'
123 LIMIT 1
124 ~, $ENV{SCRIPT_NAME}, __LINE__);
125
126 my $msg = '';
127 if ($store && $store->{stripe_account_id}) {
128 my $stripe = MODS::ShopCart::Stripe->new;
129 if ($stripe->is_configured) {
130 my $acct = $stripe->retrieve_account($store->{stripe_account_id});
131 if (!$acct->{error} && $acct->{charges_enabled} && $acct->{details_submitted}) {
132 $db->db_readwrite($dbh, qq~
133 UPDATE `${DB}`.storefronts
134 SET stripe_onboarded_at=NOW()
135 WHERE id='$sid' AND user_id='$uid'
136 ~, $ENV{SCRIPT_NAME}, __LINE__);
137 $msg = 'stripe_connected=1';
138 } else {
139 $msg = 'stripe_pending=1';
140 }
141 }
142 }
143 $db->db_disconnect($dbh);
144
145 print "Status: 302 Found\nLocation: /storefront.cgi?$msg\n\n";
146 return;
147}
148
149sub _stc_bail {
150 my ($msg) = @_;
151 print "Status: 302 Found\nLocation: /storefront.cgi?stripe_error=" . _stc_urlenc($msg) . "\n\n";
152 exit;
153}
154sub _stc_urlenc {
155 my $s = shift // '';
156 $s =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
157 return $s;
158}
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help