| 1 |
<?php |
| 2 |
/** |
| 3 |
* Stripe payment api |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Core\Integrations\Stripe; |
| 8 |
|
| 9 |
use Timetics\Base\Api; |
| 10 |
use Timetics\Core\Bookings\Booking; |
| 11 |
use Timetics\Utils\Singleton; |
| 12 |
use WP_HTTP_Response; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Api Stripe |
| 16 |
*/ |
| 17 |
class Api_Stripe extends Api { |
| 18 |
use Singleton; |
| 19 |
|
| 20 |
/** |
| 21 |
* Store api namespace |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $namespace = 'timetics/v1'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Store rest base |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $rest_base = 'stripe'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Register rest routes |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function register_routes() { |
| 40 |
/** |
| 41 |
* Register route |
| 42 |
* |
| 43 |
* @var void |
| 44 |
*/ |
| 45 |
register_rest_route( |
| 46 |
$this->namespace, $this->rest_base . '/payment', [ |
| 47 |
[ |
| 48 |
'methods' => \WP_REST_Server::CREATABLE, |
| 49 |
'callback' => [ $this, 'create_payment' ], |
| 50 |
'permission_callback' => function () { |
| 51 |
return true; |
| 52 |
}, |
| 53 |
], |
| 54 |
] |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Create stripe payment |
| 60 |
* |
| 61 |
* @param WP_Rest_Request $request |
| 62 |
* |
| 63 |
* @return JSON |
| 64 |
*/ |
| 65 |
public function create_payment( $request ) { |
| 66 |
$data = json_decode( $request->get_body(), true ); |
| 67 |
|
| 68 |
$amount = ! empty( $data['amount'] ) ? floatval( $data['amount'] ) : 0; |
| 69 |
$currency = ! empty( $data['currency'] ) ? sanitize_text_field( $data['currency'] ) : ''; |
| 70 |
$booking_id = ! empty( $data['booking_id'] ) ? absint( $data['booking_id'] ) : 0; |
| 71 |
$token = ! empty( $data['security_token'] ) ? sanitize_text_field( $data['security_token'] ) : ''; |
| 72 |
|
| 73 |
$metadata = []; |
| 74 |
|
| 75 |
// Bind PaymentIntent to a real booking by metadata so make_payment can |
| 76 |
// verify ownership server-side. Reject mismatched / missing bindings. |
| 77 |
if ( $booking_id > 0 && '' !== $token ) { |
| 78 |
$booking = new Booking( $booking_id ); |
| 79 |
|
| 80 |
if ( ! $booking->is_booking() ) { |
| 81 |
return new WP_HTTP_Response( |
| 82 |
[ |
| 83 |
'success' => 0, |
| 84 |
'status_code' => 404, |
| 85 |
'message' => __( 'Invalid booking id.', 'timetics' ), |
| 86 |
], |
| 87 |
404 |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
$stored = (string) $booking->get_security_token(); |
| 92 |
|
| 93 |
if ( '' === $stored || ! hash_equals( $stored, $token ) ) { |
| 94 |
return new WP_HTTP_Response( |
| 95 |
[ |
| 96 |
'success' => 0, |
| 97 |
'status_code' => 403, |
| 98 |
'message' => __( 'Invalid booking token.', 'timetics' ), |
| 99 |
], |
| 100 |
403 |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
$metadata['booking_id'] = $booking_id; |
| 105 |
$metadata['security_token'] = $stored; |
| 106 |
} |
| 107 |
|
| 108 |
$payment = new StripePayment(); |
| 109 |
|
| 110 |
$payment = $payment->create_payment( |
| 111 |
[ |
| 112 |
'amount' => $amount * 100, |
| 113 |
'currency' => $currency, |
| 114 |
'metadata' => $metadata, |
| 115 |
] |
| 116 |
); |
| 117 |
|
| 118 |
if ( is_wp_error( $payment ) ) { |
| 119 |
$response = [ |
| 120 |
'success' => 0, |
| 121 |
'status_code' => 403, |
| 122 |
'message' => $payment->get_error_message(), |
| 123 |
]; |
| 124 |
|
| 125 |
return new WP_HTTP_Response( $response, 403 ); |
| 126 |
} |
| 127 |
|
| 128 |
return rest_ensure_response( $payment ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|