Paymill订阅PHP

时间:2013-03-17 10:19:27

标签: php paymill

我正在尝试创建一个订阅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']。)

我在这里做错了什么?

2 个答案:

答案 0 :(得分:3)

创建订阅也会创建一个正确的事务。添加付款对象(https://www.paymill.com/en-gb/documentation-3/reference/api-reference/#create-new-credit-card-payment-with而不是创建新交易并将ID传递给Subscription-&gt; create()。

答案 1 :(得分: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);