| 1 |
<?php |
| 2 |
|
| 3 |
namespace ABlocks\Blocks\StripeButton; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
final class StripePayment { |
| 10 |
|
| 11 |
const API_BASE_URL = 'https://api.stripe.com/v1'; |
| 12 |
|
| 13 |
private string $api; |
| 14 |
|
| 15 |
private array $headers = []; |
| 16 |
private array $body = []; |
| 17 |
|
| 18 |
public function __construct( string $api, array $data ) { |
| 19 |
$this->api = $api; |
| 20 |
$this->headers = [ |
| 21 |
'Authorization' => 'Bearer ' . $this->api, |
| 22 |
'Content-Type' => 'application/x-www-form-urlencoded', |
| 23 |
]; |
| 24 |
$this->set_body( $data ); |
| 25 |
} |
| 26 |
|
| 27 |
private function set_body( array $data ) : void { |
| 28 |
$unit_amount = floatval( $data['unit_amount'] ?? 0 ); |
| 29 |
$quantity = intval( $data['quantity'] ?? 0 ); |
| 30 |
// process will be failed if unit_amount & quantity invalid |
| 31 |
if ( |
| 32 |
empty( $data ) || |
| 33 |
empty( $unit_amount ) || |
| 34 |
empty( $quantity ) |
| 35 |
) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$currency = $data['currency'] ?? 'usd'; |
| 40 |
|
| 41 |
$this->body = [ |
| 42 |
'payment_method_types' => [ 'card' ], |
| 43 |
'mode' => 'payment', |
| 44 |
'line_items[0][quantity]' => $quantity, |
| 45 |
'line_items[0][price_data][currency]' => $currency, |
| 46 |
'line_items[0][price_data][unit_amount]' => $unit_amount, |
| 47 |
'line_items[0][price_data][product_data][name]' => $data['product_name'] ?? 'PRDUCT NAME', |
| 48 |
]; |
| 49 |
$shipping_charge = floatval( $data['shipping_charge'] ?? 0 ); |
| 50 |
if ( ! empty( $shipping_charge ) ) { |
| 51 |
$this->body['shipping_options'] = [ |
| 52 |
[ |
| 53 |
'shipping_rate_data' => [ |
| 54 |
'type' => 'fixed_amount', |
| 55 |
'display_name' => __( 'Shipping Charge: ', 'ablocks' ), |
| 56 |
'fixed_amount' => [ |
| 57 |
'amount' => $shipping_charge, |
| 58 |
'currency' => $currency, |
| 59 |
], |
| 60 |
] |
| 61 |
] |
| 62 |
]; |
| 63 |
} |
| 64 |
$tax_rate = ( $data['tax_rate'] ?? [] ); |
| 65 |
if ( ! empty( $tax_rate ) ) { |
| 66 |
// exclusive, inclusive, unspecified |
| 67 |
$this->body['line_items'][0]['price_data']['tax_behavior'] = $data['tax_behavior'] ?? 'unspecified'; |
| 68 |
$this->body['line_items'][0]['tax_rates'] = [ |
| 69 |
$tax_rate |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! empty( $data['success_url'] ?? 0 ) ) { |
| 74 |
$this->body['success_url'] = esc_url( $data['success_url'] ); |
| 75 |
} |
| 76 |
|
| 77 |
if ( ! empty( $data['cancel_url'] ?? 0 ) ) { |
| 78 |
$this->body['cancel_url'] = esc_url( $data['cancel_url'] ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function process() : ?array { |
| 83 |
if ( empty( $this->body ) ) { |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
$res = wp_remote_post(self::API_BASE_URL . '/checkout/sessions', [ |
| 88 |
'headers' => $this->headers, |
| 89 |
'body' => http_build_query( $this->body ), |
| 90 |
]); |
| 91 |
$response = wp_remote_retrieve_body( $res ); |
| 92 |
if ( |
| 93 |
is_wp_error( $res ) || |
| 94 |
empty( $response ) |
| 95 |
) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
|
| 99 |
return json_decode( |
| 100 |
$response, |
| 101 |
true |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
public function taxe_rates() : ?array { |
| 107 |
$res = wp_remote_get(self::API_BASE_URL . '/tax_rates', [ |
| 108 |
'headers' => $this->headers |
| 109 |
]); |
| 110 |
$response = wp_remote_retrieve_body( $res ); |
| 111 |
if ( |
| 112 |
is_wp_error( $res ) || |
| 113 |
empty( $response ) |
| 114 |
) { |
| 115 |
return null; |
| 116 |
} |
| 117 |
|
| 118 |
return json_decode( |
| 119 |
$response, |
| 120 |
true |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|