* @created 2022-08-08 16:09:21 * @modified 2022-08-08 16:09:21 */ namespace Plugin\Stripe\Services; use Stripe\Token; use Stripe\Stripe; use Beike\Shop\Services\PaymentService; use Stripe\Exception\ApiErrorException; class StripePaymentService extends PaymentService { // 零位十进制货币 https://stripe.com/docs/currencies#special-cases const ZERO_DECIMAL = [ 'BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF', ]; /** * @throws ApiErrorException */ public function capture($creditCardData): bool { $apiKey = plugin_setting('stripe.secret_key'); Stripe::setApiKey($apiKey); $token = Token::create([ 'card' => [ 'number' => $creditCardData['cardnum'], 'exp_year' => $creditCardData['year'], 'exp_month' => $creditCardData['month'], 'cvc' => $creditCardData['cvv'], ], ]); $tokenId = $token['id']; $currency = $this->order->currency_code; if (!in_array($currency, self::ZERO_DECIMAL)) { $total = round($this->order->total, 2) * 100; } else { $total = floor($this->order->total); } $stripeChargeParameters = array( 'amount' => $total, 'currency' => $currency, 'metadata' => array( 'orderId' => $this->order->id, ), 'source' => $tokenId, // 'customer' => $this->createCustomer(), ); $charge = \Stripe\Charge::create($stripeChargeParameters); return $charge['paid'] && $charge['captured']; } /** * 创建 stripe customer * @return mixed * @throws ApiErrorException */ private function createCustomer(): mixed { $customer = \Stripe\Customer::create([ 'email' => $this->order->email, ]); return $customer['id']; } }