• Namespace
  • Class
  • Tree
  • Deprecated
  • Hook Reference

Namespaces

  • None
  • PaymentPlugins

Classes

  • Braintree_Gateway_Manager
  • PaymentPlugins\WC_Braintree_Admin_Meta_Box_Product_Data
  • PaymentPlugins\WC_Braintree_Constants
  • WC_Braintree_3ds_Validation
  • WC_Braintree_Admin_Assets
  • WC_Braintree_Admin_List_Table_Subscriptions
  • WC_Braintree_Admin_Menus
  • WC_Braintree_Admin_Order_Metaboxes
  • WC_Braintree_Admin_Settings
  • WC_Braintree_Admin_User_Edit
  • WC_Braintree_Advanced_Fraud_Settings
  • WC_Braintree_Advanced_Settings_API
  • WC_Braintree_Alipay_Payment_Gateway
  • WC_Braintree_API_Settings
  • WC_Braintree_ApplePay_Payment_Gateway
  • WC_Braintree_Bancontact_Payment_Gateway
  • WC_Braintree_CC_Payment_Gateway
  • WC_Braintree_Condition_Evaluator
  • WC_Braintree_Context_Stack
  • WC_Braintree_Controller_3ds
  • WC_Braintree_Controller_ApplePay
  • WC_Braintree_Controller_Cart
  • WC_Braintree_Controller_Checkout
  • WC_Braintree_Controller_Client_Token
  • WC_Braintree_Controller_Data_Migration
  • WC_Braintree_Controller_Frontend
  • WC_Braintree_Controller_GooglePay
  • WC_Braintree_Controller_Kount
  • WC_Braintree_Controller_Local_Payment
  • WC_Braintree_Controller_Merchant_Accounts
  • WC_Braintree_Controller_Order_Actions
  • WC_Braintree_Controller_Payment_Tokens
  • WC_Braintree_Controller_PayPal
  • WC_Braintree_Controller_Plan
  • WC_Braintree_Controller_Product
  • WC_Braintree_Controller_Settings
  • WC_Braintree_Controller_Webhook
  • WC_Braintree_Customer_Manager
  • WC_Braintree_Deprecated_Filter_Hooks
  • WC_Braintree_Descriptor_Settings
  • WC_Braintree_EPS_Payment_Gateway
  • WC_Braintree_Fee_Settings
  • WC_Braintree_Field_Manager
  • WC_Braintree_Form_Handler
  • WC_Braintree_Frontend_Scripts
  • WC_Braintree_Giropay_Payment_Gateway
  • WC_Braintree_GooglePay_Payment_Gateway
  • WC_Braintree_IDEAL_Payment_Gateway
  • WC_Braintree_Install
  • WC_Braintree_Local_Payment_Gateway
  • WC_Braintree_Manager
  • WC_Braintree_Merchant_Account_Settings
  • WC_Braintree_Meta_Box_Coupons
  • WC_Braintree_Meta_Box_Subscription_Order_Data
  • WC_Braintree_MyBank_Payment_Gateway
  • WC_Braintree_P24_Payment_Gateway
  • WC_Braintree_Payment_Gateway
  • WC_Braintree_Payment_Method_Conversion
  • WC_Braintree_PayPal_Payment_Gateway
  • WC_Braintree_Post_Types
  • WC_Braintree_Product_Gateway_Option
  • WC_Braintree_Query
  • WC_Braintree_Rest_API
  • WC_Braintree_Rest_Controller
  • WC_Braintree_Rest_Webook_Authentication
  • WC_Braintree_Sepa_Payment_Gateway
  • WC_Braintree_Settings_API
  • WC_Braintree_Shortcode_Payment_Buttons
  • WC_Braintree_Shortcodes
  • WC_Braintree_Sofort_Payment_Gateway
  • WC_Braintree_Subscription
  • WC_Braintree_Subscription_Data_Store_CPT
  • WC_Braintree_Subscription_Settings
  • WC_Braintree_Subscriptions_Cart
  • WC_Braintree_Subscriptions_Checkout
  • WC_Braintree_Update
  • WC_Braintree_Venmo_Payment_Gateway
  • WC_Braintree_WeChat_Payment_Gateway
  • WC_Meta_Box_Braintree_Subscription_Data
  • WC_Payment_Token_Braintree
  • WC_Payment_Token_Braintree_ApplePay
  • WC_Payment_Token_Braintree_CC
  • WC_Payment_Token_Braintree_GooglePay
  • WC_Payment_Token_Braintree_Local_Payment
  • WC_Payment_Token_Braintree_PayPal
  • WC_Payment_Token_Braintree_Venmo
  • WC_Product_Braintree_Subscription
  • WC_Product_Braintree_Subscription_Variation
  • WC_Product_Braintree_Variable_Subscription
  • WCS_Braintree_Subscription_Controller

Traits

  • WC_Braintree_Controller_Cart_Trait
  • WC_Braintree_Settings_Trait

Functions

  • bfwc_add_body_class
  • bfwc_billing_periods_string
  • bfwc_can_delete_payment_method
  • bfwc_copy_address_from_order
  • bfwc_error_code_type_nicename
  • bfwc_get_combined_error_messages
  • bfwc_get_error_message
  • bfwc_get_error_messages
  • bfwc_get_gateway_timezone
  • bfwc_get_product_descriptors
  • bfwc_get_subscription_status_name
  • bfwc_get_subscription_statuses
  • bfwc_get_template
  • bfwc_get_timezones
  • bfwc_register_subscription_status
  • bfwc_subscription_length_string
  • bfwc_subscription_user_actions
  • bfwcs_billing_interval_string
  • bfwcs_billing_intervals
  • bfwcs_calculate_end_date
  • bfwcs_calculate_first_payment_date
  • bfwcs_calculate_next_payment_date
  • bfwcs_calculate_order_total
  • bfwcs_calculate_start_date
  • bfwcs_can_combine_subscriptions
  • bfwcs_cart_contains_subscriptions
  • bfwcs_cart_formatted_date
  • bfwcs_cart_recurring_total_html
  • bfwcs_cart_shipping_total
  • bfwcs_cart_subtotal_string
  • bfwcs_cart_tax_total_html
  • bfwcs_copy_order_meta
  • bfwcs_create_order_from_subscription
  • bfwcs_create_renewal_order
  • bfwcs_create_subscription
  • bfwcs_frontend_interval_string
  • bfwcs_get_currency_symbol
  • bfwcs_get_interval_string
  • bfwcs_get_length_string
  • bfwcs_get_period_string
  • bfwcs_get_plan_from_product
  • bfwcs_get_price_string
  • bfwcs_get_product_price_html
  • bfwcs_get_related_orders
  • bfwcs_get_subscription
  • bfwcs_get_subscription_actions
  • bfwcs_get_subscription_statuses
  • bfwcs_get_subscriptions_for_order
  • bfwcs_get_subscriptions_for_user
  • bfwcs_is_change_payment_method
  • bfwcs_is_pay_for_subscription_request
  • bfwcs_order_contains_subscription
  • bfwcs_order_is_subscription
  • bfwcs_product_is_subscription
  • bfwcs_subscription_link_active
  • braintree
  • braintree_delete_user_payment_method
  • braintree_delete_user_payment_methods
  • braintree_device_data_field
  • braintree_get_customer_id
  • braintree_get_default_method
  • braintree_get_method_uris
  • braintree_get_method_url
  • braintree_get_payment_method_formats
  • braintree_get_payment_method_from_token
  • braintree_get_payment_method_title_from_array
  • braintree_get_payment_method_title_from_method
  • braintree_get_payment_method_title_from_method_details
  • braintree_get_payment_method_title_from_transaction
  • braintree_get_payment_title_from_token
  • braintree_get_payment_token_from_transaction
  • braintree_get_user_payment_methods
  • braintree_hidden_field
  • braintree_nonce_field
  • braintree_payment_method_to_array
  • braintree_payment_token_field
  • braintree_save_payment_method_from_transaction
  • braintree_save_user_payment_method
  • braintree_save_user_payment_methods
  • bt_manager
  • bwc_3ds_no_action_needed
  • bwc_3ds_verify_vaulted_methods
  • bwc_add_allowed_html
  • bwc_add_notice
  • bwc_applepay_cart_checkout_enabled
  • bwc_applepay_checkout_banner_enabled
  • bwc_applepay_get_line_items
  • bwc_applepay_product_checkout_enabled
  • bwc_calculate_fee
  • bwc_can_refund_order
  • bwc_card_payments_enabled
  • bwc_custom_form_text
  • bwc_cvv_field_enabled
  • bwc_display_icons_on_payment_methods_page
  • bwc_display_save_payment_method
  • bwc_dropin_v2_enabled
  • bwc_dropin_v3_enabled
  • bwc_enable_signup_from_checkout
  • bwc_execute_conditional_statement
  • bwc_fail_on_duplicate
  • bwc_fee_enabled_for_gateway
  • bwc_fees_enabled
  • bwc_get_3_0_0_deprecated_order_prop
  • bwc_get_3_0_0_updated_props
  • bwc_get_3ds_modal_html
  • bwc_get_billing_agreement_desc
  • bwc_get_comma_separated_product_names
  • bwc_get_conditional_values
  • bwc_get_custom_form
  • bwc_get_custom_form_fields
  • bwc_get_custom_forms
  • bwc_get_default_method
  • bwc_get_enclosed_icon_url
  • bwc_get_fees_for_gateway
  • bwc_get_gateway_fees
  • bwc_get_gateway_id
  • bwc_get_googlepay_methods
  • bwc_get_loader_file
  • bwc_get_merchant_account
  • bwc_get_merchant_accounts
  • bwc_get_option_text
  • bwc_get_order_property
  • bwc_get_payment_gateways
  • bwc_get_paypal_button
  • bwc_get_paypal_credit_button
  • bwc_get_paypal_credit_html
  • bwc_get_paypal_html
  • bwc_get_product_property
  • bwc_get_save_method_template
  • bwc_get_smartbutton_style
  • bwc_get_template
  • bwc_get_template_html
  • bwc_get_user_applepay_payment_methods
  • bwc_get_user_payment_methods
  • bwc_get_user_paypal_payment_methods
  • bwc_googlepay_enabled
  • bwc_is_3ds_active
  • bwc_is_3ds_enabled
  • bwc_is_admin_webhook_request
  • bwc_is_advanced_fraud_tools
  • bwc_is_applepay_enabled
  • bwc_is_checkout
  • bwc_is_custom_form
  • bwc_is_descriptors_enabled
  • bwc_is_dynamic_card_display
  • bwc_is_paypal_enabled
  • bwc_is_wc_3_0_0_or_more
  • bwc_kount_enabled
  • bwc_kount_send_processing_email
  • bwc_locate_template
  • bwc_output_checkout_error
  • bwc_payment_icons_enclosed_type
  • bwc_payment_icons_outside
  • bwc_payment_icons_type
  • bwc_payment_loader_enabled
  • bwc_payment_method_token_field
  • bwc_payment_token_field
  • bwc_paypal_cart_checkout_enabled
  • bwc_paypal_checkout_banner_enabled
  • bwc_paypal_checkout_flow
  • bwc_paypal_credit_active
  • bwc_paypal_credit_conditions
  • bwc_paypal_credit_enabled
  • bwc_paypal_credit_send_shipping
  • bwc_paypal_send_shipping
  • bwc_postal_code_enabled
  • bwc_refresh_payment_fragments
  • bwc_saved_payment_method_style
  • bwc_saved_payment_methods_list
  • bwc_set_checkout_error
  • bwc_smart_buttons_enabled
  • bwc_use_admin_text_for_custom_form
  • bwcs_braintree_subscriptions_active
  • bwcs_calculate_order_total
  • bwcs_can_combine_subscriptions
  • bwcs_can_delete_payment_method
  • bwcs_cart_item_contains_subscription
  • bwcs_get_cart_item_key
  • bwcs_get_current_date_time
  • bwcs_get_num_of_billing_cycles
  • bwcs_get_plan_from_product
  • bwcs_get_product_from_subscription
  • bwcs_get_start_date_in_utc
  • bwcs_get_subscription_date
  • bwcs_is_braintree_subscription
  • bwcs_is_change_payment_method
  • bwcs_is_paid_for_failed_renewal_request
  • bwcs_is_woocommerce_change_payment
  • bwcs_order_contains_subscription
  • bwcs_order_has_coupon
  • bwcs_order_is_subscription
  • bwcs_product_is_subscription
  • bwcs_retry_after_exception
  • bwcs_start_date_in_site_timezone
  • bwcs_subscription_has_trial
  • bwcs_subscription_is_synched
  • bwcs_subscriptions_active
  • wc_braintree_add_allowed_html
  • wc_braintree_add_body_class
  • wc_braintree_banner_checkout_template
  • wc_braintree_card_loader_options
  • wc_braintree_cart_checkout_template
  • wc_braintree_connection_settings
  • wc_braintree_create_payment_token
  • wc_braintree_create_payment_token_from_transaction
  • wc_braintree_custom_form_active
  • wc_braintree_custom_form_options
  • wc_braintree_delete_customer
  • wc_braintree_deprecated_template_check
  • wc_braintree_device_data_field
  • wc_braintree_display_prices_including_tax
  • wc_braintree_enqueue_local_payments
  • wc_braintree_environment
  • wc_braintree_errors_from_object
  • wc_braintree_evaluate_condition
  • wc_braintree_filter_customer_payment_tokens
  • wc_braintree_filter_payment_tokens
  • wc_braintree_generate_client_token
  • wc_braintree_get_3ds_actions
  • wc_braintree_get_card_type_icons
  • wc_braintree_get_checkout_fields
  • wc_braintree_get_conditional_values
  • wc_braintree_get_currency
  • wc_braintree_get_custom_form
  • wc_braintree_get_customer_id
  • wc_braintree_get_error_messages
  • wc_braintree_get_fee_gateways
  • wc_braintree_get_file_data
  • wc_braintree_get_local_gateways
  • wc_braintree_get_merchant_account
  • wc_braintree_get_merchant_accounts
  • wc_braintree_get_order_environment
  • wc_braintree_get_order_for_payment_id
  • wc_braintree_get_payment_method_format
  • wc_braintree_get_payment_tokens
  • wc_braintree_get_products_for_conditions
  • wc_braintree_get_response_code_message
  • wc_braintree_get_template
  • wc_braintree_get_template_html
  • wc_braintree_hidden_field
  • wc_braintree_local_payment_completed
  • wc_braintree_local_payment_params
  • wc_braintree_log
  • wc_braintree_log_error
  • wc_braintree_log_info
  • wc_braintree_merchant_id
  • wc_braintree_merge_order_statuses
  • wc_braintree_move_translations
  • wc_braintree_nonce_field
  • wc_braintree_order_statuses
  • wc_braintree_order_statuses_for_registration
  • wc_braintree_output_checkout_error
  • wc_braintree_parse_xml_contents
  • wc_braintree_payment_complete_order_status
  • wc_braintree_payment_gateways
  • wc_braintree_payment_method_formats
  • wc_braintree_payment_method_title
  • wc_braintree_payment_token_field
  • wc_braintree_pre_orders_active
  • wc_braintree_private_key
  • wc_braintree_process_scheduled_local_payment
  • wc_braintree_production_active
  • wc_braintree_public_key
  • wc_braintree_save_cc_enabled
  • wc_braintree_save_customer
  • wc_braintree_set_checkout_error
  • wc_braintree_show_product_checkout_gateways
  • wc_braintree_spam_bot_field
  • wc_braintree_subscriptions_active
  • wc_braintree_update_customer_location
  • wc_braintree_webhook_check
  • wc_braintree_woocommerce_payment_token_deleted
  • wcs_braintree_account_menu_items
  • wcs_braintree_active
  • wcs_braintree_before_delete_order_item
  • wcs_braintree_billing_interval_string
  • wcs_braintree_billing_intervals
  • wcs_braintree_billing_periods
  • wcs_braintree_billing_periods_string
  • wcs_braintree_calculate_end_date
  • wcs_braintree_calculate_first_payment_date
  • wcs_braintree_calculate_next_payment_date
  • wcs_braintree_calculate_start_date
  • wcs_braintree_cart_contains_subscription
  • wcs_braintree_cart_formatted_date
  • wcs_braintree_cart_recurring_tax_html
  • wcs_braintree_cart_recurring_total_html
  • wcs_braintree_cart_shipping_total
  • wcs_braintree_cart_subtotal_string
  • wcs_braintree_cart_tax_total_html
  • wcs_braintree_cart_totals_coupon_html
  • wcs_braintree_change_payment_method_template
  • wcs_braintree_copy_address_from_order
  • wcs_braintree_copy_order_meta
  • wcs_braintree_create_order_from_subscription
  • wcs_braintree_create_renewal_order
  • wcs_braintree_create_subscription
  • wcs_braintree_frontend_interval_string
  • wcs_braintree_get_billing_schedule_string
  • wcs_braintree_get_interval_string
  • wcs_braintree_get_length_string
  • wcs_braintree_get_period_string
  • wcs_braintree_get_plan_from_product
  • wcs_braintree_get_plan_options
  • wcs_braintree_get_plans
  • wcs_braintree_get_price_string
  • wcs_braintree_get_product_price_html
  • wcs_braintree_get_recurring_cart_key
  • wcs_braintree_get_recurring_total_text
  • wcs_braintree_get_related_orders
  • wcs_braintree_get_subscription
  • wcs_braintree_get_subscription_actions
  • wcs_braintree_get_subscription_from_recurring_cart_key
  • wcs_braintree_get_subscription_status_name
  • wcs_braintree_get_subscription_statuses
  • wcs_braintree_get_subscription_statuses_for_registration
  • wcs_braintree_get_subscriptions_for_order
  • wcs_braintree_get_subscriptions_for_user
  • wcs_braintree_is_change_payment_method_request
  • wcs_braintree_localize_scripts
  • wcs_braintree_order_contains_subscription
  • wcs_braintree_order_details
  • wcs_braintree_product_is_subscription
  • wcs_braintree_subscription_cancelled
  • wcs_braintree_subscription_length_string
  • wcs_braintree_subscription_user_actions
  • wcs_braintree_subscriptions_template
  • wcs_braintree_sync_subscription_with_gateway
  • wcs_braintree_view_subscription_template
  • wcs_braintree_webhook_subscription_cancelled
  • wcs_braintree_webhook_subscription_charged_successfully
  • wcs_braintree_webhook_subscription_expired
  • wcs_braintree_webhook_subscription_past_due
   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  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269  270  271  272  273  274  275  276  277  278  279  280  281  282  283  284  285  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302  303  304  305  306  307  308  309  310  311  312  313  314  315  316  317  318  319  320  321  322  323  324  325  326  327  328  329  330  331  332  333  334  335  336  337  338  339  340  341  342  343  344  345  346  347  348  349  350  351  352  353  354  355  356  357  358  359  360  361  362  363  364  365  366  367  368  369  370  371  372  373  374  375  376  377  378  379  380  381  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413  414  415  416  417  418  419  420  421  422  423  424  425  426  427  428  429  430  431  432  433  434  435  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  539  540  541  542  543  544  545  546  547  548  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596  597  598  599  600  601  602  603  604  605  606  607  608  609  610  611  612  613  614  615  616  617  618  619  620  621  622  623  624  625  626  627  628  629  630  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 
<?php
defined( 'ABSPATH' ) || exit();

use \PaymentPlugins\WC_Braintree_Constants as Constants;

/**
 *
 * @param unknown $interval
 *
 * @return mixed
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_billing_interval_string( $interval = null ) {
    $intervals = wcs_braintree_billing_intervals();
    $string    = array();
    foreach ( $intervals as $index => $text ) {
        $remainder = $index % 10;
        if ( ! in_array( $index, array( 11, 12, 13 ) ) ) {
            switch ( $remainder ) {
                case 1:
                    $suffix = __( 'st', 'woo-payment-gateway' );
                    break;
                case 2:
                    $suffix = __( 'nd', 'woo-payment-gateway' );
                    break;
                case 3:
                    $suffix = __( 'rd', 'woo-payment-gateway' );
                    break;
                default:
                    $suffix = __( 'th', 'woo-payment-gateway' );
                    break;
            }
        } else {
            $suffix = __( 'th', 'woo-payment-gateway' );
        }
        $string[ $index ] = sprintf( $intervals[ $index ], $index, $suffix );
    }

    return apply_filters( 'wcs_braintree_billing_interval_string', $interval ? $string[ $interval ] : $string, $interval );
}

/**
 *
 * @return mixed
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_subscription_length_string() {
    $lengths = array();

    $lengths[0] = __( 'never expires', 'woo-payment-gateway' );
    foreach ( range( 1, 48 ) as $i ) {
        $lengths[ $i ] = sprintf( _n( '%s month', '%s months', $i, 'woo-payment-gateway' ), $i );
    }

    return apply_filters( 'wcs_braintree_subscription_length_string', $lengths );
}

/**
 * Return an array of options that can be used to represent formatted billing periods.
 *
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_billing_periods_string( $type = 'singular', $period = null ) {
    $periods = wcs_braintree_billing_periods( $type );

    return apply_filters( 'wcs_braintree_billing_periods_string', $period == null ? 'month' : $periods[ $period ], $type, $period );
}

/**
 *
 * @param string $type
 *
 * @return mixed
 * @since 3.0.0
 * @package Braintree\Functions
 */
function wcs_braintree_billing_periods( $type = 'singular' ) {
    $i       = $type === 'plural' ? 2 : 1;
    $periods = array(
        'day'   => _n( 'day', 'days', $i, 'woo-payment-gateway' ),
        'month' => _n( 'month', 'months', $i, 'woo-payment-gateway' ),
    );

    return apply_filters( 'wcs_braintree_billing_periods', $periods );
}

/**
 *
 * @return mixed
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_billing_intervals() {
    $count     = 24;
    $intervals = array();
    for ( $i = 1; $i <= $count; $i ++ ) {
        $intervals[ $i ] = _n( 'every month', 'every %1$s%2$s month', $i, 'woo-payment-gateway' );
    }

    // Allow plugins to add more intervals if necessary.
    return apply_filters( 'wcs_braintree_billing_intervals', $intervals );
}

/**
 *
 * @param string $env
 *
 * @return mixed|boolean
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_plans( $env = '' ) {
    $env = empty( $env ) ? wc_braintree_environment() : $env;

    return get_option( "wc_braintree_{$env}_plans", array() );
}

/**
 *
 * @param string $env
 *
 * @return string[]
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_plan_options( $env = '' ) {
    $plans   = wcs_braintree_get_plans( $env );
    $options = array();
    foreach ( $plans as $plan ) {
        $options[ $plan['id'] ] = sprintf( '%1$s (%2$s)', $plan['id'], $plan['currencyIsoCode'] );
    }

    return $options;
}

/**
 *
 * @param WC_Product $product
 * @param number $quantity
 * @param string $price_html
 *
 * @return string
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_billing_schedule_string( $product, $quantity = 1, $price_html = '' ) {
    $interval   = $product->get_subscription_period_interval();
    $period     = $product->get_subscription_period();
    $type       = $interval > 1 ? 'plural' : 'singluar';
    $price_html .= sprintf( __( ' every %1$s', 'woo-payment-gateway' ), $interval > 1 ? sprintf( '%1$s %2$s', $interval, wcs_braintree_billing_periods_string( $type, $period ) ) : wcs_braintree_billing_periods_string( $type, $period ) );
    if ( $product->get_subscription_length() ) {
        $price_html .= wcs_braintree_get_length_string( $product->get_subscription_length(), $product->get_subscription_period() );
    }
    if ( $product->get_subscription_trial_length() ) {
        $price_html .= sprintf( __( ' with a %1$s-%2$s free trial', 'woo-payment-gateway' ), $product->get_subscription_trial_length(), $product->get_subscription_trial_period() );
    }
    if ( $product->get_subscription_sign_up_fee() ) {
        $price_html .= sprintf( __( ' and a %1$s sign up fee', 'woo-payment-gateway' ), wc_price( $product->get_subscription_sign_up_fee() * $quantity ) );
    }

    return $price_html;
}

/**
 *
 * @param WC_Product $product
 * @param int $price
 * @param int $quantity
 *          quantity of the products that should be included in the product price.
 *
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_get_product_price_html( $product, $price = 0, $quantity = 1 ) {
    if ( is_numeric( $price ) ) {
        $price = $price * $quantity;
    }
    $price_html = $price . '<span class="subscription-Details"> ' . wcs_braintree_get_billing_schedule_string( $product, $quantity ) . '</span>';

    return apply_filters( 'wcs_braintree_get_product_price_html', $price_html, $price, $product, $quantity );
}

/**
 *
 * @param int $length
 * @param string $period
 *          day, month, etc
 *
 * @return string
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_get_length_string( $length, $period ) {
    $type = $length > 1 ? 'plural' : 'singular';

    return apply_filters( 'wcs_braintree_get_length_string', $length > 0 ? sprintf( __( ' for %1$s %2$s', 'woo-payment-gateway' ), $length, wcs_braintree_billing_periods_string( $type, $period ) ) : '', $length, $period );
}

/**
 * Return true if the product is a Braintree Subscription.
 *
 * @param int|WC_Product $product
 *
 * @return boolean
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_product_is_subscription( $product ) {
    if ( ! is_object( $product ) && ! is_int( $product ) ) {
        return false;
    }
    // ensure the product is an object.
    if ( ! is_object( $product ) ) {
        $product = wc_get_product( $product );
    }

    return apply_filters(
        'wcs_braintree_product_is_subscription',
        $product && $product->is_type(
            array(
                'braintree-subscription',
                'braintree-variable-subscription',
                'braintree-subscription-variation',
            )
        )
    );
}

/**
 * Determine if the WC_Cart contains a Braintree subscription product.
 *
 * @return boolean
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_cart_contains_subscription() {
    if ( ! empty( WC()->cart->cart_contents ) ) {
        foreach ( WC()->cart->get_cart() as $key => $item ) {
            $product = $item['data'];
            if ( wcs_braintree_product_is_subscription( $product ) ) {
                return true;
            }
        }
    }

    return false;
}

/**
 *
 * @param WC_Product $product
 *
 * @return mixed string|WP_Error
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_plan_from_product( $product ) {
    if ( ! is_object( $product ) ) {
        $product = wc_get_product( $product );
    }
    $product_plans = $product->get_plans();
    $plans         = wcs_braintree_get_plans();
    $currency      = get_woocommerce_currency();
    foreach ( $product_plans as $id ) {
        if ( isset( $plans[ $id ] ) && $plans[ $id ]['currencyIsoCode'] === $currency ) {
            return $id;
        }
    }

    return '';
}

/**
 *
 * @return DateTime
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_calculate_start_date() {
    $date = new DateTime( null, new DateTimeZone( 'UTC' ) );

    return $date;
}

/**
 * Calculate the first payment date of the subscription.
 * The date is returned in UTC.
 *
 * @param WC_Product $product
 *
 * @return DateTime
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_calculate_first_payment_date( $trial_period = 'day', $trial_length = null ) {
    $date_in_utc = new DateTime( null, new DateTimeZone( 'UTC' ) );
    // has trial period so add time.
    if ( $trial_length ) {
        switch ( $trial_period ) {
            case 'day':
                $date_in_utc->add( new DateInterval( 'P' . $trial_length . 'D' ) );
                break;
            case 'month':
                $date_in_utc->add( new DateInterval( 'P' . $trial_length . 'M' ) );
                break;
        }
    }

    return $date_in_utc;
}

/**
 *
 * @param DateTime $date
 * @param string $period
 * @param string $interval
 *
 * @return mixed
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_calculate_next_payment_date( $date, $period, $interval ) {
    $next_payment_date = clone $date;
    switch ( $period ) {
        case 'day':
            $next_payment_date->add( new DateInterval( 'P' . $interval . 'D' ) );
            break;
        case 'month':
            $period = 'M';
            // calculate the date.
            // set the day of month to first day that way adding months, won't affect february.
            $next_payment_date->setDate( $next_payment_date->format( 'Y' ), $next_payment_date->format( 'm' ), 1 );
            $next_payment_date->add( new DateInterval( "P{$interval}{$period}" ) );
            $current_day           = $date->format( 'j' ); // day of month, no leading zeros
            $days_in_date          = $date->format( 't' );
            $days_in_current_month = $next_payment_date->format( 't' ); // days in month

            /**
             * current date's billing day exceeds the next billing dates days in month, or this is the last day.
             */
            if ( $current_day > $days_in_current_month || $days_in_date === $current_day ) {
                // days of previous month were greater so add as last day.
                $next_payment_date->setDate( $next_payment_date->format( 'Y' ), $next_payment_date->format( 'm' ), $days_in_current_month );
            } else {
                $next_payment_date->setDate( $next_payment_date->format( 'Y' ), $next_payment_date->format( 'm' ), $current_day );
            }
            break;
        default:
            $period = 'M';
    }

    return apply_filters( 'wcs_braintree_calculate_next_payment_date', $next_payment_date, $date, $period, $interval );
}

/**
 *
 * @param number $subscription_length
 * @param string $subscription_period
 * @param DateTime $first_payment
 *
 * @return DateTime
 * @since 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_calculate_end_date( $subscription_length = 0, $subscription_period = 'month', $trial_period = 'day', $trial_length = 0 ) {
    if ( ! $subscription_length ) {
        return 0;
    }
    $end_date = clone wcs_braintree_calculate_first_payment_date( $trial_period, $trial_length );

    $day_of_month        = $end_date->format( 'j' );
    $days_in_first_month = $end_date->format( 't' );

    // set date to first day of month
    $end_date->setDate( $end_date->format( 'Y' ), $end_date->format( 'm' ), 1 );

    switch ( $subscription_period ) {
        case 'month':
            $end_date->add( new DateInterval( 'P' . $subscription_length . 'M' ) );
            break;
    }

    if ( $trial_length ) {
        if ( $day_of_month > $end_date->format( 't' ) ) {
            $end_date->setDate( $end_date->format( 'Y' ), $end_date->format( 'm' ), $end_date->format( 't' ) );
        } else {
            $end_date->setDate( $end_date->format( 'Y' ), $end_date->format( 'm' ), $day_of_month );
        }
    } else {
        /**
         * subscription started on last day of month or start day is greater than number of days in end month,
         * so make sure end date is on last day of month
         */
        if ( ( $day_of_month === $days_in_first_month && $day_of_month > 28 ) || $day_of_month > $end_date->format( 't' ) ) {
            $end_date->setDate( $end_date->format( 'Y' ), $end_date->format( 'm' ), $end_date->format( 't' ) );
        } else {
            $end_date->setDate( $end_date->format( 'Y' ), $end_date->format( 'm' ), $day_of_month );
        }
    }

    return $end_date;
}

/**
 * Return the string for the subscription price.
 *
 * @param WC_Cart $cart
 *
 * @return <div> <strong>$20 every month for 14 months</strong>
 *         </div>
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_cart_subtotal_string( $cart ) {
    $interval = $cart->subscription_period_interval;
    $period   = $cart->subscription_period;
    $text     = sprintf( '<span class="subscription-Details">%s</span>', wcs_braintree_frontend_interval_string( $interval, $period ) );
    if ( $length = $cart->subscription_length ) {
        $text .= wcs_braintree_get_length_string( $length, $period );
    }

    return apply_filters( 'wcs_braintree_cart_subtotal_string', sprintf( '%s %s', $cart->get_cart_subtotal(), $text ), $cart );
}

/**
 * Get recurring coupon html
 *
 * @param WC_Coupon $coupon
 * @param WC_Cart $recurring_cart
 *
 * @since 3.2.1
 */
function wcs_braintree_cart_totals_coupon_html( $coupon, $recurring_cart ) {
    if ( is_string( $coupon ) ) {
        $coupon = new WC_Coupon( $coupon );
    }
    $discount      = $recurring_cart->get_coupon_discount_amount( $coupon->get_code(), $recurring_cart->display_cart_ex_tax );
    $discount_html = wcs_braintree_get_recurring_total_text( - 1 * $discount, $recurring_cart );

    if ( $coupon->get_free_shipping() && empty( $discount ) ) {
        $discount_html = __( 'Free shipping coupon', 'woocommerce' );
    }

    $discount_html = $discount_html . ' <a href="' . esc_url( add_query_arg( 'remove_coupon', rawurlencode( $coupon->get_code() ), Automattic\Jetpack\Constants::is_defined( 'WOOCOMMERCE_CHECKOUT' ) ? wc_get_checkout_url() : wc_get_cart_url() ) ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->get_code() ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';

    return apply_filters( 'wcs_braintree_cart_totals_coupon_html', $discount_html, $coupon, $recurring_cart );
}

/**
 * Returns the interval string for the given interval and period.
 *
 * @param int $interval
 * @param string $period
 *
 * @return string every 3rd month for 12 months. / month for 9 months.
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_get_interval_string( $interval, $period ) {
    return apply_filters( 'wcs_braintree_get_interval_string', sprintf( __( '%s', 'woo-payment-gateway' ), $interval > 1 ? wcs_braintree_billing_interval_string( $interval ) : sprintf( __( '/ %s', 'woo-payment-gateway' ), wcs_braintree_get_period_string( $period ) ) ), $interval, $period );
}

/**
 *
 * @param string $period
 *
 * @return string
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_period_string( $period = 'month' ) {
    $periods = array(
        'day'   => __( 'day', 'woo-payment-gateway' ),
        'month' => __( 'month', 'woo-payment-gateway' ),
    );

    return $periods[ $period ];
}

/**
 *
 * @param string $interval
 * @param string $period
 *
 * @return string
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_frontend_interval_string( $interval, $period ) {
    return sprintf( __( 'every %s', 'woo-payment-gateway' ), $interval > 1 ? sprintf( '%s %s', $interval, wcs_braintree_billing_periods_string( 'plural', $period ) ) : wcs_braintree_billing_periods_string( 'singular', $period ) );
}

/**
 * Generate the html for the cart shipping displayed on the cart page.
 *
 * @param WC_Cart $cart
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_cart_shipping_total( $cart ) {
    $text = $cart->get_cart_shipping_total() . ' ' . wcs_braintree_frontend_interval_string( $cart->subscription_period_interval, $cart->subscription_period );

    return sprintf( '%s %s', $text, wcs_braintree_get_length_string( $cart->subscription_length, $cart->subscription_period ) );
}

/**
 *
 * @param WC_Cart $cart
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_cart_recurring_tax_html( $cart ) {
    return apply_filters( 'wcs_braintree_cart_recurring_tax_html', wcs_braintree_get_recurring_total_text( $cart->get_taxes_total(), $cart ) );
}

/**
 *
 * @param float $total
 * @param WC_Cart $cart
 */
function wcs_braintree_get_recurring_total_text( $total, $cart ) {
    $text = is_numeric( $total ) ? wc_price( $total ) : $total;
    $text = sprintf( '%s %s', $text, wcs_braintree_frontend_interval_string( $cart->subscription_period_interval, $cart->subscription_period ) );
    if ( $length = $cart->subscription_length ) {
        $text = sprintf( '%s %s', $text, wcs_braintree_get_length_string( $cart->subscription_length, $cart->subscription_period ) );
    }

    return $text;
}

/**
 * Return html for the recurring total portion displayed on the cart page.
 *
 * @param WC_Cart $cart
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_cart_recurring_total_html( $cart ) {
    $total           = $cart->get_total();
    $recurring_total = sprintf( '%s %s', $total, wcs_braintree_frontend_interval_string( $cart->subscription_period_interval, $cart->subscription_period ) );
    if ( $length = $cart->subscription_length ) {
        $recurring_total = sprintf( '%s %s', $recurring_total, wcs_braintree_get_length_string( $cart->subscription_length, $cart->subscription_period ) );
    }

    return apply_filters( 'wcs_braintree_cart_recurring_total_html', $recurring_total );
}

/**
 * Return a formatted date.
 * The timezone given is the timezone in which the formatted string will be converted to. All given times
 * should be in UTC to ensure proper conversion.
 *
 * @param DateTime|string $date
 * @param string $timezone
 * @param string $format
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_cart_formatted_date( $date, $timezone = 'UTC', $format = 'F j, Y' ) {
    $date = $date instanceof DateTime ? $date : new DateTime( $date, new DateTimeZone( 'UTC' ) );
    $date->setTimezone( new DateTimeZone( $timezone ) );

    return date_format( $date, $format );
}

/**
 * Return html for the tax total portion displayed on the cart page.
 *
 * @param WC_Tax $tax
 * @param WC_Cart $cart
 *
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_cart_tax_total_html( $tax, $cart ) {
    $recurring_total = sprintf( '%1$s %2$s', $tax->formatted_amount, wcs_braintree_frontend_interval_string( $cart->subscription_period_interval, $cart->subscription_period ) );
    if ( $cart->subscription_length ) {
        $recurring_total = sprintf( '%1$s %2$s', $recurring_total, wcs_braintree_get_length_string( $cart->subscription_length, $cart->subscription_period ) );
    }

    return apply_filters( 'wcs_braintree_cart_tax_total_html', $recurring_total );
}

/**
 *
 * @param int|WC_Order $order
 *
 * @return WC_Braintree_Subscription[]
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_subscriptions_for_order( $order ) {
    if ( is_object( $order ) && is_a( $order, 'WC_Braintree_Subscription' ) ) {
        return array( $order );
    }
    if ( $order instanceof WC_Order ) {
        $order_id = $order->get_id();
    } else {
        $order_id = $order;
    }

    $args = array(
        'post_type'      => 'bfwc_subscription',
        'posts_per_page' => - 1,
        'post_parent'    => $order_id,
        'post_status'    => 'any',
    );

    $posts = get_posts( $args );
    if ( ! $posts ) {
        return array();
    }
    $subscriptions = array();

    foreach ( $posts as $post ) {
        $subscriptions[] = wcs_braintree_get_subscription( $post->ID );
    }

    return apply_filters( 'wcs_braintree_get_subscriptions_for_order', $subscriptions, $order );
}

/**
 * Return a subscription object from the given id.
 *
 * @param int $id
 *
 * @return WC_Braintree_Subscription
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_subscription( $id ) {
    $subscription = WC()->order_factory->get_order( $id );

    return apply_filters( 'wcs_braintree_get_subscription', $subscription );
}

/**
 * Create the subscription within the WordPress database.
 *
 * @param array $args
 *
 * @return WC_Braintree_Subscription
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_create_subscription( $args ) {
    $post_args = array(
        'post_type'   => 'bfwc_subscription',
        'post_author' => 0,
        'post_status' => 'wc-pending',
        'post_parent' => absint( $args['order_id'] ),
    );

    $post_id = wp_insert_post( $post_args );

    if ( is_wp_error( $post_id ) || $post_id === 0 ) {
        return new WP_Error( 'subscription-error', __( 'There was an error creating the subscription.', 'woo-payment-gateway' ) );
    }
    $subscription = WC()->order_factory->get_order( $post_id );
    $subscription->set_props(
        array(
            'start_date'                   => $args['start_date'],
            'next_payment_date'            => $args['next_payment_date'],
            'trial_end_date'               => $args['trial_end_date'],
            'end_date'                     => $args['end_date'],
            'subscription_trial_length'    => $args['subscription_trial_length'],
            'subscription_trial_period'    => $args['subscription_trial_period'],
            'first_payment_date'           => $args['first_payment_date'],
            'braintree_plan'               => $args['braintree_plan'],
            'merchant_account_id'          => $args['merchant_account_id'],
            'subscription_period'          => $args['subscription_period'],
            'subscription_period_interval' => $args['subscription_period_interval'],
            'subscription_length'          => $args['subscription_length'],
        )
    );
    $subscription->set_currency( $args['order_currency'] );
    $subscription->set_customer_id( $args['customer_user'] );
    $subscription->save();

    return $subscription;
}

/**
 *
 * @param WC_Order $order
 * @param WC_Braintree_Subscription $subscription
 * @param string $address_type
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_copy_address_from_order( $order, $subscription, $address_type = 'all' ) {
    $address_types = $address_type === 'all' ? array( 'billing', 'shipping' ) : array( $address_type );

    $address_fields = array(
        'first_name',
        'last_name',
        'company',
        'address_1',
        'address_2',
        'city',
        'state',
        'postcode',
        'country',
        'email',
        'phone'
    );

    foreach ( $address_types as $type ) {

        foreach ( $address_fields as $field_key ) {
            $field_var = sprintf( '%1$s_%2$s', $type, $field_key );
            if ( method_exists( $order, 'get_' . $field_var ) ) {
                $address[ $field_key ] = $order->{'get_' . $field_var}();
            }
        }

        $subscription->set_address( $address, $type );
    }
    $subscription->save();

    return $subscription;
}

/**
 *
 * @param WC_Braintree_Subscription $subscription
 *
 * @return WP_Error|boolean|WC_Order|WP_Error
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_create_renewal_order( $subscription ) {
    if ( ! is_object( $subscription ) ) {
        $subscription = wcs_braintree_get_subscription( $subscription );
    }

    $order = wcs_braintree_create_order_from_subscription( $subscription );

    if ( ! $order ) {
        return new WP_Error();
    }

    return $order;
}

/**
 *
 * @param int|WC_Braintree_Subscription $subscription
 *
 * @package Braintree/Functions
 * @since 3.0.0
 * @version 3.0.0
 */
function wcs_braintree_create_order_from_subscription( $subscription ) {
    global $wpdb;
    try {
        if ( ! is_object( $subscription ) ) {
            $subscription = wcs_braintree_get_subscription( $subscription );
        }

        $items = $subscription->get_items( array( 'line_item', 'fee', 'shipping', 'tax', 'coupon' ) );

        $renewal_order = wc_create_order( array( 'customer_id' => $subscription->get_user_id() ) );

        // add all of the items from the subscription to the new order.
        foreach ( $items as $item_id => $item ) {
            /**
             *
             * @var WC_Order_Item $item
             */
            $item_id  = wc_add_order_item(
                $renewal_order->get_id(),
                array(
                    'order_item_name' => $item->get_name(),
                    'order_item_type' => $item->get_type(),
                )
            );
            $new_item = $renewal_order->get_item( $item_id );
            foreach ( $item->get_meta_data() as $meta_data ) {
                $new_item->update_meta_data( $meta_data->key, $meta_data->value );
            }
            $order_itemmeta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d", $item->get_id() ) );
            foreach ( $order_itemmeta as $meta ) {
                wc_update_order_item_meta( $item_id, $meta->meta_key, $meta->meta_value );
            }
            $renewal_order->add_item( $new_item );
        }

        // copy all the metadata from the subscription to the order.
        wcs_braintree_copy_order_meta( $subscription, $renewal_order );

        $renewal_order->update_meta_data( '_renewal_order', true );
        $renewal_order->update_meta_data( '_subscription_id', $subscription->get_id() );
        $renewal_order->save();

        return $renewal_order;
    } catch ( Exception $e ) {
        return false;
    }
}

/**
 * Copy meta data from one order to another.
 *
 * @param WC_Order $from
 * @param WC_Order $to
 *
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_copy_order_meta( $from, $to ) {
    global $wpdb;
    $query   = $wpdb->prepare(
        "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d
            AND meta_key NOT LIKE '%%_date' AND meta_key NOT LIKE '_subscription%%' AND meta_key NOT IN ('_created_in_braintree', '_transaction_id', '_order_key')",
        $from->get_id()
    );
    $results = $wpdb->get_results( $query );

    foreach ( $results as $result ) {
        update_post_meta( $to->get_id(), $result->meta_key, maybe_unserialize( $result->meta_value ) );
    }
}

/**
 * Returns true if the order contains a Braintree Subscription.
 *
 * @param int|WC_Order $order
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_order_contains_subscription( $order ) {
    if ( ! is_object( $order ) ) {
        $order = wc_get_order( $order );
    }
    if ( ! $order instanceof WC_Braintree_Subscription && count( wcs_braintree_get_subscriptions_for_order( $order ) ) > 0 ) {
        return true;
    }

    return false;
}

/**
 *
 * @param string $total_string
 * @param string $interval
 * @param string $period
 *
 * @return mixed
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_price_string( $total_string, $interval, $period ) {
    $string       = __( '%1$s every %2$s', 'woo-payment-gateway' );
    $price_string = sprintf( $string, $total_string, $interval > 1 ? sprintf( '%1$s %2$s', $interval, wcs_braintree_billing_periods_string( 'plural', $period ) ) : wcs_braintree_billing_periods_string( 'singular', $period ) );

    return apply_filters( 'wcs_braintree_get_price_string', $price_string, $total_string, $period, $interval );
}

/**
 * Return the nice name for the status provided.
 * If the status is invalid, then the status provided will
 * be returned.
 *
 * @param unknown $status
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_get_subscription_status_name( $status ) {
    $status   = strpos( $status, 'wc-' ) === false ? 'wc-' . $status : $status;
    $statuses = wp_parse_args( wc_get_order_statuses(), wcs_braintree_get_subscription_statuses() );

    return isset( $statuses[ $status ] ) ? $statuses[ $status ] : $status;
}

/**
 *
 * @return mixed
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_get_subscription_statuses_for_registration() {
    return apply_filters(
        'wcs_braintree_get_subscription_statuses',
        array(
            'wc-active'   => array(
                'label'                     => __( 'Active', 'woo-payment-gateway' ),
                'public'                    => true,
                'exclude_from_search'       => false,
                'show_in_admin_all_list'    => true,
                'show_in_admin_status_list' => true,
                'label_count'               => _n_noop( 'Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', 'woo-payment-gateway' ),
            ),
            'wc-expired'  => array(
                'label'                     => __( 'Expired', 'woo-payment-gateway' ),
                'public'                    => true,
                'exclude_from_search'       => false,
                'show_in_admin_all_list'    => true,
                'show_in_admin_status_list' => true,
                'label_count'               => _n_noop( 'Expired <span class="count">(%s)</span>', 'Expired <span class="count">(%s)</span>', 'woo-payment-gateway' ),
            ),
            'wc-past-due' => array(
                'label'                     => __( 'Past Due', 'woo-payment-gateway' ),
                'public'                    => true,
                'exclude_from_search'       => false,
                'show_in_admin_all_list'    => true,
                'show_in_admin_status_list' => true,
                'label_count'               => _n_noop(
                    'Past Due <span class="count">(%s)</span>',
                    'Past Due <span class="count">(%s)</span>',
                    'woo-payment-gateway'
                ),
            ),
        )
    );
}

/**
 *
 * @param WC_Order $order
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_order_details( $order ) {
    if ( wc_braintree_subscriptions_active() && wcs_braintree_order_contains_subscription( $order ) ) {
        wc_braintree_get_template( 'order/subscription-details.php', array( 'subscriptions' => wcs_braintree_get_subscriptions_for_order( $order ) ) );
    }
}

/**
 *
 * @param int $id
 *
 * @package Braintree/Functions
 * @since 3.0.0
 * @version 3.0.0
 */
function wcs_braintree_view_subscription_template( $id ) {
    $subscription = wc_get_order( $id );
    if ( ! $subscription ) {
        wc_add_notice( __( 'Invalid subscription.', 'woo-payment-gateway' ), 'error' );
        wc_print_notices();

        return;
    }
    wcs_braintree_sync_subscription_with_gateway( $subscription );
    wc_braintree_get_template( 'myaccount/view-subscription.php', array( 'subscription' => $subscription ) );
}

/**
 *
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_subscriptions_template() {
    global $wp;
    $current_page  = ! empty( $wp->query_vars['subscriptions'] ) ? absint( $wp->query_vars['subscriptions'] ) : 1;
    $subscriptions = wcs_braintree_get_subscriptions_for_user( get_current_user_id() );
    $max_pages     = ceil( count( $subscriptions ) / get_option( 'posts_per_page' ) );
    $subscriptions = array_slice( $subscriptions, ( ( $current_page - 1 ) * get_option( 'posts_per_page' ) ), get_option( 'posts_per_page' ) );

    wc_braintree_get_template(
        'myaccount/my-subscriptions.php',
        array(
            'subscriptions' => $subscriptions,
            'current_page'  => $current_page,
            'max_pages'     => $max_pages,
        )
    );
}

/**
 *
 * @param int $id
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_change_payment_method_template( $id ) {
    $subscription = wc_get_order( $id );
    if ( ! $subscription ) {
        wc_add_notice( __( 'Invalid subscription.', 'woo-payment-gateway' ), 'error' );
        wc_print_notices();

        return;
    }
    wc_add_notice( __( 'Please choose a new payment method.', 'woo-payment-gateway' ), 'notice' );
    do_action( 'wcs_braintree_pre_change_payment_method_template' );
    wc_braintree_get_template(
        'myaccount/change-payment-method.php',
        array(
            'subscription'       => $subscription,
            'available_gateways' => WC()->payment_gateways()->get_available_payment_gateways(),
        )
    );
}

/**
 *
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_is_change_payment_method_request() {
    global $wp;

    return is_account_page() && isset( $wp->query_vars['change-payment-method'] );
}

/**
 *
 * @param WC_Braintree_Subscription $subscription
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_get_subscription_actions( $subscription ) {
    $actions = array(
        'view' => array(
            'url'   => wc_get_endpoint_url( 'view-subscription', $subscription->get_id(), wc_get_page_permalink( 'myaccount' ) ),
            'label' => __( 'View', 'woo-payment-gateway' ),
        ),
    );

    return apply_filters( 'wcs_braintree_get_subscription_actions', $actions );
}

/**
 * return an array of user actions that pertain to the subscription.
 *
 * @param WC_Braintree_Subscription $subscription
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_subscription_user_actions( $subscription ) {
    $actions = array();
    if ( $subscription->has_status( 'active' ) || $subscription->has_status( 'past-due' ) ) {
        $actions['cancel']                = array(
            'label' => __( 'Cancel', 'woo-payment-gateway' ),
            'url'   => wp_nonce_url( wc_get_endpoint_url( 'cancel-subscription', $subscription->get_id(), wc_get_page_permalink( 'myaccount' ) ) ),
        );
        $actions['change_payment_method'] = array(
            'label' => __( 'Change Payment Method', 'woo-payment-gateway' ),
            'url'   => wc_get_endpoint_url( 'change-payment-method', $subscription->get_id(), wc_get_page_permalink( 'myaccount' ) ),
        );
    } elseif ( $subscription->has_status( 'pending' ) ) {
        $actions['pay_for_subscription'] = array(
            'label' => __( 'Pay', 'woo-payment-gateway' ),
            'url'   => $subscription->get_checkout_payment_url(),
        );
    }

    return apply_filters( 'wcs_braintree_subscription_user_actions', $actions, $subscription );
}

/**
 *
 * @param WC_Braintree_Subscription|int $subscription
 *
 * @return WC_Order[]
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_related_orders( $subscription ) {
    global $wpdb;
    if ( ! is_object( $subscription ) ) {
        $subscription = wcs_braintree_get_subscription( $subscription );
    }
    $results = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT ID FROM $wpdb->posts AS posts LEFT JOIN $wpdb->postmeta AS postmeta
            ON  posts.ID = postmeta.post_id WHERE posts.post_type = 'shop_order' AND postmeta.meta_key = '_subscription_id'
            AND postmeta.meta_value = %s",
            $subscription->get_id()
        )
    );
    $orders  = array();
    if ( $subscription->get_parent_id() ) {
        $orders[] = $subscription->get_order( $subscription->get_parent_id() );
    }
    foreach ( $results as $result ) {
        $orders[] = wc_get_order( $result->ID );
    }

    return $orders;
}

/**
 * Retrieve subscriptions for the given user.
 *
 * @param number $user_id
 *
 * @return WC_Braintree_Subscription[]
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_get_subscriptions_for_user( $user_id = 0, $args = array() ) {
    $user_id       = empty( $user_id ) ? get_current_user_id() : $user_id;
    $posts         = get_posts(
        array_merge(
            array(
                'post_type'      => 'bfwc_subscription',
                'post_status'    => array_merge( array_keys( wcs_braintree_get_subscription_statuses() ), array_keys( wc_get_order_statuses() ) ),
                'posts_per_page' => - 1,
                'orderby'        => 'date',
                'order'          => 'DESC',
                'meta_query'     => array(
                    array(
                        'key'   => '_customer_user',
                        'value' => $user_id,
                    ),
                ),
            ),
            $args
        )
    );
    $subscriptions = array();
    foreach ( $posts as $post ) {
        $subscriptions[] = wcs_braintree_get_subscription( $post->ID );
    }

    return $subscriptions;
}

/**
 * Snyc the WP database dates with the dates stored in Braintree to keep everything consistant.
 *
 * @param WC_Braintree_Subscription $subscription
 *
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_sync_subscription_with_gateway( $subscription ) {
    try {
        $gateway = braintree()->gateway( wc_braintree_get_order_environment( $subscription ) );
        if ( $gateway ) {
            $braintree_subscription = $gateway->subscription()->find( $subscription->get_id() );
            $next_payment_date      = clone $braintree_subscription->nextBillingDate;
            $old_date               = $subscription->get_date( 'next_payment' );
            $next_payment_date->setTime( $old_date->format( 'H' ), $old_date->format( 'i' ), $old_date->format( 's' ) );
            $subscription->set_next_payment_date( $next_payment_date );
            if ( $braintree_subscription->transactions ) {
                $latest_transaction = $braintree_subscription->transactions[0];
                $previous_payment   = clone $latest_transaction->createdAt;
                $previous_payment->setTime( $old_date->format( 'H' ), $old_date->format( 'i' ), $old_date->format( 's' ) );
                $subscription->set_previous_payment_date( $previous_payment );
            }
            $subscription->save();
        }
    } catch ( \Braintree\Exception $e ) {
        wc_braintree_log_error( sprintf( __( 'Error syncing subscription %1$s. Reason: %1$s', 'woo-payment-gateway' ), wc_braintree_errors_from_object( $e ) ) );
    } catch ( Exception $e ) {
        wc_braintree_log_error( sprintf( __( 'Error syncing subscription %1$s. Reason: %1$s', 'woo-payment-gateway' ), wc_braintree_errors_from_object( $e ) ) );
    }

    return $subscription;
}

/**
 *
 * @param array $items
 * @param array $endpoints
 *
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_account_menu_items( $items, $endpoints = array() ) {
    $wc_braintree_endpoints = array( 'subscriptions' => braintree()->subscription_settings->get_option( 'subscriptions_endpoint' ) );
    $labels                 = array( 'subscriptions' => __( 'Subscriptions', 'woo-payment-gateway' ) );
    foreach ( $wc_braintree_endpoints as $endpoint => $value ) {
        if ( empty( $value ) ) {
            unset( $labels[ $endpoint ] );
        }
    }
    // if possible we want subscriptions to appear right under orders in the navigation.
    if ( isset( $labels['subscriptions'], $items['orders'] ) ) {
        $position = array_search( 'orders', array_keys( $items ) );
        $items    = array_merge( array_slice( $items, 0, $position + 1 ), $labels, array_slice( $items, $position + 1 ) );
    } else {
        $items = array_merge( $items, $labels );
    }

    return $items;
}

/**
 *
 * @return mixed
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_get_subscription_statuses() {
    return apply_filters(
        'wcs_braintree_get_subscription_statuses',
        array(
            'wc-active'   => __( 'Active', 'woo-payment-gateway' ),
            'wc-expired'  => __( 'Expired', 'woo-payment-gateway' ),
            'wc-past-due' => __(
                'Past Due',
                'woo-payment-gateway'
            ),
        )
    );
}

/**
 *
 * @param int $id
 * @param WC_Braintree_Subscription $subscription
 *
 * @version 3.0.0
 * @package Braintree/Functions
 */
function wcs_braintree_subscription_cancelled( $id, $subscription ) {
    /**
     *
     * @var WC_Braintree_Payment_Gateway $gateway
     */
    $gateway = WC()->payment_gateways()->payment_gateways()[ $subscription->get_payment_method() ];
    if ( $gateway ) {
        try {
            $gateway->connect( wc_braintree_get_order_environment( $subscription ) );
            $response = $gateway->gateway->subscription()->cancel( $subscription->get_id() );
            if ( $response->success ) {
                $subscription->add_order_note( __( 'Subscription has been cancelled in Braintree.', 'woo-payment-gateway' ) );
            } else {
                $subscription->add_order_note( sprintf( __( 'Error cancelling subscription in Braintree. Reason: %1$s', 'woo-payment-gateway' ), wc_braintree_errors_from_object( $response ) ) );
            }
        } catch ( \Braintree\Exception $e ) {
            $subscription->add_order_note( sprintf( __( 'Error cancelling subscription in Braintree. Reason: %1$s.', 'woo-payment-gateway' ), wc_braintree_errors_from_object( $response ) ) );
        }
        $gateway->connect();
    }
}

/**
 *
 * @param \Braintree\WebhookNotification $notification
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_webhook_subscription_cancelled( $notification ) {
    $id           = absint( $notification->subscription->id );
    $subscription = wcs_braintree_get_subscription( $id );
    // subscription has been cancelled in Braintree so avoid trying to cancel again.
    remove_action( 'wcs_braintree_subscription_status_cancelled', 'wcs_braintree_subscription_cancelled' );
    $subscription->update_status( 'cancelled' );
}

/**
 *
 * @param \Braintree\WebhookNotification $notification
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_webhook_subscription_past_due( $notification ) {
    $id           = absint( $notification->subscription->id );
    $subscription = wc_get_order( $id );
    $subscription->update_status( 'past-due' );
}

/**
 *
 * @param \Braintree\WebhookNotification $notification
 *
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_webhook_subscription_expired( $notification ) {
    $id           = absint( $notification->subscription->id );
    $subscription = wc_get_order( $id );
    $subscription->update_status( 'expired' );
}

/**
 * Create a renewal order associated with the subscription payment.
 *
 * @param \Braintree\WebhookNotification $notification
 *
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_webhook_subscription_charged_successfully( $notification ) {
    $id                     = absint( $notification->subscription->id );
    $braintree_subscription = $notification->subscription;
    $subscription           = wc_get_order( $id );
    $transactions           = $braintree_subscription->transactions;
    $transaction            = count( $transactions ) > 0 ? $transactions[0] : null;
    if ( $transaction ) {
        global $wpdb;
        // look for an existing order associated with this transaction.
        $result = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts as posts INNER JOIN $wpdb->postmeta as postmeta ON postmeta.post_id = posts.ID WHERE posts.post_type = 'shop_order' AND postmeta.meta_key = '_transaction_id' AND postmeta.meta_value = %s", $transaction->id ) );
        if ( ! $result ) {
            $next_billing_date = clone $braintree_subscription->nextBillingDate;
            $last_payment      = clone $transaction->createdAt;
            $start_date        = $subscription->get_date( 'start' );
            $next_billing_date->setTime( $start_date->format( 'H' ), $start_date->format( 'i' ), $start_date->format( 's' ) );
            $last_payment->setTime( $start_date->format( 'H' ), $start_date->format( 'i' ), $start_date->format( 's' ) );

            $subscription->update_date( 'next_payment', $next_billing_date );
            $subscription->update_date( 'last_payment', $last_payment );

            // create renewal order.
            $renewal_order = wcs_braintree_create_renewal_order( $subscription );
            $renewal_order->update_meta_data( Constants::TRANSACTION_STATUS, $transaction->status );
            if ( $transaction->status === \Braintree\Transaction::AUTHORIZED ) {
                $renewal_order->update_meta_data( '_authorization_exp_at', $transaction->authorizationExpiresAt->getTimestamp() );
            }
            $renewal_order->payment_complete( $transaction->id );
            $renewal_order->save();
            $renewal_order->add_order_note( sprintf( __( 'Renewal order created via webhook for subscription %s', 'woo-payment-gateway' ), $subscription->get_id() ) );
            $subscription->add_order_note( sprintf( __( 'Renewal order %s created from Braintree webhook.', 'woo-payment-gateway' ), $renewal_order->get_id() ) );
            $subscription->update_status( 'active' );
        } else {
            $subscription->add_order_note( sprintf( __( 'Renewal order %1$s already created for transaction %2$s.', 'woo-payment-gateway' ), $result->ID, $transaction->id ) );
        }
    }
}

/**
 *
 * @param WC_Product $product
 *
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_get_recurring_cart_key( $product ) {
    $plan_id = wcs_braintree_get_plan_from_product( $product );

    /**
     * recurring cart key should create uniquiness based on parameters like subscription plan, billing period, trial length, trial period, etc
     * Ex: silver_plan_2_month_interval_5_days_trial_for_10
     */
    $key = sprintf( '%1$s_%2$s_%3$s_interval_%4$s_%5$s_trial_for_%6$s', $plan_id, $product->get_subscription_period_interval(), $product->get_subscription_period(), $product->get_subscription_trial_period(), $product->get_subscription_trial_length(), $product->get_subscription_length() );
    if ( ! braintree()->subscription_settings->is_active( 'combine' ) ) {
        $key = $key . '_' . wc_rand_hash();
    }

    return apply_filters( 'wcs_braintree_get_recurring_cart_key', md5( str_replace( ' ', '_', $key ) ), $product );
}

/**
 *
 * @param int $item_id
 *
 * @package Braintree/Functions
 * @since 3.0.0
 */
function wcs_braintree_before_delete_order_item( $item_id ) {
    // prevent infinite loop
    remove_action( 'woocommerce_before_delete_order_item', 'wcs_braintree_before_delete_order_item' );
    $data_store = WC_Data_Store::load( 'order-item' );
    $order_id   = $data_store->get_order_id_by_order_item_id( $item_id );
    $order      = wc_get_order( $order_id );
    /**
     *
     * @var WC_Order_Item $item
     */
    $removed_item = $order->get_item( $item_id );

    if ( $order->get_type() === 'bfwc_subscription' ) {
        $order_to_remove_from = wc_get_order( $order->get_parent_id() );
        foreach ( $order_to_remove_from->get_items( 'line_item' ) as $item ) {
            if ( $item->get_product()->get_id() === $removed_item->get_product()->get_id() ) {
                wc_delete_order_item( $item->get_id() );
                $order_to_remove_from->calculate_totals();
                break;
            }
        }
    } elseif ( wcs_braintree_order_contains_subscription( $order ) ) {
        $key          = wcs_braintree_get_recurring_cart_key( $removed_item->get_product() );
        $subscription = wcs_braintree_get_subscription_from_recurring_cart_key( $key, $order->get_id() );
        if ( $subscription ) {
            foreach ( $subscription->get_items( 'line_item' ) as $item ) {
                if ( $item->get_product()->get_id() === $removed_item->get_product()->get_id() ) {
                    wc_delete_order_item( $item->get_id() );
                    $subscription->calculate_totals();
                    break;
                }
            }
        }
    }
}

/**
 *
 * @param string $key
 * @param int $parent_id
 *
 * @return WC_Braintree_Subscription
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_get_subscription_from_recurring_cart_key( $key, $parent_id ) {
    global $wpdb;
    $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts as posts INNER JOIN $wpdb->postmeta AS postmeta ON posts.ID = postmeta.post_id WHERE posts.post_parent = %d AND postmeta.meta_key = '_recurring_cart_key' AND postmeta.meta_value = %s", $parent_id, $key ) );

    return wcs_braintree_get_subscription( $post_id );
}

/**
 *
 * @param array $data
 * @param string $name
 *
 * @return string[][]|NULL[][]
 * @package Braintree/Functions
 * @version 3.0.0
 */
function wcs_braintree_localize_scripts( $data, $name ) {
    global $wp;
    switch ( $name ) {
        case 'view-subscription':
            $subscription_id = absint( $wp->query_vars['view-subscription'] );
            $data            = array(
                'messages' => array(
                    'cancel_confirmation' => sprintf( __( 'Please click OK if you wish to cancel subscription %1$s.', 'woo-payment-gateway' ), $subscription_id ),
                    'cancel'              => __( 'Cancel', 'woo-payment-gateway' ),
                    'confirm'             => __( 'Confirm', 'woo-payment-gateway' ),
                    'title'               => sprintf( __( 'Subscription #%1$s', 'woo-payment-gateway' ), $subscription_id ),
                ),
            );
            break;
    }

    return $data;
}
Braintree API documentation generated by ApiGen