blob: dddc1f01e3b34bb052ecc1765bef7ee7b62df8e5 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2/**
3 * Class WC_REST_Stripe_Orders_Controller
4 */
5
6defined( 'ABSPATH' ) || exit;
7
8/**
9 * REST controller for orders.
10 */
11class WC_REST_Stripe_Orders_Controller extends WC_Stripe_REST_Base_Controller {
12
13 /**
14 * Endpoint path.
15 *
16 * @var string
17 */
18 protected $rest_base = 'wc_stripe/orders';
19
20 /**
21 * Stripe payment gateway.
22 *
23 * @var WC_Gateway_Stripe
24 */
25 private $gateway;
26
27 /**
28 * Constructor.
29 *
30 * @param WC_Gateway_Stripe $gateway Stripe payment gateway.
31 */
32 public function __construct( WC_Gateway_Stripe $gateway ) {
33 $this->gateway = $gateway;
34 }
35
36 /**
37 * Configure REST API routes.
38 */
39 public function register_routes() {
40 register_rest_route(
41 $this->namespace,
42 '/' . $this->rest_base . '/(?P<order_id>\d+)/create_customer',
43 [
44 'methods' => WP_REST_Server::CREATABLE,
45 'callback' => [ $this, 'create_customer' ],
46 'permission_callback' => [ $this, 'check_permission' ],
47 ]
48 );
49
50 register_rest_route(
51 $this->namespace,
52 '/' . $this->rest_base . '/(?P<order_id>\w+)/capture_terminal_payment',
53 [
54 'methods' => WP_REST_Server::CREATABLE,
55 'callback' => [ $this, 'capture_terminal_payment' ],
56 'permission_callback' => [ $this, 'check_permission' ],
57 'args' => [
58 'payment_intent_id' => [
59 'required' => true,
60 ],
61 ],
62 ]
63 );
64 }
65
66 /**
67 * Create a Stripe customer for an order if needed, or return existing customer.
68 *
69 * @param WP_REST_Request $request Full data about the request.
70 */
71 public function create_customer( $request ) {
72 $order_id = $request['order_id'];
73
74 // Ensure order exists.
75 $order = wc_get_order( $order_id );
76 if ( false === $order || ! ( $order instanceof WC_Order ) ) {
77 return new WP_Error( 'wc_stripe', __( 'Order not found', 'woocommerce-gateway-stripe' ), [ 'status' => 404 ] );
78 }
79
80 // Validate order status before creating customer.
81 $disallowed_order_statuses = apply_filters( 'wc_stripe_create_customer_disallowed_order_statuses', [ 'completed', 'cancelled', 'refunded', 'failed' ] );
82 if ( $order->has_status( $disallowed_order_statuses ) ) {
83 return new WP_Error( 'wc_stripe_invalid_order_status', __( 'Invalid order status', 'woocommerce-gateway-stripe' ), [ 'status' => 400 ] );
84 }
85
86 // Get a customer object with the order's user, if available.
87 $order_user = $order->get_user();
88 if ( false === $order_user ) {
89 $order_user = new WP_User();
90 }
91 $customer = new WC_Stripe_Customer( $order_user->ID );
92
93 // Set the customer ID if known but not already set.
94 $customer_id = $order->get_meta( '_stripe_customer_id', true );
95 if ( ! $customer->get_id() && $customer_id ) {
96 $customer->set_id( $customer_id );
97 }
98
99 try {
100 // Update or create Stripe customer.
101 $customer_data = WC_Stripe_Customer::map_customer_data( $order );
102 if ( $customer->get_id() ) {
103 $customer_id = $customer->update_customer( $customer_data );
104 } else {
105 $customer_id = $customer->create_customer( $customer_data );
106 }
107 } catch ( WC_Stripe_Exception $e ) {
108 return new WP_Error( 'stripe_error', $e->getMessage() );
109 }
110
111 $order->update_meta_data( '_stripe_customer_id', $customer_id );
112 $order->save();
113
114 return rest_ensure_response( [ 'id' => $customer_id ] );
115 }
116
117 public function capture_terminal_payment( $request ) {
118 try {
119 $intent_id = $request['payment_intent_id'];
120 $order_id = $request['order_id'];
121 $order = wc_get_order( $order_id );
122
123 // Check that order exists before capturing payment.
124 if ( ! $order ) {
125 return new WP_Error( 'wc_stripe_missing_order', __( 'Order not found', 'woocommerce-gateway-stripe' ), [ 'status' => 404 ] );
126 }
127
128 // Do not process refunded orders.
129 if ( 0 < $order->get_total_refunded() ) {
130 return new WP_Error( 'wc_stripe_refunded_order_uncapturable', __( 'Payment cannot be captured for partially or fully refunded orders.', 'woocommerce-gateway-stripe' ), [ 'status' => 400 ] );
131 }
132
133 // Retrieve intent from Stripe.
134 $intent = WC_Stripe_API::retrieve( "payment_intents/$intent_id" );
135
136 // Check that intent exists.
137 if ( ! empty( $intent->error ) ) {
138 return new WP_Error( 'stripe_error', $intent->error->message );
139 }
140
141 // Ensure that intent can be captured.
142 if ( ! in_array( $intent->status, [ 'processing', 'requires_capture' ], true ) ) {
143 return new WP_Error( 'wc_stripe_payment_uncapturable', __( 'The payment cannot be captured', 'woocommerce-gateway-stripe' ), [ 'status' => 409 ] );
144 }
145
146 // Update order with payment method and intent details.
147 $order->set_payment_method( WC_Gateway_Stripe::ID );
148 $order->set_payment_method_title( __( 'WooCommerce Stripe In-Person Payments', 'woocommerce-gateway-stripe' ) );
149 $this->gateway->save_intent_to_order( $order, $intent );
150
151 // Capture payment intent.
152 $charge = end( $intent->charges->data );
153 $this->gateway->process_response( $charge, $order );
154 $result = WC_Stripe_Order_Handler::get_instance()->capture_payment( $order );
155
156 // Check for failure to capture payment.
157 if ( empty( $result ) || empty( $result->status ) || 'succeeded' !== $result->status ) {
158 return new WP_Error(
159 'wc_stripe_capture_error',
160 sprintf(
161 // translators: %s: the error message.
162 __( 'Payment capture failed to complete with the following message: %s', 'woocommerce-gateway-stripe' ),
163 $result->error->message ?? __( 'Unknown error', 'woocommerce-gateway-stripe' )
164 ),
165 [ 'status' => 502 ]
166 );
167 }
168
169 // Successfully captured.
170 $order->update_status( 'completed' );
171 return rest_ensure_response(
172 [
173 'status' => $result->status,
174 'id' => $result->id,
175 ]
176 );
177 } catch ( WC_Stripe_Exception $e ) {
178 return rest_ensure_response( new WP_Error( 'stripe_error', $e->getMessage() ) );
179 }
180
181 }
182}