1

我正在尝试创建订阅 i Paymill。我已经阅读了他们的例子,但我似乎不明白。

我真正想做的就是设置订阅,这是我当前的代码:

$token = $_POST['paymillToken'];

if ($token) {
    require "Services/Paymill/Payments.php";
    require "Services/Paymill/Transactions.php";
    require "Services/Paymill/Subscriptions.php";
    require "Services/Paymill/Offers.php";


  $params = array(
    'amount'      => '49900',  // Cent!
    'currency'    => 'SEK',   // ISO 4217
    'token'       => $token,
    'description' => 'User ID# ' . $userIdMain . ' Email: ' . $userEmail
  );

$transactionsObject = new Services_Paymill_Transactions(
    PAYMILL_API_KEY, PAYMILL_API_HOST
);
$transaction        = $transactionsObject->create($params);

echo "<br /><br />";    
print_r($transaction);

echo $transaction['client']['id'];
echo $transaction['payment']['id'];


$params = array(
    'client'   => $transaction['client']['id'],
    'offer'    => 'offer_9cdffb501f565bf827a8',
    'payment'  => $transaction['payment']['id']
);
$subscriptionsObject = new Services_Paymill_Subscriptions(PAYMILL_API_KEY, PAYMILL_API_HOST);
$subscription        = $subscriptionsObject->create($params);

echo "<br /><br />";    
print_r($subscription);

} 

问题是上面一次创建了两个付款。但似乎订阅对象要求我首先有一个 payment_id (参见上面的 $transaction['payment']['id'] )。

我在这里做错了什么?

4

2 回答 2

3

创建订阅也会创建一个事务,这是正确的。添加支付对象 ( https://www.paymill.com/en-gb/documentation-3/reference/api-reference/#create-new-credit-card-payment-with )而不是创建新交易并通过Subscription->create() 的 id。

于 2013-03-18T12:37:28.300 回答
0

好吧,这不是 PHP,但在正确的步骤设置订阅后,我也有同样的不确定性。最后,这很容易,所以我到目前为止发布我的代码以防万一。

该应用程序是 MVC .net,我正在使用 PayMill PayButton 生成支付令牌。这是注册过程中模型的一部分。以下代码调用 PayMill API 的 .net 包装器。

   private static async Task<Subscription> MakePayMillSubscription(int amount, ApplicationUser user, PaymillContext paymillContext, Payment payment, Client client)
    {

        SubscriptionService subscriptionService = paymillContext.SubscriptionService;
        Subscription subscription = await subscriptionService.CreateAsync( payment, client, null, amount, "EUR", Interval.periodWithChargeDay(1, Interval.TypeUnit.MONTH), null, user.Id, Interval.period(10, Interval.TypeUnit.YEAR));
        return subscription;
    }

    private static async Task<Client> MakePayMillClient(RegisterViewModel model, ApplicationUser user, PaymillContext paymillContext)
    {

        ClientService clientService = paymillContext.ClientService;
        Client client = await clientService.CreateWithEmailAndDescriptionAsync(
            model.Email,
            user.Id
        );
        return client;
    }

    private static async Task<Payment> MakePayMillPayment(RegisterViewModel model,PaymillContext context, Client client)
    {

        Payment payment = await context.PaymentService.CreateWithTokenAndClientAsync(model.paymillToken, client);

        return payment;
    }

然后我现在像这样在注册期间调用这些(我仍处于测试阶段)

  client = await MakePayMillClient(model, user, paymillContext);
   payment = await MakePayMillPayment(model, paymillContext,client);
   subscription = await MakePayMillSubscription((int)(account.TotalAmountDue * 100.0M), user, paymillContext, payment, client);
于 2015-01-18T19:45:20.410 回答