-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69dce60
commit 53d722a
Showing
10 changed files
with
294 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace BuyMeCoffee\Builder\Methods\Stripe; | ||
|
||
use BuyMeCoffee\Helpers\ArrayHelper as Arr; | ||
|
||
class API | ||
{ | ||
private $createSessionUrl; | ||
private $apiUrl = 'https://api.stripe.com/v1/'; | ||
|
||
public function makeRequest($path, $data, $apiKey, $method = 'GET') | ||
{ | ||
$stripeApiKey = $apiKey; | ||
$sessionHeaders = array( | ||
'Authorization' => 'Bearer ' . $stripeApiKey, | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
); | ||
|
||
$requestData = array( | ||
'headers' => $sessionHeaders, | ||
'body' => http_build_query($data), | ||
'method' => $method, | ||
); | ||
|
||
$url = $this->apiUrl . $path; | ||
|
||
$sessionResponse = wp_remote_post($url, $requestData); | ||
|
||
if (is_wp_error($sessionResponse)) { | ||
echo "API Error: " . esc_html($sessionResponse->get_error_message()); | ||
exit; | ||
} | ||
|
||
$sessionResponseData = wp_remote_retrieve_body($sessionResponse); | ||
|
||
$sessionData = json_decode($sessionResponseData, true); | ||
|
||
if (empty($sessionData['id'])) { | ||
$message = Arr::get($sessionData, 'detail'); | ||
if (!$message) { | ||
$message = Arr::get($sessionData, 'error.message'); | ||
} | ||
if (!$message) { | ||
$message = 'Unknown Stripe API request error'; | ||
} | ||
|
||
return new \WP_Error(423, $message, $sessionData); | ||
} | ||
|
||
return $sessionData; | ||
} | ||
|
||
|
||
public function verifyIPN() | ||
{ | ||
if (!isset($_REQUEST['wpm_bmc_stripe_listener'])) { | ||
return; | ||
} | ||
|
||
$post_data = ''; | ||
if (ini_get('allow_url_fopen')) { | ||
$post_data = file_get_contents('php://input'); | ||
} else { | ||
// If allow_url_fopen is not enabled, then make sure that post_max_size is large enough | ||
ini_set('post_max_size', '12M'); | ||
} | ||
|
||
$data = json_decode($post_data); | ||
|
||
if ($data->id) { | ||
status_header(200); | ||
return $data; | ||
} else { | ||
error_log("specific event"); | ||
error_log(print_r($data)); | ||
return false; | ||
} | ||
|
||
exit(200); | ||
} | ||
|
||
public function getInvoice($eventId) | ||
{ | ||
$api = new ApiRequest(); | ||
$api::set_secret_key((new StripeSettings())->getApiKey()); | ||
return $api::request([], 'events/' . $eventId, 'GET'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace BuyMeCoffee\Builder\Methods\Stripe; | ||
|
||
class StripeSettings | ||
{ | ||
public static function getSettings($key = null) | ||
{ | ||
$settings = get_option('wpm_bmc_payment_settings_stripe', []); | ||
|
||
$defaults = array( | ||
'enable' => 'no', | ||
'payment_mode' => 'test', | ||
'live_pub_key' => '', | ||
'live_secret_key' => '', | ||
'test_pub_key' => '', | ||
'test_secret_key' => '' | ||
); | ||
|
||
$data = wp_parse_args($settings, $defaults); | ||
return $key && isset($data[$key]) ? $data[$key] : $data; | ||
} | ||
|
||
public static function getKeys($key = null) | ||
{ | ||
$settings = self::getSettings(); | ||
|
||
if ($settings['payment_mode'] == 'test') { | ||
$data = array( | ||
'secret' => $settings['test_secret_key'], | ||
'public' => $settings['test_pub_key'] | ||
); | ||
} else { | ||
$data = array( | ||
'secret' => $settings['live_secret_key'], | ||
'public' =>$settings['live_pub_key'] | ||
); | ||
} | ||
|
||
return $key && isset($data[$key]) ? $data[$key] : $data; | ||
|
||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class StripeCheckout { | ||
constructor ($form, $response) { | ||
this.form = $form | ||
this.data = $response.data | ||
this.intent = $response.data?.intent | ||
} | ||
|
||
init () { | ||
this.form.find('.wpm_submit_button').hide(); | ||
let amounPrefix = this.form.find('.wpm_payment_total_amount_prefix').text(); | ||
|
||
let buttonText = "Pay " + amounPrefix + (parseInt(this.intent.amount) / 100) + " Now" | ||
|
||
let submitButton = "<button id='wpm_bmc_pay_now' style='margin-top: 20px;width: 100%;background: #11d8cc;' type='submit'>" + buttonText + "</button>"; | ||
var stripe = Stripe(this.data?.order_items?.payment_args?.public_key); | ||
const elements = stripe.elements({ | ||
clientSecret: this.intent.client_secret | ||
}); | ||
const paymentElement = elements.create('payment', {}); | ||
paymentElement.mount('#wpm_bmc_pay_methods'); | ||
|
||
this.form.find('.wpm_bmc_pay_methods')?.parent().append("<p class='wpm_bmc_loading_processor' style='color: #48a891;font-size: 14px;'>Payment processing...</p>"); | ||
this.form.find('#fluent_cart_order_btn').hide(); | ||
|
||
let that= this; | ||
paymentElement.on('ready', (event) => { | ||
jQuery('.wpm_bmc_loading_processor').remove(); | ||
jQuery('#wpm_bmc_pay_methods').append(submitButton); | ||
this.form.find('.wpm_bmc_input_content, .wpm_bmc_payment_input_content').hide(); | ||
this.form.prepend("<p class='complete_payment_instruction'>Please complete your donation with Stripe 👇</p>"); | ||
|
||
jQuery('#wpm_bmc_pay_now').on('click', function(e) { | ||
e.preventDefault() | ||
jQuery(this).text('Processing...'); | ||
elements.submit().then(result=> { | ||
stripe.confirmPayment({ | ||
elements, | ||
confirmParams: { | ||
return_url: that.data?.order_items?.payment_args?.success_url | ||
} | ||
}).then((result) => { | ||
jQuery(this).text(buttonText); | ||
}) | ||
}).catch(error => { | ||
jQuery(this).text(buttonText); | ||
}) | ||
}) | ||
}); | ||
} | ||
} | ||
|
||
window.addEventListener("wpm_bmc_payment_next_action_stripe", function (e) { | ||
new StripeCheckout(e.detail.form, e.detail.response).init(); | ||
}); |
Oops, something went wrong.