2

そのため、ストライプ形式で表示されている金額と同じ金額を顧客 (ユーザーではない) に請求したい

<form action="/charge" method="POST">
                 {!! csrf_field() !!}
                  <script
                    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="pk_test_key"
                    data-amount="{{ $note->price*100 }}"
                    data-name="Hello World"
                    data-description="{{$note->title}}"
                    data-image="/img/documentation/checkout/marketplace.png"
                    data-locale="auto"
                    data-currency="aud">
                  </script>
                </form>

形式上表示価格は当該ノートモデルに記載されている価格*100です。実際にお客様に請求していただきたいのですが、ノートによって変わります。

これが私のサーバー請求です

public function charge()
{
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey("sk_test_key");

    // Get the credit card details submitted by the form
    $token = $_POST['stripeToken'];

    // Create the charge on Stripe's servers - this will charge the user's card
    try {
      $charge = \Stripe\Charge::create(array(
        "amount" => 100, // amount in cents, again
        "currency" => "aud",
        "source" => $token,
        "description" => "Example charge"
        ));
    } catch(\Stripe\Error\Card $e) {
      // The card has been declined
    }

    return 'payment succesful';
}

「量」を音価に等しく設定する必要があります。

それ以外の場合は、レジのテスト アカウントで支払いが行われており、他のほとんどすべてが機能している必要があります。

ただし、顧客は登録ユーザーではありません。

4

1 に答える 1

2

金額はサーバー側で計算する必要があります。そうでない場合は、DOM を変更して好きな金額を渡すことができます。

代わりにid、サーバー側のキャッシュに を保存noteし、依存関係としてcharge関数に渡し、2 を比較します。

/**
 * Assuming name of "view" page
 */
public function checkout(Note $note) 
{
    /**
     * Always forget this on each page load, so we can switch seamlessly between Notes.
     */
    Illuminate\Support\Facades\Cache::forget('nid')->put('nid', $note->id);
}

ルートを変更して、依存関係chargeへの新しい参照を含めることを忘れないでくださいNote

route::post('charge/{note}', 'Controller@charge')->name('charge');

noteも渡すようにフォームを更新してください

<form action="{{route('charge', $note)}}" method="POST">

補足私は名前付きルートが好きですが、必須ではありません。

$nid次に、リクエストを処理しているときに、とを比較します。$note->id

public function charge(Illuminate\Http\Request $request, Note $note)
{
    $nid = Illuminate\Support\Facades\Cache::get('nid');

    // id's match, no sneaky stuff. safe to use $note->price now
    if ($nid === $note->id) {
        //now we can process our payment.
        // Set your secret key: remember to change this to your live secret key in production
        // See your keys here https://dashboard.stripe.com/account/apikeys
        \Stripe\Stripe::setApiKey("sk_test_key");

        // Get the credit card details submitted by the form
        $token = $request->get('stripeToken');

        // Create the charge on Stripe's servers - this will charge the user's card
        try {
            $charge = \Stripe\Charge::create(array(
                "amount" => number_format($note->price, 0, '', ''), // amount in cents, again
                "currency" => "aud",
                "source" => $token,
                "description" => "Example charge"
            ));
        } catch(\Stripe\Error\Card $e) {
            // The card has been declined
        }    
    }
}
于 2016-06-27T05:25:53.230 に答える