用PHP条纹支付

时间:2017-05-23 17:56:12

标签: php angular

我正在开发一个前端用angularjs和后端用laravel制作的网站。 我想整合条纹付款。我很难为laravel添加angularjs api以进行条纹支付。所以,我试图用程序性的PHP解决它。这是我的代码。

<?php
require_once('/stripe-php/lib/Stripe.php');
require_once('/stripe-php/lib/Charge.php');

Stripe::setApiKey('my-secret-key');
$charge = Charge::create(array('amount' => 2000, 'currency' => 'usd', 'source' => $token ));
if ($charge) {
  echo "Payment successcul";
}
else {
  echo "Not success";
}

?>

我可以获得令牌。但付款没有处理,我看到一个空白页面。

2 个答案:

答案 0 :(得分:5)

按照此步骤划分付款。     1.从https://stripe.com/docs/libraries#php下载条带文件夹     2.在项目的www文件夹中包含该文件夹。     3.创建新的PHP文件并将此代码放入文件中。

<?php
{
  require_once "./Stripe-php/init.php"; //include your stripe folder
  function __construct()
  {
      // put your keys here
      \Stripe\Stripe::setApiKey("Secret-Key");
  }

 //put your code into function.

  $token = $_POST['stripeToken']; // Token ID

  $charge = \Stripe\Charge::create([
       'amount' => $20 * 100,
       'currency' => 'usd',
       'description' => 'Message',
       'source' => $token,
   ]);

    if($charge['status'] == "succeeded"){  // if success
        $status=1;
        $post['charge_id']=$charge['id'];
        $post['amount']=$charge['amount'];
        $post['funding']=$charge['source']['funding'];
        $post['holder_name']=$charge['source']['name'];
        $post['brand']=$charge['source']['brand'];
        $Message="Payment Success";
    }
    else{
        $status=2;                        // failed
        $post=null;
        $Message="Payment Failed";
    }

    $data["status"] = ($status > 1) ? FAILED : SUCCESS;
    $data["message"]=$Message;
    $data["PaymentData"]=$post;
    return $data; // JSON Returen
}
?>

答案 1 :(得分:0)

在页面中包含Stripes error handling

try {
  // Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}