首页 > 解决方案 > Stripe 在创建令牌时返回“客户必须附加一个有效的付款来源”

问题描述

我正在为电子商务平台创建一个由 Stripe 提供支持的支付网关。当我去结账时,我收到错误消息“客户必须附加一个有效的付款来源”。但是,当我从 \Stripe\PaymentMethod:: 记录结果时,将返回客户的所有付款方式。

这是问题区域的代码片段。

         \Stripe\Stripe::setApiKey($this->secret_key);
          $intentID = $_POST['intentID'];
          $setup_intent = \Stripe\SetupIntent::retrieve($intentID);

          //save payment method id to customer
          $customer = \Stripe\Customer::create(['payment_method' => $setup_intent->payment_method]);

          //get all customer payment methods
          $payment_methods = \Stripe\PaymentMethod::all([
            'customer' => $customer->id,
            'type' => 'card',
          ]);

          //if vendor is connected to stripe create token
          if ( ! empty( $access_token ) ) {
              try{
                $token = \Stripe\Token::create( array( 'customer' => $customer->id ), $access_token );
              }catch(Exception $e){
                var_error_log($e->getMessage());
              }

          } else if ( $this->allow_unconnected_sellers == 'yes' ) {
              $token = null;
          } else {
              wc_add_notice('Unable to process with Stripe gateway', 'error');
              return;
          }

付款意图被检索并具有付款方式 ID。这就是我记录它时 $setup_intent 变量返回的内容。

[25-Feb-2020 16:21:37 UTC] object(Stripe\SetupIntent)#2877 (20) {
  ["id"]=>
  string(29) "seti_1GG69pL8ByYsKvROh2NNVQya"
  ["object"]=>
  string(12) "setup_intent"
  ["application"]=>
  NULL
  ["cancellation_reason"]=>
  NULL
  ["client_secret"]=>
  string(68) "seti_1GG69pL8ByYsKvROh2NNVQya_secret_GnhYhOiZIVQJMapNMR9ZpfOzhEXzt1B"
  ["created"]=>
  int(1582647677)
  ["customer"]=>
  NULL
  ["description"]=>
  NULL
  ["last_setup_error"]=>
  NULL
  ["livemode"]=>
  bool(false)
  ["mandate"]=>
  NULL
  ["metadata"]=>
  object(Stripe\StripeObject)#2948 (0) {
  }
  ["next_action"]=>
  NULL
  ["on_behalf_of"]=>
  NULL
  ["payment_method"]=>
  string(27) "pm_1GG6A5L8ByYsKvROlbLS4YOi"
  ["payment_method_options"]=>
  object(Stripe\StripeObject)#2909 (1) {
    ["card"]=>
    object(Stripe\StripeObject)#2960 (1) {
      ["request_three_d_secure"]=>
      string(9) "automatic"
    }
  }
  ["payment_method_types"]=>
  array(1) {
    [0]=>
    string(4) "card"
  }
  ["single_use_mandate"]=>
  NULL
  ["status"]=>
  string(9) "succeeded"
  ["usage"]=>
  string(11) "off_session"
}

这是我记录 $customer 变量时返回的内容。

[25-Feb-2020 16:21:37 UTC] object(Stripe\Customer)#2947 (26) {
  ["id"]=>
  string(18) "cus_GnhZUhQOCslFcl"
  ["object"]=>
  string(8) "customer"
  ["account_balance"]=>
  int(0)
  ["address"]=>
  NULL
  ["balance"]=>
  int(0)
  ["created"]=>
  int(1582647696)
  ["currency"]=>
  NULL
  ["default_source"]=>
  NULL
  ["delinquent"]=>
  bool(false)
  ["description"]=>
  NULL
  ["discount"]=>
  NULL
  ["email"]=>
  NULL
  ["invoice_prefix"]=>
  string(8) "10DD54AF"
  ["invoice_settings"]=>
  object(Stripe\StripeObject)#2838 (3) {
    ["custom_fields"]=>
    NULL
    ["default_payment_method"]=>
    NULL
    ["footer"]=>
    NULL
  }
  ["livemode"]=>
  bool(false)
  ["metadata"]=>
  object(Stripe\StripeObject)#2837 (0) {
  }
  ["name"]=>
  NULL
  ["phone"]=>
  NULL
  ["preferred_locales"]=>
  array(0) {
  }
  ["shipping"]=>
  NULL
  ["sources"]=>
  object(Stripe\Collection)#2995 (5) {
    ["object"]=>
    string(4) "list"
    ["data"]=>
    array(0) {
    }
    ["has_more"]=>
    bool(false)
    ["total_count"]=>
    int(0)
    ["url"]=>
    string(40) "/v1/customers/cus_GnhZUhQOCslFcl/sources"
  }
  ["subscriptions"]=>
  object(Stripe\Collection)#2992 (5) {
    ["object"]=>
    string(4) "list"
    ["data"]=>
    array(0) {
    }
    ["has_more"]=>
    bool(false)
    ["total_count"]=>
    int(0)
    ["url"]=>
    string(46) "/v1/customers/cus_GnhZUhQOCslFcl/subscriptions"
  }
  ["tax_exempt"]=>
  string(4) "none"
  ["tax_ids"]=>
  object(Stripe\Collection)#2989 (5) {
    ["object"]=>
    string(4) "list"
    ["data"]=>
    array(0) {
    }
    ["has_more"]=>
    bool(false)
    ["total_count"]=>
    int(0)
    ["url"]=>
    string(40) "/v1/customers/cus_GnhZUhQOCslFcl/tax_ids"
  }
  ["tax_info"]=>
  NULL
  ["tax_info_verification"]=>
  NULL
}

我还记录了 $payment_methods 变量的结果。客户的付款方式与付款方式 ID 一起返回。

[25-Feb-2020 16:21:37 UTC] object(Stripe\Collection)#2986 (4) {
  ["object"]=>
  string(4) "list"
  ["data"]=>
  array(1) {
    [0]=>
    object(Stripe\PaymentMethod)#2978 (9) {
      ["id"]=>
      string(27) "pm_1GG6A5L8ByYsKvROlbLS4YOi"
      ["object"]=>
      string(14) "payment_method"
      ["billing_details"]=>
      object(Stripe\StripeObject)#2974 (4) {
        ["address"]=>
        object(Stripe\StripeObject)#2972 (6) {
          ["city"]=>
          NULL
          ["country"]=>
          NULL
          ["line1"]=>
          NULL
          ["line2"]=>
          NULL
          ["postal_code"]=>
          string(5) "19104"
          ["state"]=>
          NULL
        }
        ["email"]=>
        NULL
        ["name"]=>
        string(8) "John Doe"
        ["phone"]=>
        NULL
      }
      ["card"]=>
      object(Stripe\StripeObject)#2973 (11) {
        ["brand"]=>
        string(4) "visa"
        ["checks"]=>
        object(Stripe\StripeObject)#2793 (3) {
          ["address_line1_check"]=>
          NULL
          ["address_postal_code_check"]=>
          string(4) "pass"
          ["cvc_check"]=>
          string(4) "pass"
        }
        ["country"]=>
        string(2) "US"
        ["exp_month"]=>
        int(4)
        ["exp_year"]=>
        int(2024)
        ["fingerprint"]=>
        string(16) "MIexcoDyPrQncpp1"
        ["funding"]=>
        string(6) "credit"
        ["generated_from"]=>
        NULL
        ["last4"]=>
        string(4) "4242"
        ["three_d_secure_usage"]=>
        object(Stripe\StripeObject)#2971 (1) {
          ["supported"]=>
          bool(true)
        }
        ["wallet"]=>
        NULL
      }
      ["created"]=>
      int(1582647693)
      ["customer"]=>
      string(18) "cus_GnhZUhQOCslFcl"
      ["livemode"]=>
      bool(false)
      ["metadata"]=>
      object(Stripe\StripeObject)#2969 (0) {
      }
      ["type"]=>
      string(4) "card"
    }
  }
  ["has_more"]=>
  bool(false)
  ["url"]=>
  string(19) "/v1/payment_methods"
}

标签: phpstripe-payments

解决方案


推荐阅读