如何获取Charge id Stripe并将其写入表?

问题描述

使用Stripe创建费用时,将返回一个Charge对象,我想将费用ID写入数据库以供进一步使用。

我需要获取的Charge对象及其想要写入基数的ID的示例

{
"id": "ch_1HU7oJBvP3uypX39ZQk1n8JC","object": "charge","amount": 100,"amount_captured": 0,"amount_refunded": 0,"application": null,"application_fee": null,"application_fee_amount": null,"balance_transaction": "txn_1GG1SWBvP3uypX39X6sCO4we","billing_details": {
"address": {
  "city": null,"country": null,"line1": null,"line2": null,"postal_code": null,"state": null
  },"email": null,"name": "Jenny Rosen","phone": null
},"calculated_statement_descriptor": null,"captured": false,"created": 1600766959,"currency": "dkk","customer": null,"description": "My First Test Charge (created for API docs)","disputed": false,"failure_code": null,"failure_message": null,"fraud_details": {},"invoice": null,"livemode": false,"Metadata": {},"on_behalf_of": null,"order": null,"outcome": null,"paid": true,"payment_intent": null,"payment_method": "card_1HU6xpBvP3uypX39jOlnayk1","payment_method_details": {
"card": {
  "brand": "visa","checks": {
    "address_line1_check": null,"address_postal_code_check": null,"cvc_check": "pass"
  },"country": "US","exp_month": 8,"exp_year": 2021,"fingerprint": "3D881K0RUc9Adq5Z","funding": "credit","installments": null,"last4": "4242","network": "visa","three_d_secure": null,"wallet": null
  },"type": "card"
},"receipt_email": null,"receipt_number": null,"receipt_url": "https://pay.stripe.com/receipts/","refunded": false,"refunds": {
"object": "list","data": [],"has_more": false,"url": "/v1/charges/ch_1HU7oJBvP3uypX39ZQk1n8JC/refunds"
},"review": null,"shipping": null,"source_transfer": null,"statement_descriptor": null,"statement_descriptor_suffix": null,"status": "succeeded","transfer_data": null,"transfer_group": null
}

payment_customer表对应于PaymentCustomerEntity实体

payment_customer table

服务中的方法

public String charge(String customerId,double amount,String description,Map<String,String> params) {
try {
    Map<String,Object> chargeParams = new HashMap<>();
    chargeParams.put("amount",(int) (amount * 100));
    chargeParams.put("currency","dkk");
    chargeParams.put("customer",customerId);
    if (description != null)
        chargeParams.put("description",description);
    if (params != null) {
        chargeParams.put("Metadata",params);
    }
    Charge charge = Charge.create(chargeParams);

    //setchargeId
    if(paymentCustomerDao.getByStripeId(customerId)!= null) {
        PaymentCustomerEntity paymentCustomerEntity = 
  paymentCustomerDao.getByStripeId(customerId);
        paymentCustomerEntity.setStripeChargeId(charge.getId());
    }
    return charge.getId();
} catch (StripeException e) {
    throw ErrorMessage.build(ERROR_SERVER_INTERNAL)
            .logLevel(LogLevel.ERROR)
            .cause(e)
            .detail(STRIPE_CUSTOMER_ID,customerId)
            .httpStatus(INTERNAL_SERVER_ERROR)
            .exception();
}
}

实体类

@Entity
@Table(name = "payment_customer")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PaymentCustomerEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "email")
private String email;

@Column(name = "stripe_id")
private String stripeId;

@Column(name = "charge_id")
private String stripeChargeId;

@OnetoOne(fetch = FetchType.EAGER)
@JoinColumn(name = "address_id")
private AddressEntity address;


@OnetoOne(fetch = FetchType.EAGER)
@JoinColumn(name = "created_by_user_id")
private UserEntity createdByUser;

@Column(name = "create_time")
private Timestamp createTime;

public PaymentCustomerEntity(String stripeCustomerId,UserEntity userEntity) {
this.email = userEntity.getEmail();
this.stripeId = stripeCustomerId;
this.address = userEntity.getAddress();
this.createdByUser = userEntity;
createTime = DateTimeUtil.getCurrentTimestamp();
}
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)