1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Controller_ApplePay extends WC_Braintree_Controller_Frontend {
protected $namespace = 'applepay';
private $request;
private $error;
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'payment-method',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'payment_method' ),
'permission_callback' => '__return_true',
)
);
register_rest_route(
$this->rest_uri(),
'domain-association',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'register_domain_association' ),
'permission_callback' => array( $this, 'admin_permission_check' )
)
);
}
public function payment_method( $request ) {
try {
if ( isset( $request['address'] ) ) {
wc_braintree_update_customer_location( $request['address'] );
}
WC()->cart->calculate_totals();
$gateway = WC()->payment_gateways()->payment_gateways()['braintree_applepay'];
return rest_ensure_response(
array(
'success' => true,
'data' => array(
'newLineItems' => $gateway->get_display_items(),
'newTotal' => array(
'type' => 'final',
'label' => $gateway->get_option( 'store_name' ),
'amount' => wc_format_decimal( WC()->cart->total, 2 ),
),
),
)
);
} catch ( Exception $e ) {
return new WP_Error( 'payment-method', $e->getMessage(), array( 'status' => 200 ) );
}
}
public function register_domain_association( $request ) {
try {
if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
$path = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '.well-known';
$file = $path . DIRECTORY_SEPARATOR . 'apple-developer-merchantid-domain-association';
require_once( ABSPATH . '/wp-admin/includes/file.php' );
if ( function_exists( 'WP_Filesystem' ) && ( WP_Filesystem() ) ) {
global $wp_filesystem;
if ( ! $wp_filesystem->is_dir( $path ) ) {
$wp_filesystem->mkdir( $path );
}
$contents = $wp_filesystem->get_contents( WC_BRAINTREE_PATH . 'apple-developer-merchantid-domain-association' );
if ( $wp_filesystem->put_contents( $file, $contents, 0755 ) ) {
return rest_ensure_response(
array(
'data' => array(
'message' => sprintf(
__(
'The %1$sapple-developer-merchantid-domain-association%2$s file has been added to your root folder. You may now register your domains in the Braintree control panel.',
'woo-payment-gateway'
),
'<b>',
'</b>'
) . sprintf(
'<p><a target="_blank" href="%s">%s</a></p>',
'https://docs.paymentplugins.com/wc-braintree/config/#/braintree_applepay',
__( 'Apple Pay Integration Guide', 'woo-payment-gateway' )
),
),
)
);
} else {
throw new Exception( sprintf( __( 'The %1$sapple-developer-merchantid-domain-association%2$s file could not be added to your root folder. You will need to add the file manually.', 'woo-payment-gateway' ), '<b>', '</b>' ) . sprintf( '<p><a target="_blank" href="%s">%s</a></p>', 'https://docs.paymentplugins.com/wc-braintree/config/#/braintree_applepay', __( 'Apple Pay Integration Guide', 'woo-payment-gateway' ) ) );
}
}
}
} catch ( Exception $e ) {
return new WP_Error(
'apple-error',
$e->getMessage(),
array(
'status' => 200,
'message' => $e->getMessage(),
)
);
}
}
}