| 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\Utils\Singleton; |
| 11 |
use WP_HTTP_Response; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Api Stripe |
| 15 |
*/ |
| 16 |
class Api_Stripe extends Api { |
| 17 |
use Singleton; |
| 18 |
|
| 19 |
/** |
| 20 |
* Store api namespace |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $namespace = 'timetics/v1'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Store rest base |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $rest_base = 'stripe'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Register rest routes |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function register_routes() { |
| 39 |
/** |
| 40 |
* Register route |
| 41 |
* |
| 42 |
* @var void |
| 43 |
*/ |
| 44 |
register_rest_route( |
| 45 |
$this->namespace, $this->rest_base . '/payment', [ |
| 46 |
[ |
| 47 |
'methods' => \WP_REST_Server::CREATABLE, |
| 48 |
'callback' => [ $this, 'create_payment' ], |
| 49 |
'permission_callback' => function () { |
| 50 |
return true; |
| 51 |
}, |
| 52 |
], |
| 53 |
] |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Create stripe payment |
| 59 |
* |
| 60 |
* @param WP_Rest_Request $request |
| 61 |
* |
| 62 |
* @return JSON |
| 63 |
*/ |
| 64 |
public function create_payment( $request ) { |
| 65 |
$data = json_decode( $request->get_body(), true ); |
| 66 |
|
| 67 |
$amount = ! empty( $data['amount'] ) ? floatval( $data['amount'] ) : 0; |
| 68 |
$currency = ! empty( $data['currency'] ) ? sanitize_text_field( $data['currency'] ) : ''; |
| 69 |
|
| 70 |
$payment = new StripePayment(); |
| 71 |
|
| 72 |
$payment = $payment->create_payment( |
| 73 |
[ |
| 74 |
'amount' => $amount * 100, |
| 75 |
'currency' => $currency, |
| 76 |
] |
| 77 |
); |
| 78 |
|
| 79 |
if ( is_wp_error( $data ) ) { |
| 80 |
$response = [ |
| 81 |
'success' => 0, |
| 82 |
'status_code' => 403, |
| 83 |
'message' => $payment->get_error_message(), |
| 84 |
]; |
| 85 |
|
| 86 |
return new WP_HTTP_Response( $response, 403 ); |
| 87 |
} |
| 88 |
|
| 89 |
return rest_ensure_response( $payment ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|