Skip to content

Commit

Permalink
Integrate Apple Pay as express payment method (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanzade-hasan authored Jan 27, 2025
1 parent 207997b commit 317a1b2
Show file tree
Hide file tree
Showing 22 changed files with 1,205 additions and 727 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
This changelog follows the specifications of https://keepachangelog.com. Please, follow the specs when adding a new entry

## [1.3.0] - 2025-01-24

### Added

- Apple Pay as express checkout


## [1.2.0] - 2024-10-23

### Added
Expand Down
20 changes: 16 additions & 4 deletions bp-plugin-woocommerce-api2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* Plugin Name: Better Payment WooCommerce Extension
* Plugin URI: https://github.com/better-payment/bp-plugin-woocommerce-api2
* Description: Better Payment plugin to implement payment methods using API2
* Version: 1.2.0
* Version: 1.3.0
* Author: Better Payment
* Author URI: https://betterpayment.de
* Text Domain: bp-plugin-woocommerce-api2
* Domain Path: /languages
*/

//defined( 'ABSPATH' ) || exit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use Automattic\WooCommerce\Utilities\FeaturesUtil;
Expand Down Expand Up @@ -44,6 +46,7 @@ public function init() {
add_filter( 'woocommerce_integrations', array( $this, 'add_integration' ) );

// Include payment methods
include_once 'includes/payment-gateways/abstract-betterpayment-gateway.php';
include_once 'includes/payment-gateways/credit-card.php';
include_once 'includes/payment-gateways/paypal.php';
include_once 'includes/payment-gateways/paydirekt.php';
Expand All @@ -56,14 +59,18 @@ public function init() {
include_once 'includes/payment-gateways/sepa-direct-debit-b2b.php';
include_once 'includes/payment-gateways/invoice.php';
include_once 'includes/payment-gateways/invoice-b2b.php';
include_once 'includes/payment-gateways/apple-pay.php';

// Register payment methods
add_filter('woocommerce_payment_gateways', array($this, 'add_betterpayment_gateways'));

// Include helpers
include_once 'includes/helpers/config-reader.php';

// Include webhook route endpoint
include_once 'includes/webhook.php';
// Include custom endpoints
include_once 'includes/api/webhook.php';
include_once 'includes/api/apple-pay-session.php';
include_once 'includes/api/betterpayment-payment-request.php';

// Load translations
load_plugin_textdomain( 'bp-plugin-woocommerce-api2', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
Expand Down Expand Up @@ -98,6 +105,7 @@ public function add_betterpayment_gateways($methods) {
$methods[] = 'BetterPayment_Sepa_Direct_Debit_B2B';
$methods[] = 'BetterPayment_Invoice';
$methods[] = 'BetterPayment_Invoice_B2B';
$methods[] = 'BetterPayment_Apple_Pay';

return $methods;
}
Expand All @@ -124,6 +132,8 @@ public function betterpayment_blocks_support() {
include_once 'includes/blocks/payments/sepa-direct-debit.php';
include_once 'includes/blocks/payments/sepa-direct-debit-b2b.php';

include_once 'includes/blocks/payments/apple-pay.php';

add_action(
'woocommerce_blocks_payment_method_type_registration',
function( PaymentMethodRegistry $payment_method_registry ) {
Expand All @@ -140,6 +150,8 @@ function( PaymentMethodRegistry $payment_method_registry ) {
$payment_method_registry->register( new BetterPayment_Invoice_B2B_Block() );
$payment_method_registry->register( new BetterPayment_Sepa_Direct_Debit_Block() );
$payment_method_registry->register( new BetterPayment_Sepa_Direct_Debit_B2B_Block() );

$payment_method_registry->register( new BetterPayment_ApplePay_Block() );
}
);
}
Expand Down
35 changes: 35 additions & 0 deletions includes/api/apple-pay-session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
add_action('rest_api_init', function () {
register_rest_route('betterpayment', 'apple-pay-session', array(
'methods' => 'POST',
'callback' => 'fetch_apple_pay_session',
'permission_callback' => '__return_true', // Adjust permissions as needed
));
});

function fetch_apple_pay_session(): WP_REST_Response {
$url = Config_Reader::get_api_url() . '/db_apple_pay_merchants';

$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode( Config_Reader::get_api_key() . ':' . Config_Reader::get_outgoing_key())
];

$body = wp_json_encode([
'initiative_context' => parse_url(home_url(), PHP_URL_HOST),
]);

$response = wp_remote_post( $url, [
'headers' => $headers,
'body' => $body,
]);

if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
$responseBody = json_decode( wp_remote_retrieve_body( $response ), true );

return new WP_REST_Response($responseBody, 200);
}
else {
return new WP_REST_Response(wp_remote_retrieve_body($response), wp_remote_retrieve_response_code($response));
}
}
29 changes: 29 additions & 0 deletions includes/api/betterpayment-payment-request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
add_action('rest_api_init', function () {
register_rest_route('betterpayment', 'payment', array(
'methods' => 'POST',
'callback' => 'payment',
'permission_callback' => '__return_true', // Adjust permissions as needed
));
});

// Only used for Apple Pay
function payment(WP_REST_Request $request): WP_REST_Response {
$url = Config_Reader::get_api_url() . '/rest/payment';
$body = $request->get_body_params();

// Add the payment type to the body
$body['payment_type'] = 'applepay';

$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode( Config_Reader::get_api_key() . ':' . Config_Reader::get_outgoing_key() )
];

$response = wp_remote_post( $url, [
'headers' => $headers,
'body' => wp_json_encode($body),
] );

return new WP_REST_Response(wp_remote_retrieve_body($response), wp_remote_retrieve_response_code($response));
}
File renamed without changes.
Loading

0 comments on commit 317a1b2

Please sign in to comment.