我有一个angular应用程序https://github.com/Kyro1980/timeout-app-public和使用支付意图API来构建一个集成,可以处理复杂的支付流(Stripe)。我正在发送一个PaymentIntent (Stripe)到aws lambda,并试图使用下面的java代码获得响应:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.fasterxml.jackson.jr.ob.JSON;
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.PaymentIntent;
public class CheckoutPayment implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>{
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context){
String paymentStr ="";
try {
PaymentInfo paymentInfo = JSON.std.beanFrom(PaymentInfo.class, input.getBody());
PaymentIntent paymentIntent = createPaymentIntent(paymentInfo);
paymentStr = paymentIntent.toJson();
} catch (Exception ex) {
ex.printStackTrace();
return new APIGatewayProxyResponseEvent()
.withStatusCode(400)
.withBody("Error processing the request");
}
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
response.withStatusCode(200).withBody(paymentStr);
return response;
}
private PaymentIntent createPaymentIntent(PaymentInfo paymentInfo) throws StripeException {
Stripe.apiKey = "sk_test_5....something";
List<String> paymentMethodeTypes = new ArrayList<>();
paymentMethodeTypes.add("card");
Map<String, Object> params = new HashMap<>();
params.put("amount", paymentInfo.getAmount());
params.put("currency", paymentInfo.getCurrency());
params.put("payment_method_types", paymentMethodeTypes);
return PaymentIntent.create(params);
}
}
PaymentInfo.class
public class PaymentInfo {
private Long amount;
private String currency;
public PaymentInfo() {}
public PaymentInfo(Long amount, String currency) {
this.amount = amount;
this.currency = currency;
}
public Long getAmount() {
return amount;
}
public String getCurrency() {
return currency;
}
public void setAmount(Long amount) {
this.amount = amount;
}
public void setCurrency(String currency) {
this.currency = currency;
}
@Override
public String toString() {
return "PaymentInfo [amount=" + amount + ", currency=" + currency + "]";
}
}
代码在响应中返回null。我期待这样的回应:
{
"id": "pi_3MQBPOHNXSQZoPBk0KGKrqAV",
"object": "payment_intent",
"amount": 2000,
"amount_capturable": 0,
"amount_details": {
"tip": {}
},
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"client_secret": "pi_remo",
"confirmation_method": "automatic",
"created": 1673709158,
"currency": "usd",
"customer": null,
"description": null,
"invoice": null,
"last_payment_error": null,
"latest_charge": null,
"livemode": false,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {
"card": {
"installments": null,
"mandate_options": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"processing": null,
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "requires_payment_method",
"transfer_data": null,
"transfer_group": null
}
前端(angular) onSubmit()函数:
onSubmit() {
// compute payment info
this.paymentInfo.amount = Math.round(this.totalPrice * 100);
this.paymentInfo.currency = "CAD";
if (!this.checkoutFormGroup.invalid && this.displayError.textContent === "") {
this.checkoutService.createPaymentIntent(this.paymentInfo).subscribe(
(paymentIntentResponse) => {
this.stripe.confirmCardPayment(paymentIntentResponse.client_secret,
{
payment_method: {
card: this.cardElement
}
}, { handleActions: false })
.then(function(result) {
if (result.error) {
// inform the customer there was an error
alert(`There was an error: ${result.error.message}`);
} else {
// reset cart
this.resetCart();
}
}.bind(this));
}
);
} else {
this.checkoutFormGroup.markAllAsTouched();
return;
}
}
我期望paymentIntentResponse包含client_secret和其他记录,但它是空的。在CheckoutService代码下面:
import { PaymentInfo } from './../common/payment-info';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class CheckoutService {
private paymentIntentUrl = 'https://removed real path amazonaws.com/api/payment-intent';
constructor(private httpClient: HttpClient) { }
createPaymentIntent(paymentInfo: PaymentInfo): Observable<any>{
console.log("paymentInfo - ", paymentInfo)
return this.httpClient.post<PaymentInfo>(this.paymentIntentUrl, paymentInfo);
}
}
尝试Spring boot Rest Controller (elastic beanstalk)实现Stripe Api,前端和后端工作正常,无法在AWS Lambda上工作。
根据您共享的内容,API密钥为空(即Stripe.apiKey = "";
)。为了成功创建一个PaymentIntent,你应该从Stripe Dashboard获取你的API key,并在你的代码中设置它。