StripeCredit.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * 自己写别抄,抄NMB抄
  4. */
  5. namespace App\Payments;
  6. use Stripe\Source;
  7. use Stripe\Stripe;
  8. class StripeCredit {
  9. public function __construct($config)
  10. {
  11. $this->config = $config;
  12. }
  13. public function form()
  14. {
  15. return [
  16. 'currency' => [
  17. 'label' => '货币单位',
  18. 'description' => '',
  19. 'type' => 'input',
  20. ],
  21. 'stripe_sk_live' => [
  22. 'label' => 'SK_LIVE',
  23. 'description' => '',
  24. 'type' => 'input',
  25. ],
  26. 'stripe_pk_live' => [
  27. 'label' => 'PK_LIVE',
  28. 'description' => '',
  29. 'type' => 'input',
  30. ],
  31. 'stripe_webhook_key' => [
  32. 'label' => 'WebHook密钥签名',
  33. 'description' => '',
  34. 'type' => 'input',
  35. ]
  36. ];
  37. }
  38. public function pay($order)
  39. {
  40. info($order);
  41. $currency = $this->config['currency'];
  42. $exchange = $this->exchange('CNY', strtoupper($currency));
  43. if (!$exchange) {
  44. abort(500, __('user.order.stripeCard.currency_convert_timeout'));
  45. }
  46. Stripe::setApiKey($this->config['stripe_sk_live']);
  47. try {
  48. $charge = \Stripe\Charge::create([
  49. 'amount' => floor($order['total_amount'] * $exchange),
  50. 'currency' => $currency,
  51. 'source' => $order['stripe_token'],
  52. 'metadata' => [
  53. 'user_id' => $order['user_id'],
  54. 'out_trade_no' => $order['trade_no'],
  55. 'identifier' => ''
  56. ]
  57. ]);
  58. } catch (\Exception $e) {
  59. info($e);
  60. abort(500, __('user.order.stripeCard.was_problem'));
  61. }
  62. if (!$charge->paid) {
  63. abort(500, __('user.order.stripeCard.deduction_failed'));
  64. }
  65. return [
  66. 'type' => 2,
  67. 'data' => $charge->paid
  68. ];
  69. }
  70. public function notify($params)
  71. {
  72. \Stripe\Stripe::setApiKey($this->config['stripe_sk_live']);
  73. try {
  74. $event = \Stripe\Webhook::constructEvent(
  75. file_get_contents('php://input'),
  76. $_SERVER['HTTP_STRIPE_SIGNATURE'],
  77. $this->config['stripe_webhook_key']
  78. );
  79. } catch (\Stripe\Error\SignatureVerification $e) {
  80. abort(400);
  81. }
  82. switch ($event->type) {
  83. case 'source.chargeable':
  84. $object = $event->data->object;
  85. \Stripe\Charge::create([
  86. 'amount' => $object->amount,
  87. 'currency' => $object->currency,
  88. 'source' => $object->id,
  89. 'metadata' => json_decode($object->metadata, true)
  90. ]);
  91. break;
  92. case 'charge.succeeded':
  93. $object = $event->data->object;
  94. if ($object->status === 'succeeded') {
  95. $metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
  96. $tradeNo = $metaData->out_trade_no;
  97. return [
  98. 'trade_no' => $tradeNo,
  99. 'callback_no' => $object->balance_transaction
  100. ];
  101. }
  102. break;
  103. default:
  104. abort(500, 'event is not support');
  105. }
  106. die('success');
  107. }
  108. private function exchange($from, $to)
  109. {
  110. $result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
  111. $result = json_decode($result, true);
  112. return $result['rates'][$to];
  113. }
  114. }