C#:在条带中设置默认付款方式

时间:2017-06-28 08:08:55

标签: c# asp.net stripe-payments payment-gateway stripe.net

我是条纹新手,我们如何在条纹中设置默认付款方式。

我们可以通过cardId / sourceId向customerId收取客户费用。

代码: -

<form role="form" method="POST">
    <br style="clear:both">
    <h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
    <div class="form-group">
    <input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
        <select name="situation" id="situation">
            <option>Select Current Situation</option>
          <option class="placeholder" value="Unemployed">Unemployed</option>
          <option class="placeholder" value="Employed">Employed</option>
        </select>
    </div>
    <button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

    <?php
        if (isset($_POST["submit"])) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $mobile = $_POST['mobile'];
            $subject_line = $_POST['subject'];
            $situation = $_POST['situation'];
            $from = 'myemail@email.co.za'; 
            $to = 'myemail@email.co.za'; 
            $subject = 'SchoemanLaw lead ...';

            $body ="From: $name <br/> E-Mail: $email <br/> Mobile:  $mobile Subject: $subject_line <br/> Situation: $situation";

            //$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";

            // set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

            // More headers optional/headers 
            $headers .= "From:$from";

             // Check if name has been entered
            if (!$_POST['name']) {
                $errName = 'Please enter your name';
            }

            // Check if email has been entered and is valid
            if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                $errEmail = 'Please enter a valid email address';
            }

             // Check if mobile has been entered
            if (!$_POST['mobile']) {
                $errMobile = 'Please enter your number';
            }

            // If there are no errors, send the email
            if (!$errName && !$errEmail && !$errMobile) {
                if (mail($to,$subject,$body,$headers)) {
                    $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
                    echo $result;
                } else {
                    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
                    echo $result;
                }
            }
        }   
    ?>
 </form>

还有一个问题,如何获取费用列表,我使用下面的代码,但得到异常(转换错误): -

private static async Task<string> ChargeCustomer(string customerId)
{
    return await System.Threading.Tasks.Task.Run(() =>
    {
        var myCharge = new StripeChargeCreateOptions
        {
            Amount = 50,
            Currency = "gbp",
            Description = "Charge for property sign and postage",
            CustomerId = customerId
        };

        var chargeService = new StripeChargeService();
        var stripeCharge = chargeService.Create(myCharge);

        return stripeCharge.Id;
    });
}

3 个答案:

答案 0 :(得分:1)

我们可以在 StripeChargeCreateOptions 的SourceTokenOrExistingSourceId属性中传递cardId / BankAccountId / TokenId / SourceId,

private static async Task<string> ChargeCustomer(string customerId, string cardId)
        {
            try
            {
                return await System.Threading.Tasks.Task.Run(() =>
                {
                    var myCharge = new StripeChargeCreateOptions
                    {
                        Amount = 50,
                        Currency = "gbp",
                        Description = "Charge for property sign and postage",
                        CustomerId = customerId,
                        SourceTokenOrExistingSourceId = cardId
                    };

                    var chargeService = new StripeChargeService();
                    var stripeCharge = chargeService.Create(myCharge);

                    return stripeCharge.Id;
                });
            }
            catch(Exception ex)
            {
                return "";
            }
        }

设置/更改默认付款方式: -

 public void ChangeDefaultPayment(string customerId, string sourceId)
    {
        var myCustomer = new StripeCustomerUpdateOptions();
        myCustomer.DefaultSource = sourceId;
        var customerService = new StripeCustomerService();
        StripeCustomer stripeCustomer = customerService.Update(customerId, myCustomer);
    }

仍然在寻找如何获得收费清单。

答案 1 :(得分:0)

internal static IEnumerable<StripeCharge> GetChargeList()
        {
            var chargeService = new StripeChargeService();
            return chargeService.List();
        }

它对我来说很好。请确保您在项目中安装了“Newtonsoft.Json”dll,因为我在开始条形码付款时出现此错误。 here is the response received from stripe server.

答案 2 :(得分:0)

这就是我最终要做的。不知道为什么Stripe Checkout没有将预订设置的卡设置为默认卡。无论如何,这是由payment_intent.succeeded Web挂钩触发的。当然有更好的方法,但是...

    var customerService = new CustomerService(Configs.STRIPE_SECRET_KEY);
    var c = customerService.Get(pi.CustomerId);

    if (!string.IsNullOrEmpty(c.InvoiceSettings.DefaultPaymentMethodId)) {
      status = "already has default payment method, no action";
      hsc = HttpStatusCode.OK;
      return;
    }


    var paymentMethodService = new PaymentMethodService(Configs.STRIPE_SECRET_KEY);
    var lopm = paymentMethodService.ListAutoPaging(options: new PaymentMethodListOptions {
      CustomerId = pi.CustomerId,
      Type = "card"
    });

    if (!lopm.Any()) {
      status = "customer has no payment methods";
      hsc = HttpStatusCode.BadRequest;
      return;
    }
    var pm = lopm.FirstOrDefault();
    customerService.Update(pi.CustomerId, options: new CustomerUpdateOptions {
      InvoiceSettings = new CustomerInvoiceSettingsOptions {
        DefaultPaymentMethodId = pm.Id
      }
    });

    hsc = HttpStatusCode.OK;
    return;