• Namespace
  • Class
  • Tree
  • Deprecated
  • Hook Reference

Namespaces

  • None
  • PaymentPlugins

Classes

  • PaymentPlugins\WC_Stripe_Admin_Meta_Box_Product_Data
  • PaymentPlugins\WC_Stripe_Admin_Notices
  • WC_Payment_Gateway_Stripe
  • WC_Payment_Gateway_Stripe_ACH
  • WC_Payment_Gateway_Stripe_Afterpay
  • WC_Payment_Gateway_Stripe_Alipay
  • WC_Payment_Gateway_Stripe_ApplePay
  • WC_Payment_Gateway_Stripe_Bancontact
  • WC_Payment_Gateway_Stripe_BECS
  • WC_Payment_Gateway_Stripe_Boleto
  • WC_Payment_Gateway_Stripe_CC
  • WC_Payment_Gateway_Stripe_EPS
  • WC_Payment_Gateway_Stripe_FPX
  • WC_Payment_Gateway_Stripe_Giropay
  • WC_Payment_Gateway_Stripe_GooglePay
  • WC_Payment_Gateway_Stripe_GrabPay
  • WC_Payment_Gateway_Stripe_Ideal
  • WC_Payment_Gateway_Stripe_Klarna
  • WC_Payment_Gateway_Stripe_Local_Payment
  • WC_Payment_Gateway_Stripe_Multibanco
  • WC_Payment_Gateway_Stripe_OXXO
  • WC_Payment_Gateway_Stripe_P24
  • WC_Payment_Gateway_Stripe_Payment_Request
  • WC_Payment_Gateway_Stripe_Sepa
  • WC_Payment_Gateway_Stripe_Sofort
  • WC_Payment_Gateway_Stripe_WeChat
  • WC_Payment_Token_Stripe
  • WC_Payment_Token_Stripe_ACH
  • WC_Payment_Token_Stripe_ApplePay
  • WC_Payment_Token_Stripe_CC
  • WC_Payment_Token_Stripe_GooglePay
  • WC_Payment_Token_Stripe_Local
  • WC_Payment_Token_Stripe_Sepa
  • WC_Stripe_Account_Settings
  • WC_Stripe_Admin_Assets
  • WC_Stripe_Admin_Menus
  • WC_Stripe_Admin_Order_Metaboxes
  • WC_Stripe_Admin_Settings
  • WC_Stripe_Admin_User_Edit
  • WC_Stripe_Advanced_Settings
  • WC_Stripe_API_Operation
  • WC_Stripe_API_Request_Filter
  • WC_Stripe_API_Settings
  • WC_Stripe_Constants
  • WC_Stripe_Controller_Cart
  • WC_Stripe_Controller_Checkout
  • WC_Stripe_Controller_Gateway_Settings
  • WC_Stripe_Controller_GooglePay
  • WC_Stripe_Controller_Order_Actions
  • WC_Stripe_Controller_Payment_Intent
  • WC_Stripe_Controller_Payment_Method
  • WC_Stripe_Controller_Plaid
  • WC_Stripe_Controller_Product_Data
  • WC_Stripe_Controller_Source
  • WC_Stripe_Controller_Webhook
  • WC_Stripe_Customer_Manager
  • WC_Stripe_Field_Manager
  • WC_Stripe_Frontend_Scripts
  • WC_Stripe_Gateway
  • WC_Stripe_Gateway_Conversion
  • WC_Stripe_Install
  • WC_Stripe_Manager
  • WC_Stripe_Payment
  • WC_Stripe_Payment_Balance
  • WC_Stripe_Payment_Charge
  • WC_Stripe_Payment_Charge_Local
  • WC_Stripe_Payment_Factory
  • WC_Stripe_Payment_Intent
  • WC_Stripe_Product_Gateway_Option
  • WC_Stripe_Product_Option
  • WC_Stripe_Redirect_Handler
  • WC_Stripe_Rest_API
  • WC_Stripe_Rest_Controller
  • WC_Stripe_Settings_API
  • WC_Stripe_Shortcode_Payment_Buttons
  • WC_Stripe_Shortcodes
  • WC_Stripe_Update
  • WC_Stripe_Utils

Traits

  • WC_Payment_Token_Payment_Method_Trait
  • WC_Payment_Token_Source_Trait
  • WC_Stripe_Controller_Cart_Trait
  • WC_Stripe_Controller_Frontend_Trait
  • WC_Stripe_Local_Payment_Charge_Trait
  • WC_Stripe_Local_Payment_Intent_Trait
  • WC_Stripe_Payment_Charge_Trait
  • WC_Stripe_Payment_Intent_Trait
  • WC_Stripe_Settings_Trait

Functions

  • stripe_wc
  • wc_stripe_add_allowed_html
  • wc_stripe_add_number_precision
  • wc_stripe_api_options
  • wc_stripe_available_payment_gateways
  • wc_stripe_charge_dispute_closed
  • wc_stripe_charge_dispute_created
  • wc_stripe_credit_card_labels
  • wc_stripe_delete_customer
  • wc_stripe_display_prices_including_tax
  • wc_stripe_filter_address_state
  • wc_stripe_filter_order_id
  • wc_stripe_get_account_id
  • wc_stripe_get_available_local_gateways
  • wc_stripe_get_checkout_fields
  • wc_stripe_get_currencies
  • wc_stripe_get_current_page
  • wc_stripe_get_custom_forms
  • wc_stripe_get_customer_id
  • wc_stripe_get_customer_payment_tokens
  • wc_stripe_get_display_items
  • wc_stripe_get_error_messages
  • wc_stripe_get_idempotency_key
  • wc_stripe_get_local_payment_params
  • wc_stripe_get_order_from_source_id
  • wc_stripe_get_order_from_transaction
  • wc_stripe_get_publishable_key
  • wc_stripe_get_secret_key
  • wc_stripe_get_shipping_options
  • wc_stripe_get_site_locale
  • wc_stripe_get_template
  • wc_stripe_get_template_html
  • wc_stripe_hidden_field
  • wc_stripe_log
  • wc_stripe_log_error
  • wc_stripe_log_info
  • wc_stripe_mode
  • wc_stripe_order_cancelled
  • wc_stripe_order_has_shipping_address
  • wc_stripe_order_mode
  • wc_stripe_order_status_completed
  • wc_stripe_output_checkout_error
  • wc_stripe_payment_complete_order_status
  • wc_stripe_payment_gateways
  • wc_stripe_payment_intent_field
  • wc_stripe_pre_orders_active
  • wc_stripe_process_charge_failed
  • wc_stripe_process_charge_succeeded
  • wc_stripe_process_create_refund
  • wc_stripe_process_payment_intent_succeeded
  • wc_stripe_process_shop_subscription_meta
  • wc_stripe_process_source_chargeable
  • wc_stripe_remove_number_precision
  • wc_stripe_remove_order_locks
  • wc_stripe_restore_cart
  • wc_stripe_restore_cart_after_product_checkout
  • wc_stripe_retry_source_chargeable
  • wc_stripe_review_closed
  • wc_stripe_review_opened
  • wc_stripe_save_customer
  • wc_stripe_set_checkout_error
  • wc_stripe_set_idempotency_key
  • wc_stripe_shipping_address_serviceable
  • wc_stripe_stash_cart
  • wc_stripe_stash_product_cart
  • wc_stripe_token_field
  • wc_stripe_update_customer_location
  • wc_stripe_update_option_3_3_13
  • wc_stripe_update_shipping_methods
  • wc_stripe_woocommerce_payment_token_deleted
  • wcs_stripe_active
   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 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 
<?php

defined( 'ABSPATH' ) || exit();

if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
    return;
}

require_once( WC_STRIPE_PLUGIN_FILE_PATH . 'includes/class-wc-stripe-payment-intent.php' );
require_once( WC_STRIPE_PLUGIN_FILE_PATH . 'includes/class-wc-stripe-payment-charge.php' );
require_once( WC_STRIPE_PLUGIN_FILE_PATH . 'includes/class-wc-stripe-payment-charge-local.php' );

require_once( WC_STRIPE_PLUGIN_FILE_PATH . 'includes/traits/wc-stripe-payment-traits.php' );

/**
 *
 * @since   3.0.0
 * @author  PaymentPlugins
 * @package Stripe/Abstract
 *
 */
abstract class WC_Payment_Gateway_Stripe extends WC_Payment_Gateway {

    use WC_Stripe_Settings_Trait;

    /**
     *
     * @var WC_Stripe_Payment
     */
    public $payment_object;

    /**
     * @var bool
     * @since 3.1.8
     */
    protected $has_digital_wallet = false;

    /**
     *
     * @var string
     */
    public $token_key;

    /**
     *
     * @var string
     */
    public $saved_method_key;

    /**
     *
     * @var string
     */
    public $payment_type_key;

    /**
     *
     * @var string
     */
    public $payment_intent_key;

    /**
     *
     * @var string
     */
    public $save_source_key;

    /**
     *
     * @var string
     */
    public $template_name;

    /**
     *
     * @var bool
     */
    protected $checkout_error = false;

    /**
     * Used to create an instance of a WC_Payment_Token
     *
     * @var string
     */
    protected $token_type;

    /**
     *
     * @var WC_Stripe_Gateway
     */
    protected $gateway;

    /**
     *
     * @var WP_Error
     */
    protected $wp_error;

    /**
     *
     * @var string
     */
    public $payment_method_token = null;

    /**
     *
     * @var string
     */
    protected $new_source_token = null;

    /**
     * Is the payment method synchronous or asynchronous
     *
     * @var bool
     */
    public $synchronous = true;

    /**
     *
     * @var array
     */
    protected $post_payment_processes = array();

    /**
     *
     * @var bool
     */
    public $processing_payment = false;

    /**
     * @var WP_Error
     */
    public $last_payment_error;

    public function __construct() {
        $this->token_key          = $this->id . '_token_key';
        $this->saved_method_key   = $this->id . '_saved_method_key';
        $this->save_source_key    = $this->id . '_save_source_key';
        $this->payment_type_key   = $this->id . '_payment_type_key';
        $this->payment_intent_key = $this->id . '_payment_intent_key';
        $this->has_fields         = true;
        $this->init_form_fields();
        $this->init_settings();
        $this->title       = $this->get_option( 'title_text' );
        $this->description = $this->get_option( 'description' );
        $this->hooks();
        $this->init_supports();
        $this->gateway = WC_Stripe_Gateway::load();

        $this->payment_object = $this->get_payment_object();
    }

    public function hooks() {
        add_filter( 'wc_stripe_settings_nav_tabs', array( $this, 'admin_nav_tab' ) );
        add_action( 'woocommerce_stripe_settings_checkout_' . $this->id, array( $this, 'enqueue_admin_scripts' ) );
        add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
        add_filter( 'woocommerce_payment_methods_list_item', array( $this, 'payment_methods_list_item' ), 10, 2 );
        add_action( 'wc_stripe_payment_token_deleted_' . $this->id, array( $this, 'delete_payment_method' ), 10, 2 );
        add_filter( 'woocommerce_subscription_payment_meta', array( $this, 'subscription_payment_meta' ), 10, 2 );
        add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'scheduled_subscription_payment' ), 10, 2 );
        add_action( 'woocommerce_subscription_failing_payment_method_updated_' . $this->id, array( $this, 'update_failing_payment_method' ), 10, 2 );
        add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array( $this, 'process_pre_order_payment' ) );

        /**
         * @since 3.1.8
         */
        add_filter( 'wc_stripe_mini_cart_dependencies', array( $this, 'get_mini_cart_dependencies' ), 10, 2 );
    }

    public function init_supports() {
        $this->supports = array(
            'tokenization',
            'products',
            'subscriptions',
            'add_payment_method',
            'subscription_cancellation',
            'multiple_subscriptions',
            'subscription_amount_changes',
            'subscription_date_changes',
            'default_credit_card_form',
            'refunds',
            'pre-orders',
            'subscription_payment_method_change_admin',
            'subscription_reactivation',
            'subscription_suspension',
            'subscription_payment_method_change_customer',
        );
    }

    public function init_form_fields() {
        $this->form_fields = include stripe_wc()->plugin_path() . 'includes/gateways/settings/' . str_replace( array( 'stripe_', '_' ), array(
                '',
                '-'
            ), $this->id ) . '-settings.php';
        $this->form_fields = apply_filters( 'wc_stripe_form_fields_' . $this->id, $this->form_fields );
    }

    public function get_method_formats() {
        $class_name = 'WC_Payment_Token_' . $this->token_type;
        if ( class_exists( $class_name ) ) {
            /**
             *
             * @var WC_Payment_Token_Stripe
             */
            $token = new $class_name();

            return $token->get_formats();
        }

        return array();
    }

    public function enqueue_admin_scripts() {
    }

    public function payment_fields() {
        $this->enqueue_frontend_scripts();
        wc_stripe_token_field( $this );
        wc_stripe_payment_intent_field( $this );
        $this->output_display_items( 'checkout' );
        wc_stripe_get_template(
            'checkout/stripe-payment-method.php',
            array(
                'gateway' => $this,
                'tokens'  => is_add_payment_method_page() ? null : $this->get_tokens(),
            )
        );
    }

    /**
     * Output the product payment fields.
     */
    public function product_fields() {
        global $product;
        $this->enqueue_frontend_scripts( 'product' );
        $this->output_display_items( 'product' );
        wc_stripe_get_template(
            'product/' . $this->template_name,
            array(
                'gateway' => $this,
                'product' => $product,
            )
        );
    }

    public function cart_fields() {
        $this->enqueue_frontend_scripts( 'cart' );
        $this->output_display_items( 'cart' );
        wc_stripe_get_template( 'cart/' . $this->template_name, array( 'gateway' => $this ) );
    }

    public function mini_cart_fields() {
        $this->output_display_items( 'cart' );
        wc_stripe_get_template( 'mini-cart/' . $this->template_name, array( 'gateway' => $this ) );
    }

    /**
     * Enqueue scripts needed by the gateway on the frontend of the WC shop.
     *
     * @param WC_Stripe_Frontend_Scripts $scripts
     */
    public function enqueue_frontend_scripts( $page = '' ) {
        global $wp;
        if ( $page ) {
            if ( 'product' === $page ) {
                $this->enqueue_product_scripts( stripe_wc()->scripts() );
            } elseif ( 'cart' === $page ) {
                $this->enqueue_cart_scripts( stripe_wc()->scripts() );
            } elseif ( 'checkout' === $page ) {
                $this->enqueue_checkout_scripts( stripe_wc()->scripts() );
            } elseif ( 'mini_cart' === $page ) {
                $this->enqueue_mini_cart_scripts( stripe_wc()->scripts() );
            } else {
                $this->enqueue_frontend_scripts();
            }
        } else {
            if ( is_add_payment_method_page() ) {
                $this->enqueue_add_payment_method_scripts( stripe_wc()->scripts() );
            }
            if ( is_checkout() ) {
                $this->enqueue_checkout_scripts( stripe_wc()->scripts() );
            }
            if ( is_cart() ) {
                $this->enqueue_cart_scripts( stripe_wc()->scripts() );
            }
            if ( is_product() ) {
                $this->enqueue_product_scripts( stripe_wc()->scripts() );
            }
        }
        if ( ! empty( stripe_wc()->scripts()->enqueued_scripts ) ) {
            wp_enqueue_style( stripe_wc()->scripts()->prefix . 'styles', stripe_wc()->scripts()->assets_url( 'css/stripe.css' ), array(), stripe_wc()->version() );
            wp_style_add_data( stripe_wc()->scripts()->prefix . 'styles', 'rtl', 'replace' );
        }
    }

    /**
     * Enqueue scripts needed by the gateway on the checkout page.
     *
     * @param WC_Stripe_Frontend_Scripts $scripts
     */
    public function enqueue_checkout_scripts( $scripts ) {
    }

    /**
     * Enqueue scripts needed by the gateway on the add payment method page.
     *
     * @param WC_Stripe_Frontend_Scripts $scripts
     */
    public function enqueue_add_payment_method_scripts( $scripts ) {
        $this->enqueue_checkout_scripts( $scripts );
    }

    /**
     * Enqueue scripts needed by the gateway on the cart page.
     *
     * @param WC_Stripe_Frontend_Scripts $scripts
     */
    public function enqueue_cart_scripts( $scripts ) {
    }

    /**
     * Enqueue scripts needed by the gateway on the product page.
     *
     * @param WC_Stripe_Frontend_Scripts $scripts
     */
    public function enqueue_product_scripts( $scripts ) {
    }

    /**
     * @param WC_Stripe_Frontend_Scripts $scripts
     *
     * @since 3.1.8
     */
    public function enqueue_mini_cart_scripts( $scripts ) {
        if ( ! wp_script_is( $scripts->get_handle( 'mini-cart' ) ) ) {
            $scripts->enqueue_script( 'mini-cart',
                $scripts->assets_url( 'js/frontend/mini-cart.js' ),
                apply_filters( 'wc_stripe_mini_cart_dependencies', array( $scripts->get_handle( 'wc-stripe' ) ), $scripts ) );
        }
        $scripts->localize_script( 'mini-cart', $this->get_localized_params(), 'wc_' . $this->id . '_mini_cart_params' );
    }

    /**
     *
     * {@inheritDoc}
     *
     * @see WC_Payment_Gateway::process_payment()
     */
    public function process_payment( $order_id ) {
        $order = wc_get_order( $order_id );

        if ( $this->is_change_payment_method_request() && wcs_is_subscription( $order ) ) {
            return $this->process_change_payment_method_request( $order );
        }

        do_action( 'wc_stripe_before_process_payment', $order, $this->id );

        if ( wc_notice_count( 'error' ) > 0 ) {
            return $this->get_order_error();
        }
        $this->processing_payment = true;

        if ( $this->order_contains_pre_order( $order ) && $this->pre_order_requires_tokenization( $order ) ) {
            return $this->process_pre_order( $order );
        }

        // if order total is zero, then save meta but don't process payment.
        if ( $order->get_total() == 0 ) {
            return $this->process_zero_total_order( $order );
        }

        $result = $this->payment_object->process_payment( $order, $this );

        if ( is_wp_error( $result ) ) {
            wc_add_notice( $this->is_active( 'generic_error' ) ? $this->get_generic_error( $result ) : $result->get_error_message(), 'error' );

            return $this->get_order_error( $result );
        }

        if ( $result->complete_payment ) {
            $this->payment_object->payment_complete( $order, $result->charge );
            $this->trigger_post_payment_processes( $order, $this );
            WC()->cart->empty_cart();

            return array(
                'result'   => 'success',
                'redirect' => $this->get_return_url( $order ),
            );
        } else {
            return array(
                'result'   => 'success',
                'redirect' => $result->redirect,
            );
        }
    }

    /**
     *
     * @return array
     */
    public function get_localized_params() {
        return array(
            'gateway_id'            => $this->id,
            'api_key'               => wc_stripe_get_publishable_key(),
            'saved_method_selector' => '[name="' . $this->saved_method_key . '"]',
            'token_selector'        => '[name="' . $this->token_key . '"]',
            'messages'              => array(
                'terms'          => __( 'Please read and accept the terms and conditions to proceed with your order.', 'woocommerce' ),
                'required_field' => __( 'Please fill out all required fields.', 'woo-stripe-payment' )
            ),
            'routes'                => array(
                'setup_intent'     => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->payment_intent->rest_uri( 'setup-intent' ) ),
                'sync_intent'      => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->payment_intent->rest_uri( 'sync-payment-intent' ) ),
                'add_to_cart'      => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->cart->rest_uri( 'add-to-cart' ) ),
                'cart_calculation' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->cart->rest_uri( 'cart-calculation' ) ),
                'shipping_method'  => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->cart->rest_uri( 'shipping-method' ) ),
                'shipping_address' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->cart->rest_uri( 'shipping-address' ) ),
                'checkout'         => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->checkout->rest_uri( 'checkout' ) ),
                'checkout_payment' => WC_Stripe_Rest_API::get_endpoint( stripe_wc()->rest_api->checkout->rest_uri( 'checkout/payment' ) )
            ),
            'rest_nonce'            => wp_create_nonce( 'wp_rest' ),
            'banner_enabled'        => $this->banner_checkout_enabled(),
            'currency'              => get_woocommerce_currency(),
            'total_label'           => __( 'Total', 'woo-stripe-payment' ),
            'country_code'          => wc_get_base_location()['country'],
            'user_id'               => get_current_user_id(),
            'description'           => $this->get_description(),
            'elementOptions'        => $this->get_element_options()
        );
    }

    /**
     * Save the Stripe data to the order.
     *
     * @param WC_Order       $order
     * @param \Stripe\Charge $charge
     */
    public function save_order_meta( $order, $charge ) {
        /**
         *
         * @var WC_Payment_Token_Stripe $token
         */
        $token = $this->get_payment_token( $this->get_payment_method_from_charge( $charge ), $charge->payment_method_details );
        $order->set_transaction_id( $charge->id );
        $order->set_payment_method_title( $token->get_payment_method_title() );
        $order->update_meta_data( WC_Stripe_Constants::MODE, wc_stripe_mode() );
        $order->update_meta_data( WC_Stripe_Constants::CHARGE_STATUS, $charge->status );
        $order->update_meta_data( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN, $token->get_token() );

        // if WCS is active and there are subscriptions in the order, save meta data
        if ( wcs_stripe_active() && wcs_order_contains_subscription( $order ) ) {
            foreach ( wcs_get_subscriptions_for_order( $order ) as $subscription ) {
                $subscription->set_transaction_id( $charge->id );
                $subscription->set_payment_method_title( $token->get_payment_method_title() );
                $subscription->update_meta_data( WC_Stripe_Constants::MODE, wc_stripe_mode() );
                $subscription->update_meta_data( WC_Stripe_Constants::CHARGE_STATUS, $charge->status );
                $subscription->update_meta_data( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN, $token->get_token() );
                $subscription->update_meta_data( WC_Stripe_Constants::CUSTOMER_ID, wc_stripe_get_customer_id( $order->get_user_id() ) );
                $subscription->save();
            }
        }
        /**
         * @param WC_Order                  $order
         * @param WC_Payment_Gateway_Stripe $this
         * @param \Stripe\Charge            $charge
         *
         * @since 3.2.7
         */
        do_action( 'wc_stripe_save_order_meta', $order, $this, $charge );

        $order->save();
    }

    /**
     * Given a charge object, return the ID of the payment method used for the charge.
     *
     * @param \Stripe\Charge $charge
     *
     * @since 3.0.6
     */
    public function get_payment_method_from_charge( $charge ) {
        return $this->payment_object->get_payment_method_from_charge( $charge );
    }

    /**
     *
     * {@inheritDoc}
     *
     * @see WC_Payment_Gateway::add_payment_method()
     */
    public function add_payment_method() {
        $user_id = get_current_user_id();
        try {
            if ( ! is_user_logged_in() ) {
                throw new Exception( __( 'User must be logged in.', 'woo-stripe-payment' ) );
            }

            $customer_id = wc_stripe_get_customer_id( $user_id );

            if ( empty( $customer_id ) ) {
                $customer_id = $this->create_customer( $user_id );
            }

            $result = $this->create_payment_method( $this->get_new_source_token(), $customer_id );

            if ( is_wp_error( $result ) ) {
                throw new Exception( $result->get_error_message() );
            }
            $result->set_user_id( $user_id );
            $result->save();
            WC_Payment_Tokens::set_users_default( $user_id, $result->get_id() );

            do_action( 'wc_stripe_add_payment_method_success', $result );

            return array(
                'result'   => 'success',
                'redirect' => wc_get_account_endpoint_url( 'payment-methods' ),
            );
        } catch ( Exception $e ) {
            wc_add_notice( sprintf( __( 'Error saving payment method. Reason: %s', 'woo-stripe-payment' ), $e->getMessage() ), 'error' );

            return array( 'result' => 'error' );
        }
    }

    /**
     *
     * {@inheritDoc}
     *
     * @see WC_Payment_Gateway::process_refund()
     */
    public function process_refund( $order_id, $amount = null, $reason = '' ) {
        $order  = wc_get_order( $order_id );
        $result = $this->payment_object->process_refund( $order, $amount );

        if ( ! is_wp_error( $result ) ) {
            $order->add_order_note(
                sprintf(
                    __( 'Order refunded in Stripe. Amount: %s', 'woo-stripe-payment' ),
                    wc_price(
                        $amount,
                        array(
                            'currency' => $order->get_currency(),
                        )
                    )
                )
            );
        }

        return $result;
    }

    /**
     * Captures the charge for the Stripe order.
     *
     * @param float    $amount
     * @param WC_Order $order
     */
    public function capture_charge( $amount, $order ) {
        $result = $this->gateway->charges->mode( wc_stripe_order_mode( $order ) )->retrieve( $order->get_transaction_id() );

        if ( is_wp_error( $result ) ) {
            return;
        } else {
            if ( ! $result->captured ) {
                $result = $this->payment_object->capture_charge( $amount, $order );

                if ( ! is_wp_error( $result ) ) {
                    remove_action( 'woocommerce_order_status_completed', 'wc_stripe_order_status_completed' );
                    WC_Stripe_Utils::add_balance_transaction_to_order( $result, $order, true );
                    if ( isset( $result->refunds->data[0] ) ) {
                        $balance_transaction = $this->gateway->balanceTransactions->retrieve( $result->refunds->data[0]->balance_transaction );
                        if ( ! is_wp_error( $balance_transaction ) ) {
                            WC_Stripe_Utils::update_balance_transaction( $balance_transaction, $order, true );
                        }
                    }
                    $order->payment_complete();
                    $order->add_order_note( sprintf( __( 'Order amount captured in Stripe. Amount: %s', 'woo-stripe-payment' ),
                            wc_price( $amount, array( 'currency' => $order->get_currency(), ) ) )
                    );
                } else {
                    $order->add_order_note( sprintf( __( 'Error capturing charge in Stripe. Reason: %s', 'woo-stripe-payment' ), $result->get_error_message() ) );
                }
            }
        }

        return $result;
    }

    /**
     * Void the Stripe charge.
     *
     * @param WC_Order $order
     */
    public function void_charge( $order ) {
        // @3.1.1 - check added so errors aren't encountered if the order can't be voided
        if ( ! $this->payment_object->can_void_order( $order ) ) {
            return;
        }
        $result = $this->payment_object->void_charge( $order );

        if ( is_wp_error( $result ) ) {
            $order->add_order_note( sprintf( __( 'Error voiding charge. Reason: %s', 'woo-stripe-payment' ), $result->get_error_message() ) );
        } else {
            $order->add_order_note( __( 'Charge voided in Stripe.', 'woo-stripe-payment' ) );
        }
    }

    /**
     * Return the \Stripe\Charge object
     *
     * @param String $charge_id
     * @param String $mode
     *
     * @return WP_Error|\Stripe\Charge
     */
    public function retrieve_charge( $charge_id, $mode = '' ) {
        return $this->gateway->charges->mode( $mode )->retrieve( $charge_id );
    }

    /**
     *
     * @param string             $method_id
     * @param \Stripe\Card|array $method_details
     */
    public function get_payment_token( $method_id, $method_details = null ) {
        $class_name = 'WC_Payment_Token_' . $this->token_type;
        if ( class_exists( $class_name ) ) {
            /**
             *
             * @var WC_Payment_Token_Stripe $token
             */
            $token = new $class_name( '', $method_details );
            $token->set_token( $method_id );
            $token->set_gateway_id( $this->id );
            $token->set_format( $this->get_option( 'method_format' ) );
            $token->set_environment( wc_stripe_mode() );
            if ( $method_details ) {
                $token->details_to_props( $method_details );
            }

            return $token;
        }
    }

    /**
     * Return a failed order response.
     *
     * @param WP_Error $error
     *
     * @return array
     */
    public function get_order_error( $error = null ) {
        wc_stripe_set_checkout_error();
        $this->last_payment_error = $error;
        do_action( 'wc_stripe_process_payment_error', $error, $this );

        return array( 'result' => WC_Stripe_Constants::FAILURE, 'redirect' => '' );
    }

    /**
     * Return the payment source the customer has chosen to use.
     * This can be a saved source
     * or a one time use source.
     */
    public function get_payment_source() {
        if ( $this->use_saved_source() ) {
            return $this->get_saved_source_id();
        } else {
            if ( $this->payment_method_token ) {
                return $this->payment_method_token;
            }

            return $this->get_new_source_token();
        }
    }

    /**
     * Returns the payment method the customer wants to use.
     * This can be a saved payment method
     * or a new payment method.
     */
    public function get_payment_method_from_request() {
        return $this->get_payment_source();
    }

    public function get_payment_intent_id() {
        return ! empty( $_POST[ $this->payment_intent_key ] ) ? wc_clean( $_POST[ $this->payment_intent_key ] ) : '';
    }

    /**
     * Return true of the customer is using a saved payment method.
     */
    public function use_saved_source() {
        return ( ! empty( $_POST[ $this->payment_type_key ] ) && wc_clean( $_POST[ $this->payment_type_key ] ) === 'saved' ) || $this->payment_method_token
               || ( ! empty( $_POST["wc-{$this->id}-payment-token"] ) );
    }

    /**
     *
     * @deprecated
     *
     */
    public function get_new_source_id() {
        return $this->get_new_source_token();
    }

    public function get_new_source_token() {
        return null != $this->new_source_token ? $this->new_source_token : ( ! empty( $_POST[ $this->token_key ] ) ? wc_clean( $_POST[ $this->token_key ] ) : '' );
    }

    public function get_saved_source_id() {
        // Check if Blocks are being used
        if ( ! empty( $_POST["wc-{$this->id}-payment-token"] ) ) {
            $token = WC_Payment_Tokens::get( wc_clean( $_POST["wc-{$this->id}-payment-token"] ) );

            return $token->get_token();
        }
        if ( ! empty( $_POST[ $this->saved_method_key ] ) && ! empty( $_POST[ $this->payment_type_key ] ) && 'saved' == $_POST[ $this->payment_type_key ] ) {
            return wc_clean( $_POST[ $this->saved_method_key ] );
        }

        return $this->payment_method_token;
    }

    /**
     * Create a customer in the stripe gateway.
     *
     * @param int $user_id
     *
     * @throws Exception
     */
    public function create_customer( $user_id ) {
        $customer = WC()->customer;
        $response = WC_Stripe_Customer_Manager::instance()->create_customer( $customer );
        if ( ! is_wp_error( $response ) ) {
            wc_stripe_save_customer( $response->id, $user_id );
        } else {
            throw new Exception( $response->get_error_message() );
        }

        return $response->id;
    }

    /**
     * Creates a payment method in Stripe.
     *
     * @param string $id
     *          payment method id
     * @param string $customer_id
     *          WC Stripe customer ID
     *
     * @return WC_Payment_Token_Stripe|WP_Error
     */
    public function create_payment_method( $id, $customer_id ) {
        $token = $this->get_payment_token( $id );
        $token->set_customer_id( $customer_id );

        $result = $token->save_payment_method();

        if ( is_wp_error( $result ) ) {
            return $result;
        } else {
            $token->set_token( $result->id );
            $token->details_to_props( $result );

            return $token;
        }
    }

    /**
     *
     * @param array                      $item
     * @param WC_Payment_Token_Stripe_CC $payment_token
     */
    public function payment_methods_list_item( $item, $payment_token ) {
        if ( $payment_token->get_type() === $this->token_type ) {
            $item['method']['last4'] = $payment_token->get_last4();
            $item['method']['brand'] = ucfirst( $payment_token->get_brand() );
            if ( $payment_token->has_expiration() ) {
                $item['expires'] = sprintf( '%s / %s', $payment_token->get_exp_month(), $payment_token->get_exp_year() );
            } else {
                $item['expires'] = __( 'n/a', 'woo-stripe-payment' );
            }
            $item['wc_stripe_method'] = true;
        }

        return $item;
    }

    /**
     *
     * @param string                  $token_id
     * @param WC_Payment_Token_Stripe $token
     */
    public function delete_payment_method( $token_id, $token ) {
        $token->delete_from_stripe();
    }

    public function saved_payment_methods( $tokens = array() ) {
        wc_stripe_get_template(
            'payment-methods.php',
            array(
                'tokens'  => $tokens,
                'gateway' => $this,
            )
        );
    }

    public function get_new_method_label() {
        return __( 'New Card', 'woo-stripe-payment' );
    }

    public function get_saved_methods_label() {
        return __( 'Saved Cards', 'woo-stripe-payment' );
    }

    /**
     * Return true if shipping is needed.
     * Shipping is based on things like if the cart or product needs shipping.
     *
     * @return bool
     */
    public function get_needs_shipping() {
        if ( is_checkout() || is_cart() ) {
            global $wp;
            if ( wcs_stripe_active() && WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment ) {
                return false;
            }
            // return false if this is the order pay page. Gateways that have payment sheets don't need
            // to make any changes to the order.
            if ( ! empty( $wp->query_vars['order-pay'] ) ) {
                return false;
            }

            return WC()->cart->needs_shipping();
        }
        if ( is_product() ) {
            global $product;

            return is_a( $product, 'WC_Product' ) && $product->needs_shipping();
        }
    }

    /**
     * Return true of the payment method should be saved.
     *
     * @param WC_Order $order
     *
     * @return bool
     */
    public function should_save_payment_method( $order ) {
        $bool = false;
        if ( ! $this->use_saved_source() && ! $this->is_processing_scheduled_payment() ) {
            if ( wcs_stripe_active() && $this->supports( 'subscriptions' ) ) {
                if ( wcs_order_contains_subscription( $order ) || wcs_order_contains_renewal( $order ) ) {
                    $bool = true;
                }
            }
            if ( ! empty( $_POST[ $this->save_source_key ] ) ) {
                $bool = true;
            }
        }

        /**
         * @param bool                      $bool
         * @param WC_Order                  $order
         * @param WC_Payment_Gateway_Stripe $this
         *
         * @since 3.3.12
         */
        return apply_filters( 'wc_stripe_should_save_payment_method', $bool, $order, $this );
    }

    /**
     * Returns true if the save payment method checkbox can be displayed.
     *
     * @return boolean
     */
    public function show_save_source() {
        return false;
    }

    /**
     * Returns a formatted array of items for display in the payment gateway's payment sheet.
     *
     * @param stirng $page
     *
     * @return []
     */
    public function get_display_items( $page = 'checkout', $order = null ) {
        global $wp;
        $items = array();
        if ( in_array( $page, array( 'cart', 'checkout' ) ) ) {
            $items = $this->get_display_items_for_cart( WC()->cart );
        } elseif ( 'order_pay' === $page ) {
            $order = ! is_null( $order ) ? $order : wc_get_order( absint( $wp->query_vars['order-pay'] ) );
            $items = $this->get_display_items_for_order( $order );
        } elseif ( 'product' === $page ) {
            global $product;
            $items = array( $this->get_display_item_for_product( $product ) );
        }

        /**
         * @param array         $items
         * @param WC_Order|null $order
         * @param string        $page
         */
        return apply_filters( 'wc_stripe_get_display_items', $items, $order, $page );
    }

    /**
     * Returns a formatted array of shipping methods for display in the payment gateway's
     * payment sheet.
     *
     * @param bool $encode
     *
     * @return array
     * @deprecated
     */
    public function get_shipping_methods() {
        return $this->get_formatted_shipping_methods();
    }

    /**
     * Return true if product page checkout is enabled for this gateway
     *
     * @return bool
     */
    public function product_checkout_enabled() {
        return in_array( 'product', $this->get_option( 'payment_sections', array() ) );
    }

    /**
     * Return true if cart page checkout is enabled for this gateway
     *
     * @return bool
     */
    public function cart_checkout_enabled() {
        return in_array( 'cart', $this->get_option( 'payment_sections', array() ) );
    }

    /**
     * Return true if mini-cart checkout is enabled for this gateway
     *
     * @since 3.1.8
     * @return bool
     */
    public function mini_cart_enabled() {
        return in_array( 'mini_cart', $this->get_option( 'payment_sections', array() ) );
    }

    /**
     * Return true if checkout page banner is enabled for this gateway
     *
     * @return bool
     */
    public function banner_checkout_enabled() {
        global $wp;

        return empty( $wp->query_vars['order-pay'] ) && $this->supports( 'wc_stripe_banner_checkout' ) && in_array( 'checkout_banner', $this->get_option( 'payment_sections', array() ) );
    }

    /**
     * Decorate the response with data specific to the gateway.
     *
     * @param [] $data
     */
    public function add_to_cart_response( $data ) {
        return $data;
    }

    /**
     * Decorate the update shipping method reponse with data.
     *
     * @param [] $data
     */
    public function get_update_shipping_method_response( $data ) {
        return $data;
    }

    /**
     * Decorate the update shipping address respond with data.
     *
     * @param [] $data
     */
    public function get_update_shipping_address_response( $data ) {
        return apply_filters( 'wc_stripe_update_shipping_address_response', $data );
    }

    /**
     * Save the customer's payment method.
     * If the payment method has already been saved to the customer
     * then simply return true.
     *
     * @param string   $id
     * @param WC_Order $order
     * @param
     *
     * @return WP_Error|bool
     */
    public function save_payment_method( $id, $order, $payment_details = null ) {
        if ( $payment_details ) {
            $token = $this->get_payment_token( $id, $payment_details );
            $token->set_customer_id( wc_stripe_get_customer_id( $order->get_customer_id(), wc_stripe_order_mode( $order ) ) );
        } else {
            $token = $this->create_payment_method( $id, wc_stripe_get_customer_id( $order->get_customer_id() ) );
            if ( is_wp_error( $token ) ) {
                $this->wp_error = $token;
                $order->add_order_note( sprintf( __( 'Attempt to save payment method failed. Reason: %s', 'woo-stripe-payment' ), $token->get_error_message() ) );

                return $token;
            }
        }
        $token->set_user_id( $order->get_user_id() );
        $token->save();

        // set token value so it can be used for other processes.
        $this->payment_method_token = $token->get_token();

        return true;
    }

    /**
     * Set an error on the order.
     * This error is used on the frontend to alert customer's to a failed payment method save.
     *
     * @param WC_Order $order
     * @param WP_Error $error
     *
     * @deprecated
     */
    public function set_payment_save_error( $order, $error ) {
        if ( wcs_stripe_active() && wcs_order_contains_subscription( $order ) ) {
            $message = __( 'We were not able to save your payment method. To prevent billing issues with your subscription, please add a payment method to the subscription.', 'woo-stripe-payment' );
        } else {
            $message = sprintf( __( 'We were not able to save your payment method. Reason: %s', 'woo-stripe-payment' ), $error->get_error_message() );
        }
        $order->update_meta_data( '_wc_stripe_order_error', $message );
        $order->save();
    }

    /**
     *
     * @param string $token_id
     * @param int    $user_id
     *
     * @return null|WC_Payment_Token_Stripe_CC
     */
    public function get_token( $token_id, $user_id ) {
        $tokens = WC_Payment_Tokens::get_tokens( array( 'user_id' => $user_id, 'gateway_id' => $this->id, 'limit' => 20 ) );
        foreach ( $tokens as $token ) {
            if ( $token_id === $token->get_token() ) {
                return $token;
            }
        }

        return null;
    }

    /**
     *
     * @param array           $payment_meta
     * @param WC_Subscription $subscription
     */
    public function subscription_payment_meta( $payment_meta, $subscription ) {
        $payment_meta[ $this->id ] = array(
            'post_meta' => array(
                WC_Stripe_Constants::PAYMENT_METHOD_TOKEN => array(
                    'value' => $this->get_order_meta_data( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN, $subscription ),
                    'label' => __( 'Payment Method Token', 'woo-stripe-payment' ),
                ),
            ),
        );

        return $payment_meta;
    }

    /**
     *
     * @param float    $amount
     * @param WC_Order $order
     */
    public function scheduled_subscription_payment( $amount, $order ) {
        $this->processing_payment = true;

        $result = $this->payment_object->scheduled_subscription_payment( $amount, $order );

        if ( is_wp_error( $result ) ) {
            $order->update_status( 'failed' );
            $order->add_order_note( sprintf( __( 'Recurring payment for order failed. Reason: %s', 'woo-stripe-payment' ), $result->get_error_message() ) );

            return;
        }

        $this->save_order_meta( $order, $result->charge );

        // set the payment method token that was used to process the renewal order.
        $this->payment_method_token = $order->get_meta( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN );

        if ( $result->complete_payment ) {
            if ( $result->charge->captured ) {
                WC_Stripe_Utils::add_balance_transaction_to_order( $result->charge, $order );
                $order->payment_complete( $result->charge->id );
                $order->add_order_note( sprintf( __( 'Recurring payment captured in Stripe. Payment method: %s', 'woo-stripe-payment' ), $order->get_payment_method_title() ) );
            } else {
                $order->update_status( apply_filters( 'wc_stripe_authorized_renewal_order_status', 'on-hold', $order, $this ),
                    sprintf( __( 'Recurring payment authorized in Stripe. Payment method: %s', 'woo-stripe-payment' ), $order->get_payment_method_title() ) );
            }
        } else {
            $order->update_status( 'pending', sprintf( __( 'Customer must manually complete payment for payment method %s', 'woo-stripe-payment' ), $order->get_payment_method_title() ) );
        }
    }

    /**
     * Return true if this request is to change the payment method of a WC Subscription.
     *
     * @return bool
     */
    public function is_change_payment_method_request() {
        return wcs_stripe_active() && did_action( 'woocommerce_subscriptions_pre_update_payment_method' );
    }

    /**
     * Sets the ID of a payment token.
     *
     * @param string $id
     */
    public function set_payment_method_token( $id ) {
        $this->payment_method_token = $id;
    }

    public function set_new_source_token( $token ) {
        $this->new_source_token = $token;
    }

    /**
     *
     * @param WC_Order $order
     *
     * @deprecated
     *
     */
    public function get_order_description( $order ) {
        return sprintf( __( 'Order %1$s from %2$s', 'woo-stripe-payment' ), $order->get_order_number(), get_bloginfo( 'name' ) );
    }

    /**
     *
     * @param WC_Order $order
     */
    public function process_zero_total_order( $order ) {
        // save payment method if necessary
        if ( ! defined( WC_Stripe_Constants::PROCESSING_PAYMENT ) && $this->should_save_payment_method( $order ) ) {
            $result = $this->save_payment_method( $this->get_new_source_token(), $order );
            if ( is_wp_error( $result ) ) {
                wc_add_notice( $result->get_error_message(), 'error' );

                return $this->get_order_error();
            }
        } else {
            $this->payment_method_token = $this->get_saved_source_id();

            return $this->payment_object->process_zero_total_order( $order, $this, true );
        }

        return $this->payment_object->process_zero_total_order( $order, $this );
    }

    /**
     * @param WC_Order $order
     *
     * @return array
     */
    public function process_pre_order( $order ) {
        $token = null;
        // maybe save payment method
        if ( ! $this->use_saved_source() ) {
            // if user not logged in, create a Stripe customer that won't be assigned to a user.
            if ( ! $order->get_customer_id() ) {
                $customer = WC_Stripe_Customer_Manager::instance()->create_customer( WC()->customer );
                if ( is_wp_error( $customer ) ) {
                    return wc_add_notice( $customer->get_error_message(), 'error' );
                }
                $order->update_meta_data( WC_Stripe_Constants::CUSTOMER_ID, $customer->id );
                $result = $token = $this->create_payment_method( $this->get_new_source_token(), $customer->id );
            } else {
                $result = $this->save_payment_method( $this->get_new_source_token(), $order );
            }
            if ( is_wp_error( $result ) ) {
                wc_add_notice( $result->get_error_message(), 'error' );

                return $this->get_order_error();
            }
        } else {
            $this->payment_method_token = $this->get_saved_source_id();
        }
        WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order );
        $this->save_zero_total_meta( $order, $token );

        return array(
            'result'   => 'success',
            'redirect' => $this->get_return_url( $order ),
        );
    }

    /**
     *
     * @param WC_Order $order
     */
    public function save_zero_total_meta( $order, $token = null ) {
        $token = ! $token ? $this->get_token( $this->payment_method_token, $order->get_user_id() ) : $token;
        $order->set_payment_method_title( $token->get_payment_method_title() );
        $order->update_meta_data( WC_Stripe_Constants::MODE, wc_stripe_mode() );
        $order->update_meta_data( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN, $token->get_token() );
        if ( $order->get_customer_id() ) {
            $order->update_meta_data( WC_Stripe_Constants::CUSTOMER_ID, wc_stripe_get_customer_id( $order->get_user_id() ) );
        }
        if ( wcs_stripe_active() && wcs_order_contains_subscription( $order ) ) {
            foreach ( wcs_get_subscriptions_for_order( $order ) as $subscription ) {
                /**
                 *
                 * @var WC_Subscription $subscription
                 */
                $subscription->set_payment_method_title( $token->get_payment_method_title() );
                $subscription->update_meta_data( WC_Stripe_Constants::MODE, wc_stripe_mode() );
                $subscription->update_meta_data( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN, $token->get_token() );
                $subscription->update_meta_data( WC_Stripe_Constants::CUSTOMER_ID, wc_stripe_get_customer_id( $order->get_user_id() ) );
                $subscription->save();
            }
        }
        /**
         * @param WC_Order                  $order
         * @param WC_Payment_Gateway_Stripe $this
         * @param null                      $charge
         *
         * @since 3.2.7
         */
        do_action( 'wc_stripe_save_order_meta', $order, $this, null );

        $order->save();
    }

    /**
     * Pre orders can't be mixed with regular products.
     *
     * @param WC_Order $order
     */
    protected function order_contains_pre_order( $order ) {
        return wc_stripe_pre_orders_active() && WC_Pre_Orders_Order::order_contains_pre_order( $order );
    }

    /**
     *
     * @param WC_Order $order
     *
     * @return boolean
     */
    protected function pre_order_requires_tokenization( $order ) {
        return WC_Pre_Orders_Order::order_requires_payment_tokenization( $order );
    }

    /**
     * Sets a lock on the order.
     * Default behavior is a 2 minute lock.
     *
     * @param WC_Order|int $order
     */
    public function set_order_lock( $order ) {
        $order_id = ( is_object( $order ) ? $order->get_id() : $order );
        set_transient( 'stripe_lock_order_' . $order_id, $order_id, apply_filters( 'wc_stripe_set_order_lock', 2 * MINUTE_IN_SECONDS ) );
    }

    /**
     * Removes the lock on the order
     *
     * @param WC_Order|int $order
     */
    public function release_order_lock( $order ) {
        delete_transient( 'stripe_lock_order_' . ( is_object( $order ) ? $order->get_id() : $order ) );
    }

    /**
     * Returns true of the order has been locked.
     * If the lock exists and is greater than current time
     * method returns true;
     *
     * @param WC_Order|int $order
     */
    public function has_order_lock( $order ) {
        $lock = get_transient( 'stripe_lock_order_' . ( is_object( $order ) ? $order->get_id() : $order ) );

        return $lock !== false;
    }

    public function set_post_payment_process( $callback ) {
        $this->post_payment_processes[] = $callback;
    }

    /**
     *
     * @param WC_Order                  $order
     * @param WC_Payment_Gateway_Stripe $gateway
     */
    public function trigger_post_payment_processes( $order, $gateway ) {
        foreach ( $this->post_payment_processes as $callback ) {
            call_user_func_array( $callback, func_get_args() );
        }
    }

    public function validate_payment_sections_field( $key, $value ) {
        if ( empty( $value ) ) {
            $value = array();
        }

        return $value;
    }

    /**
     *
     * @param WC_Order $order
     */
    public function process_pre_order_payment( $order ) {
        $this->processing_payment = true;

        $result = $this->payment_object->process_pre_order_payment( $order );

        if ( is_wp_error( $result ) ) {
            $order->update_status( 'failed' );
            $order->add_order_note( sprintf( __( 'Pre-order payment for order failed. Reason: %s', 'woo-stripe-payment' ), $result->get_error_message() ) );
        } else {
            if ( $result->complete_payment ) {
                $this->save_order_meta( $order, $result->charge );

                if ( $result->charge->captured ) {
                    WC_Stripe_Utils::add_balance_transaction_to_order( $result->charge, $order );
                    $order->payment_complete( $result->charge->id );
                    $order->add_order_note( sprintf( __( 'Pre-order payment captured in Stripe. Payment method: %s', 'woo-stripe-payment' ), $order->get_payment_method_title() ) );
                } else {
                    $order->update_status( apply_filters( 'wc_stripe_authorized_preorder_order_status', 'on-hold', $order, $this ),
                        sprintf( __( 'Pre-order payment authorized in Stripe. Payment method: %s', 'woo-stripe-payment' ), $order->get_payment_method_title() ) );
                }
            } else {
                $order->update_status( 'pending', sprintf( __( 'Customer must manually complete payment for payment method %s', 'woo-stripe-payment' ), $order->get_payment_method_title() ) );
            }
        }
    }

    /**
     * Given a meta key, see if there is a value for that key in another plugin.
     * This acts as a lazy conversion
     * method for merchants that have switched to our plugin from other plugins.
     *
     * @param string   $meta_key
     * @param WC_Order $order
     * @param string   $context
     *
     * @since 3.1.0
     */
    public function get_order_meta_data( $meta_key, $order, $context = 'view' ) {
        $value = $order->get_meta( $meta_key, true, $context );
        // value is empty so check metadata from other plugins
        if ( empty( $value ) ) {
            $keys = array();
            switch ( $meta_key ) {
                case WC_Stripe_Constants::PAYMENT_METHOD_TOKEN:
                    $keys = array( WC_Stripe_Constants::SOURCE_ID );
                    break;
                case WC_Stripe_Constants::CUSTOMER_ID:
                    $keys = array( WC_Stripe_Constants::STRIPE_CUSTOMER_ID );
                    break;
                case WC_Stripe_Constants::PAYMENT_INTENT_ID:
                    $keys = array( WC_Stripe_Constants::STRIPE_INTENT_ID );
            }
            if ( $keys ) {
                $meta_data = $order->get_meta_data();
                if ( $meta_data ) {
                    $keys       = array_intersect( wp_list_pluck( $meta_data, 'key' ), $keys );
                    $array_keys = array_keys( $keys );
                    if ( ! empty( $array_keys ) ) {
                        $value = $meta_data[ current( $array_keys ) ]->value;
                        update_post_meta( $order->get_id(), $meta_key, $value );
                    }
                }
            }
        }

        return $value;
    }

    /**
     * Gateways can override this method to add attributes to the Stripe object before it's
     * sent to Stripe.
     *
     * @param array    $args
     * @param WC_Order $order
     */
    public function add_stripe_order_args( &$args, $order ) {
    }

    /**
     *
     * @param WP_Error $result
     *
     * @since 3.1.1
     */
    public function get_generic_error( $result = null ) {
        $messages = wc_stripe_get_error_messages();
        if ( isset( $messages["{$this->id}_generic"] ) ) {
            return $messages["{$this->id}_generic"];
        }

        return null != $result ? $result->get_error_message() : __( 'Cannot process payment', 'woo-stripe-payment' );
    }

    /**
     *
     * @since 3.1.2
     */
    private function get_payment_section_description() {
        return sprintf( __( 'Increase your conversion rate by offering %1$s on your Product and Cart pages, or at the top of the checkout page. <br/><strong>Note:</strong> you can control which products display %s by going to the product edit page.',
            'woo-stripe-payment' ),
            $this->get_method_title() );
    }

    /**
     * Outputs fields required by Google Pay to render the payment wallet.
     *
     * @param string $page
     * @param array  $data
     */
    public function output_display_items( $page = 'checkout', $data = array() ) {
        global $wp;
        $order = null;
        $data  = wp_parse_args( $data, array(
            'items'            => $this->has_digital_wallet ? $this->get_display_items( $page ) : array(),
            'shipping_options' => $this->has_digital_wallet ? $this->get_formatted_shipping_methods() : array(),
            'total'            => wc_format_decimal( WC()->cart->total, 2 ),
            'total_cents'      => wc_stripe_add_number_precision( WC()->cart->total, get_woocommerce_currency() ),
            'currency'         => get_woocommerce_currency()
        ) );
        if ( in_array( $page, array( 'checkout', 'cart' ) ) ) {
            if ( ! empty( $wp->query_vars['order-pay'] ) ) {
                $order                  = wc_get_order( absint( $wp->query_vars['order-pay'] ) );
                $page                   = 'order_pay';
                $data['needs_shipping'] = false;
                $data['items']          = $this->has_digital_wallet ? $this->get_display_items( $page, $order ) : array();
                $data['total']          = $order->get_total();
                $data['total_cents']    = wc_stripe_add_number_precision( $order->get_total(), $order->get_currency() );
                $data['currency']       = $order->get_currency();
                $data['pre_order']      = $this->order_contains_pre_order( $order );
            } else {
                $data['needs_shipping'] = WC()->cart->needs_shipping();
                if ( 'checkout' === $page && is_cart() ) {
                    $page = 'cart';
                } elseif ( is_add_payment_method_page() ) {
                    $page = 'add_payment_method';
                }
            }
        } elseif ( 'product' === $page ) {
            global $product;
            $data['needs_shipping'] = $product->needs_shipping();
            $data['product']        = array(
                'id'        => $product->get_id(),
                'price'     => $product->get_price(),
                'variation' => false
            );
        }
        /**
         * @param array                     $data
         * @param string                    $page
         * @param WC_Payment_Gateway_Stripe $this
         *
         * @since 3.1.8
         */
        $data = wp_json_encode( apply_filters( 'wc_stripe_output_display_items', $data, $page, $this ) );
        $data = function_exists( 'wc_esc_json' ) ? wc_esc_json( $data ) : _wp_specialchars( $data, ENT_QUOTES, 'UTF-8', true );
        printf( '<input type="hidden" class="%1$s" data-gateway="%2$s"/>', "woocommerce_{$this->id}_gateway_data {$page}-page", $data );
    }

    /**
     * @param array $deps
     * @param       $scripts
     *
     * @since 3.1.8
     */
    public function get_mini_cart_dependencies( $deps, $scripts ) {
        return $deps;
    }

    /**
     * @since 3.2.0
     * @return array
     */
    public function get_shipping_packages() {
        $packages = WC()->shipping()->get_packages();
        if ( empty( $packages ) && wcs_stripe_active() && WC_Subscriptions_Cart::cart_contains_free_trial() ) {
            // there is a subscription with a free trial in the cart. Shipping packages will be in the recurring cart.
            WC_Subscriptions_Cart::set_calculation_type( 'recurring_total' );
            $count = 0;
            if ( isset( WC()->cart->recurring_carts ) ) {
                foreach ( WC()->cart->recurring_carts as $recurring_cart_key => $recurring_cart ) {
                    foreach ( $recurring_cart->get_shipping_packages() as $i => $base_package ) {
                        $packages[ $recurring_cart_key . '_' . $count ] = WC_Subscriptions_Cart::get_calculated_shipping_for_package( $base_package );
                    }
                    $count ++;
                }
            }
            WC_Subscriptions_Cart::set_calculation_type( 'none' );
        }

        return $packages;
    }

    /**
     * @param WC_Cart $cart
     * @param array   $items
     *
     * @since 3.2.1
     * @return array
     */
    protected function get_display_items_for_cart( $cart, $items = array() ) {
        $incl_tax = wc_stripe_display_prices_including_tax();
        foreach ( $cart->get_cart() as $cart_item ) {
            /**
             *
             * @var WC_Product $product
             */
            $product = $cart_item['data'];
            $qty     = $cart_item['quantity'];
            $label   = $qty > 1 ? sprintf( '%s X %s', $product->get_name(), $qty ) : $product->get_name();
            $price   = $incl_tax ? wc_get_price_including_tax( $product, array( 'qty' => $qty ) ) : wc_get_price_excluding_tax( $product, array( 'qty' => $qty ) );
            $items[] = $this->get_display_item_for_cart( $price, $label, 'product', $cart_item, $cart );
        }
        if ( $cart->needs_shipping() ) {
            $price   = $incl_tax ? $cart->shipping_total + $cart->shipping_tax_total : $cart->shipping_total;
            $items[] = $this->get_display_item_for_cart( $price, __( 'Shipping', 'woo-stripe-payment' ), 'shipping' );
        }
        foreach ( $cart->get_fees() as $fee ) {
            $price   = $incl_tax ? $fee->total + $fee->tax : $fee->total;
            $items[] = $this->get_display_item_for_cart( $price, $fee->name, 'fee', $fee, $cart );
        }
        if ( 0 < $cart->discount_cart ) {
            $price   = - 1 * abs( $incl_tax ? $cart->discount_cart + $cart->discount_cart_tax : $cart->discount_cart );
            $items[] = $this->get_display_item_for_cart( $price, __( 'Discount', 'woo-stripe-payment' ), 'discount', $cart );
        }
        if ( ! $incl_tax && wc_tax_enabled() ) {
            $items[] = $this->get_display_item_for_cart( $cart->get_taxes_total(), __( 'Tax', 'woo-stripe-payment' ), 'tax', $cart );
        }

        return $items;
    }

    /**
     * @param WC_Order $order
     * @param array    $items
     *
     * @since 3.2.1
     * @return array
     */
    protected function get_display_items_for_order( $order, $items = array() ) {
        foreach ( $order->get_items() as $item ) {
            $qty     = $item->get_quantity();
            $label   = $qty > 1 ? sprintf( '%s X %s', $item->get_name(), $qty ) : $item->get_name();
            $items[] = $this->get_display_item_for_order( $item->get_subtotal(), $label, $order, 'item', $item );
        }
        if ( 0 < $order->get_shipping_total() ) {
            $items[] = $this->get_display_item_for_order( $order->get_shipping_total(), __( 'Shipping', 'woo-stripe-payment' ), $order, 'shipping' );
        }
        if ( 0 < $order->get_total_discount() ) {
            $items[] = $this->get_display_item_for_order( - 1 * $order->get_total_discount(), __( 'Discount', 'woo-stripe-payment' ), $order, 'discount' );
        }
        if ( 0 < $order->get_fees() ) {
            $fee_total = 0;
            foreach ( $order->get_fees() as $fee ) {
                $fee_total += $fee->get_total();
            }
            $items[] = $this->get_display_item_for_order( $fee_total, __( 'Fees', 'woo-stripe-payment' ), $order, 'fee' );
        }
        if ( 0 < $order->get_total_tax() ) {
            $items[] = $this->get_display_item_for_order( $order->get_total_tax(), __( 'Tax', 'woocommerce' ), $order, 'tax' );
        }

        return $items;
    }

    /**
     * @param float  $price
     * @param string $label
     * @param string $type
     * @param mixed  ...$args
     *
     * @since 3.2.1
     * @return array
     */
    protected function get_display_item_for_cart( $price, $label, $type, ...$args ) {
        return array(
            'label'   => $label,
            'pending' => false,
            'amount'  => wc_stripe_add_number_precision( $price )
        );
    }

    /**
     * @param float    $price
     * @param string   $label
     * @param WC_Order $order
     * @param string   $type
     * @param mixed    ...$args
     */
    protected function get_display_item_for_order( $price, $label, $order, $type, ...$args ) {
        return array(
            'label'   => $label,
            'pending' => false,
            'amount'  => wc_stripe_add_number_precision( $price, $order->get_currency() )
        );
    }

    /**
     * @param WC_Product $product
     *
     * @since 3.2.1
     *
     * @return array
     */
    protected function get_display_item_for_product( $product ) {
        return array(
            'label'   => esc_attr( $product->get_name() ),
            'pending' => true,
            'amount'  => wc_stripe_add_number_precision( $product->get_price() )
        );
    }

    /**
     * @param array $methods
     * @param       $sort
     *
     * @since 3.2.1
     * @return array
     */
    public function get_formatted_shipping_methods( $methods = array() ) {
        if ( wcs_stripe_active() && WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment ) {
            return $methods;
        } else {
            $methods        = array();
            $chosen_methods = array();
            $packages       = $this->get_shipping_packages();
            $incl_tax       = wc_stripe_display_prices_including_tax();
            foreach ( WC()->session->get( 'chosen_shipping_methods', array() ) as $i => $id ) {
                $chosen_methods[] = $this->get_shipping_method_id( $id, $i );
            }
            foreach ( $packages as $i => $package ) {
                foreach ( $package['rates'] as $rate ) {
                    $price     = $incl_tax ? $rate->cost + $rate->get_shipping_tax() : $rate->cost;
                    $methods[] = $this->get_formatted_shipping_method( $price, $rate, $i, $package, $incl_tax );
                }
            }

            /**
             * Sort shipping methods so the selected method is first in the array.
             */
            usort( $methods, function ( $method ) use ( $chosen_methods ) {
                foreach ( $chosen_methods as $id ) {
                    if ( in_array( $id, $method, true ) ) {
                        return - 1;
                    }
                }

                return 1;
            } );
        }

        /**
         * @param array $methods
         *
         * @since 3.3.0
         */
        return apply_filters( 'wc_stripe_get_formatted_shipping_methods', $methods, $this );
    }

    /**
     * @param float            $price
     * @param WC_Shipping_Rate $rate
     * @param string           $i
     * @param array            $package
     * @param bool             $incl_tax
     *
     * @since 3.2.1
     * @return array
     */
    public function get_formatted_shipping_method( $price, $rate, $i, $package, $incl_tax ) {
        $method = array(
            'id'     => $this->get_shipping_method_id( $rate->id, $i ),
            'label'  => $this->get_formatted_shipping_label( $price, $rate, $incl_tax ),
            'detail' => '',
            'amount' => wc_stripe_add_number_precision( $price )
        );
        if ( $incl_tax ) {
            if ( $rate->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
                $method['detail'] = WC()->countries->inc_tax_or_vat();
            }
        } else {
            if ( $rate->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
                $method['detail'] = WC()->countries->ex_tax_or_vat();
            }
        }

        return $method;
    }

    /**
     * @param string $id
     * @param string $index
     *
     * @return mixed
     */
    protected function get_shipping_method_id( $id, $index ) {
        return sprintf( '%s:%s', $index, $id );
    }

    /**
     * @param float            $price
     * @param WC_Shipping_Rate $rate
     * @param bool             $incl_tax
     *
     * @since 3.2.1
     */
    protected function get_formatted_shipping_label( $price, $rate, $incl_tax ) {
        return sprintf( '%s', esc_attr( $rate->get_label() ) );
    }

    /**
     * Returns true if a scheduled subscription payment is being processed.
     *
     * @since 3.2.3
     * @return bool
     */
    protected function is_processing_scheduled_payment() {
        return doing_action( 'woocommerce_scheduled_subscription_payment_' . $this->id );
    }

    /**
     * @param WC_Stripe_Frontend_Scripts $scripts
     *
     * @since 3.2.5
     * @return bool
     */
    public function has_enqueued_scripts( $scripts ) {
        return false;
    }

    public function get_transaction_url( $order ) {
        if ( wc_stripe_order_mode( $order ) === 'test' ) {
            $this->view_transaction_url = 'https://dashboard.stripe.com/test/payments/%s';
        } else {
            $this->view_transaction_url = 'https://dashboard.stripe.com/payments/%s';
        }

        return parent::get_transaction_url( $order );
    }

    /**
     * @param WC_Subscription $subscription
     *
     * @since 3.2.13
     * @return array
     */
    protected function process_change_payment_method_request( $subscription ) {
        if ( ! $this->use_saved_source() ) {
            $result = $this->save_payment_method( $this->get_new_source_token(), $subscription );
            if ( is_wp_error( $result ) ) {
                wc_add_notice( sprintf( __( 'Error saving payment method for subscription. Reason: %s', 'woo-stripe-payment' ), $result->get_error_message() ), 'error' );

                return array( 'result' => 'error' );
            }
        } else {
            $this->payment_method_token = $this->get_saved_source_id();
        }
        $token = $this->get_token( $this->payment_method_token, $subscription->get_user_id() );
        // update the meta data needed by the gateway to process a subscription payment.
        $subscription->update_meta_data( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN, $this->payment_method_token );
        $subscription->update_meta_data( WC_Stripe_Constants::CUSTOMER_ID, $token->get_customer_id() );
        if ( $token ) {
            $subscription->set_payment_method_title( $token->get_payment_method_title() );
        }
        $subscription->save();

        return array( 'result' => 'success', 'redirect' => wc_get_page_permalink( 'myaccount' ) );
    }

    /**
     * @param WC_Subscription $subscription
     * @param WC_Order        $order
     */
    public function update_failing_payment_method( $subscription, $order ) {
        if ( ( $token = $this->get_token( $order->get_meta( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN ), $order->get_customer_id() ) ) ) {
            $subscription->update_meta_data( WC_Stripe_Constants::PAYMENT_METHOD_TOKEN, $token->get_token() );
            $subscription->set_payment_method_title( $token->get_payment_method_title( $this->get_option( 'method_format' ) ) );
            $subscription->save();
        }
    }

    /**
     * @param array $options
     *
     * @since 3.3.10
     * @return mixed|void
     */
    public function get_element_options( $options = array() ) {
        $options = array_merge( array( 'locale' => wc_stripe_get_site_locale() ), $options );

        return apply_filters( 'wc_stripe_get_element_options', $options, $this );
    }

}
Stripe API documentation generated by ApiGen