Laravel中控制器的功能:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>python-build</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>C:\Users\name\AppData\Local\Programs\Python\Python36\python.exe</executable>
<workingDirectory>src/main/resources/pythonlib/</workingDirectory>
<arguments>
<argument>test.py</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
视图中的JavaScript代码:
public function postCheckout(Request $request) {
if(! Session::has('cart')){
return redirect()->route('product.shoppingCart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
Stripe::setApiKey('XXXXXXXXXXXX');
//dd($request->input());
try {
Charge::create(array(
"amount" => $cart->totalPrice*100 ,
"currency" => "cad",
"source" => $request->input('stripeToken'), // obtained with Stripe.js
"description" => "Test charge..."
));
} catch (\Exception $e) {
return redirect()->route('checkout')->with('error', $e->getMessage() );
}
Session::forget('cart');
return redirect()->route('welcome')->with('success','Successfully purchased products !');
}
此函数创建隐藏的输入以将数据发送到服务器,它将令牌ID插入表单,以便将其提交到服务器:
var stripe = Stripe('YYYYYYYYYYYY');
var elements = stripe.elements();
var card = elements.create('card');
card.mount('#card-element');
card.addEventListener('change', function(event) {
var displayError = document.getElementById('charge-error');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
var form = document.getElementById('checkout-form');
form.addEventListener('click', function() {
// event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the customer that there was an error.
var errorElement = document.getElementById('charge-error');
errorElement.textContent = result.error.message;
} else {
stripeTokenHandler(result.token);
}
});
});
HTML表单:
function stripeTokenHandler(token) {
var form = document.getElementById('checkout-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
响应中必须提供来源或客户。