Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into sync
Browse files Browse the repository at this point in the history
# Conflicts:
#	example/server/src/index.ts
  • Loading branch information
jonasbark committed Oct 16, 2023
2 parents a0a920e + cbb6aeb commit c1594ce
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 1 deletion.
37 changes: 36 additions & 1 deletion example/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,14 +712,20 @@ app.post('/payment-intent-for-payment-sheet', async (req, res) => {
}
});

app.post('/customer-sheet', async (_, res) => {
app.post('/create-checkout-session', async (req, res) => {
console.log(`Called /create-checkout-session`)
const {
port,
}: { port?: string; } = req.body;

const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2023-08-16',
typescript: true,
});

var effectivePort = port ?? 8080;
// Use an existing Customer ID if this is a returning customer.
const customer = await stripe.customers.create();

Expand Down Expand Up @@ -801,10 +807,39 @@ app.post('/set-payment-option', async (req, res) => {
});

app.post('/get-payment-option', async (req, res) => {

const { secret_key } = getKeys();

const stripe = new Stripe(secret_key as string, {
apiVersion: '2023-08-16',
typescript: true,
});

const customerPaymentOption = savedPaymentOptions.get(req.body.customerId);
res.json({
savedPaymentOption: customerPaymentOption ?? null,
});
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'Stubborn Attachments',
images: ['https://i.imgur.com/EHyR2nP.png'],
},
unit_amount: 2000,
},
quantity: 1,
},
],
mode: 'payment',
success_url: `https://checkout.stripe.dev/success`,
cancel_url: `https://checkout.stripe.dev/cancel`,

});
return res.json({ id: session.id });
});

app.listen(4242, (): void =>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ enum PaymentIntentsStatus {
/// with 3D Secure, the PaymentIntent has a status of requires_action.
requiresAction,

/// If you’re separately authorising and capturing funds, your PaymentIntent
/// can instead move to requires_capture. In that case, attempting to capture
/// the funds moves it to processing.
requiresCapture,

/// Once required actions are handled, the PaymentIntent moves to processing.
/// While for some payment methods (for example, cards) processing can be
/// quick, other types of payment methods can take up to a few days to process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,117 @@ void main() {
),
);
});

test('parses intent with canceled status', () {
expect(PaymentIntent.fromJson(_paymentIntentWithStatusJson('canceled')),
_expectedPaymentIntentWithStatus(PaymentIntentsStatus.canceled));
});

test('parses intent with processing status', () {
expect(PaymentIntent.fromJson(_paymentIntentWithStatusJson('processing')),
_expectedPaymentIntentWithStatus(PaymentIntentsStatus.processing));
});

test('parses intent with requires_action status', () {
expect(
PaymentIntent.fromJson(
_paymentIntentWithStatusJson('requires_action')),
_expectedPaymentIntentWithStatus(
PaymentIntentsStatus.requiresAction));
});

test('parses intent with requires_capture status', () {
expect(
PaymentIntent.fromJson(
_paymentIntentWithStatusJson('requires_capture')),
_expectedPaymentIntentWithStatus(
PaymentIntentsStatus.requiresCapture));
});

test('parses intent with requires_confirmation status', () {
expect(
PaymentIntent.fromJson(
_paymentIntentWithStatusJson('requires_confirmation')),
_expectedPaymentIntentWithStatus(
PaymentIntentsStatus.requiresConfirmation));
});

test('parses intent with requires_payment_method status', () {
expect(
PaymentIntent.fromJson(
_paymentIntentWithStatusJson('requires_payment_method')),
_expectedPaymentIntentWithStatus(
PaymentIntentsStatus.requiresPaymentMethod));
});

test('parses intent with succeeded status', () {
expect(PaymentIntent.fromJson(_paymentIntentWithStatusJson('succeeded')),
_expectedPaymentIntentWithStatus(PaymentIntentsStatus.succeeded));
});
});
}

Map<String, dynamic> _paymentIntentWithStatusJson(String status) {
return {
"id": "pi_1Dsr1T2eZvKYlo2C7KHhBRmV",
"object": "payment_intent",
"amount": 100,
"amount_capturable": 0,
"amount_details": {"tip": {}},
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"client_secret":
"pi_1Dsr1T2eZvKYlo2C7KHhBRmV_secret_qH4mhHMXKbJjDgyVuBao7MkBq",
"confirmation_method": "automatic",
"created": 1547555283,
"currency": "usd",
"customer": null,
"description": null,
"invoice": null,
"last_payment_error": null,
"latest_charge": null,
"livemode": false,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {},
"payment_method_types": ["card"],
"processing": null,
"receipt_email": null,
"redaction": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": status,
"transfer_data": null,
"transfer_group": null
};
}

PaymentIntent _expectedPaymentIntentWithStatus(PaymentIntentsStatus status) {
return PaymentIntent(
id: "pi_1Dsr1T2eZvKYlo2C7KHhBRmV",
amount: 100,
amountCapturable: 0,

Check warning on line 218 in packages/stripe_js/test/src/api/payment_intents/payment_intent_test.dart

View workflow job for this annotation

GitHub Actions / Typo CI

amountCapturable

"amountCapturable" is a typo. Did you mean "amountTriturable"?
amountDetails: PaymentIntentAmountDetails(
tip: PaymentIntentTip(),
),
amountReceived: 0,
clientSecret:
"pi_1Dsr1T2eZvKYlo2C7KHhBRmV_secret_qH4mhHMXKbJjDgyVuBao7MkBq",
currency: "usd",
created: 1547555283,
livemode: false,
paymentMethodTypes: [
PaymentMethodType.card,
],
status: status);
}
10 changes: 10 additions & 0 deletions packages/stripe_web/lib/src/parser/payment_intent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,35 @@ extension PaymentIntentsStatusExtension on PaymentIntentsStatus {
case 'Succeeded':
case 'succeeded':
return PaymentIntentsStatus.Succeeded;

case 'RequiresPaymentMethod':
case 'requires_payment_method':
case 'requiresPaymentMethod':
return PaymentIntentsStatus.RequiresPaymentMethod;

case 'RequiresConfirmation':
case 'requires_confirmation':
case 'requiresConfirmation':
return PaymentIntentsStatus.RequiresConfirmation;

case 'Canceled':
case 'canceled':
return PaymentIntentsStatus.Canceled;

case 'Processing':
case 'processing':
return PaymentIntentsStatus.Processing;

case 'RequiresAction':
case 'requires_action':
case 'requiresAction':
return PaymentIntentsStatus.RequiresAction;

case 'RequiresCapture':
case 'requires_capture':
case 'requiresCapture':
return PaymentIntentsStatus.RequiresCapture;

case 'Unknown':
return PaymentIntentsStatus.Unknown;
}
Expand Down
97 changes: 97 additions & 0 deletions packages/stripe_web/test/parser/payment_intent_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:flutter_stripe_web/src/parser/payment_intent.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:stripe_js/stripe_api.dart' as js;
import 'package:stripe_platform_interface/stripe_platform_interface.dart';

void main() {
group('PaymentIntentsStatusExtension', () {
test('parses canceled status', () {
expect(_parsedPaymentIntentStatus('canceled'),
PaymentIntentsStatus.Canceled);
});

test('parses processing status', () {
expect(_parsedPaymentIntentStatus('processing'),
PaymentIntentsStatus.Processing);
});

test('parses requires_action status', () {
expect(_parsedPaymentIntentStatus('requires_action'),
PaymentIntentsStatus.RequiresAction);
});

test('parses requires_capture status', () {
expect(_parsedPaymentIntentStatus('requires_capture'),
PaymentIntentsStatus.RequiresCapture);
});

test('parses requires_payment_method status', () {
expect(_parsedPaymentIntentStatus('requires_payment_method'),
PaymentIntentsStatus.RequiresPaymentMethod);
});

test('parses requires_confirmation status', () {
expect(_parsedPaymentIntentStatus('requires_confirmation'),
PaymentIntentsStatus.RequiresConfirmation);
});

test('parses succeeded status', () {
expect(_parsedPaymentIntentStatus('succeeded'),
PaymentIntentsStatus.Succeeded);
});
});
}

PaymentIntentsStatus _parsedPaymentIntentStatus(String status) {
final intent = _paymentIntentWithParsedStatus(status);
return PaymentIntentsStatusExtension.parse(intent.status.name);
}

js.PaymentIntent _paymentIntentWithParsedStatus(String status) {
return js.PaymentIntent.fromJson(_paymentIntentWithStatusJson(status));
}

Map<String, dynamic> _paymentIntentWithStatusJson(String status) {
return {
"id": "pi_1Dsr1T2eZvKYlo2C7KHhBRmV",
"object": "payment_intent",
"amount": 100,
"amount_capturable": 0,
"amount_details": {"tip": {}},
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"client_secret":
"pi_1Dsr1T2eZvKYlo2C7KHhBRmV_secret_qH4mhHMXKbJjDgyVuBao7MkBq",
"confirmation_method": "automatic",
"created": 1547555283,
"currency": "usd",
"customer": null,
"description": null,
"invoice": null,
"last_payment_error": null,
"latest_charge": null,
"livemode": false,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {},
"payment_method_types": ["card"],
"processing": null,
"receipt_email": null,
"redaction": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": status,
"transfer_data": null,
"transfer_group": null
};
}

0 comments on commit c1594ce

Please sign in to comment.